C++
#include <stdio.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <jni.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int create_pipe(int pipe_fd[2]){
memset(pipe_fd, 0, sizeof(int) * 2);
if (pipe(pipe_fd) == -1){
printf("pipe fail\n");
return -1;
}
printf("pipe success\n");
return 0;
}
void send(int fd, int x, int y, int z, int t){
char data[16] = {0};
memcpy(data, &x, 4);
memcpy(data+4, &y, 4);
memcpy(data+8, &z, 4);
memcpy(data+12, &t, 4);
write(fd, data, sizeof(data));
}
int pipe_fd[2];
if(create_pipe(pipe_fd)){
return 1;
}
if(fork() == 0){
xxx(pipe_fd[0]);
} else {
int y = 100;
while(true){
y = y > 800 ? 100 : y + 1;
sendInput(pipe_fd[1], 200, y, 7, 20);
usleep(10000);
}
}
java:
Constructor<FileDescriptor> con = FileDescriptor.class.getDeclaredConstructor(int.class);
con.setAccessible(true);
FileDescriptor fileDescriptor = con.newInstance(fd);
//ParcelFileDescriptor pfd = ParcelFileDescriptor.fromFd(fd);
//FileDescriptor fileDescriptor = pfd.getFileDescriptor();
inputStream = new FileInputStream(fileDescriptor);
inputStream.read(data);