关注下面的红色字体部分即可。
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "LocalSocketImpl"
#include "JNIHelp.h"
#include "jni.h"
#include "utils/Log.h"
#include "utils/misc.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <cutils/sockets.h>
#include <netinet/tcp.h>
namespace android {
static jfieldID field_inboundFileDescriptors;
static jfieldID field_outboundFileDescriptors;
static jclass class_Credentials;
static jclass class_FileDescriptor;
static jmethodID method_CredentialsInit;
/*
* private native FileDescriptor
* create_native(boolean stream)
* throws IOException;
*/
static jobject
socket_create (JNIEnv *env, jobject object, jboolean stream)
{
int ret;
ret = socket(PF_LOCAL, stream ? SOCK_STREAM : SOCK_DGRAM, 0);
if (ret < 0) {
jniThrowIOException(env, errno);
return NULL;
}
return jniCreateFileDescriptor(env,ret);
}
/* private native void connectLocal(FileDescriptor fd,
* String name, int namespace) throws IOException
*/
static void
socket_connect_local(JNIEnv *env, jobject object,
jobject fileDescriptor, jstring name, jint namespaceId)
{
int ret;
const char *nameUtf8;
int fd;
nameUtf8 = env->GetStringUTFChars(name, NULL);
fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
if (env->ExceptionOccurred() != NULL) {
return;
}
ret = socket_local_client_connect(
fd,
nameUtf8,
namespaceId,
SOCK_STREAM);
env->ReleaseStringUTFChars(name, nameUtf8);
if (ret < 0) {
jniThrowIOException(env, errno);
return;
}
}
#define DEFAULT_BACKLOG 4
/* private native void bindLocal(FileDescriptor fd, String name, namespace)
* throws IOException;
*/
static void
socket_bind_local (JNIEnv *env, jobject object, jobject fileDescriptor,
jstring name, jint namespaceId)
{
int ret;
int fd;
const char *nameUtf8;
if (name == NULL) {
jniThrowNullPointerException(env, NULL);
}
fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
if (env->ExceptionOccurred() != NULL) {
return;
}
nameUtf8 = env->GetStringUTFChars(name, NULL);
ret = socket_local_server_bind(fd, nameUtf8, namespaceId);
env->ReleaseStringUTFChars(name, nameUtf8);
if (ret < 0) {
jniThrowIOException(env, errno);
return;
}
}
/* private native void listen_native(int fd, int backlog) throws IOException; */
static void
socket_listen (JNIEnv *env, jobject object, jobject fileDescriptor, int backlog)
{
int ret;
int fd;
fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
if (env->ExceptionOccurred() != NULL) {
return;
}
ret = listen(fd, backlog);
if (ret < 0) {
jniThrowIOException(env, errno);
return;
}
}
/* private native FileDescriptor
** accept (FileDescriptor fd, LocalSocketImpl s)
** throws IOException;
*/
static jobject
socket_accept (JNIEnv *env, jobject object, jobject fileDescriptor, jobject s)
{
union {
struct sockaddr address;
struct sockaddr_un un_address;
} sa;
int ret;
int retFD;
int fd;
socklen_t addrlen;
if (s == NULL) {
jniThrowNullPointerException(env, NULL);
return NULL;
}
fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
if (env->ExceptionOccurred() != NULL) {
return NULL;
}
do {
addrlen = sizeof(sa);
ret = accept(fd, &(sa.address), &addrlen);
} while (ret < 0 && errno == EINTR);
if (ret < 0) {
jniThrowIOException(env, errno);
return NULL;
}
retFD = ret;
return jniCreateFileDescriptor(env, retFD);
}
/* private native void shutdown(FileDescriptor fd, boolean shutdownInput) */
static void
socket_shutdown (JNIEnv *env, jobject object, jobject fileDescriptor,
jboolean shutdownInput)
{
int ret;
int fd;
fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
if (env->ExceptionOccurred() != NULL) {
return;
}
ret = shutdown(fd, shutdownInput ? SHUT_RD : SHUT_WR);</