7 #include <sys/select.h>
8 #include <sys/socket.h>
18 static pthread_mutex_t mtx;
19 static uint8_t start_conn_thread = 0;
21 static int copy_socket(
int fd_read,
int fd_write,
char* buff)
23 int rsize, towrite, wsize;
35 while ((wsize = write(fd_write, buff, towrite)) > 0)
47 static void* conn_thread(
void* arg)
50 char* buff = (
char*) calloc(
BUFF_SIZE,
sizeof(
char));
52 LOGE(
"Socket copy thread %u: Unable to alloc %d bytes", pthread_self(),
BUFF_SIZE);
68 if (select(nfds + 1, &set_read, NULL, NULL, NULL) < 0)
84 LOGI(
"Socket copy thread %u: Stopping", pthread_self());
94 static void* sync_conn_thread(
void* arg)
98 int nop, written, cmd;
102 recv(main_socket, &nop,
sizeof(nop), MSG_WAITALL);
107 pthread_mutex_lock(&mtx);
108 start_conn_thread = 1;
109 pthread_mutex_unlock(&mtx);
114 written = write(main_socket, &cmd,
sizeof(cmd));
115 if (written !=
sizeof(cmd))
116 LOGW(
"write() returned %d", written);
122 LOGD(
"sync thread: received %d ", nop);
130 pthread_mutex_init(&mtx, NULL);
137 pthread_t sync_thread_id;
138 pthread_t new_thread_id;
146 LOGW(
"connect() error: %s", strerror(errno));
150 LOGI(
"OpenGL management connected to socket %d", main_socket);
153 if (write(main_socket, &cmd,
sizeof(cmd)) == -1)
154 LOGW(
"Unable to write data port to main connection - error %d (%s)", errno,
158 rc = pthread_create(&sync_thread_id, NULL, sync_conn_thread, &main_socket);
161 LOGE(
"pthread_create returned %d", rc);
172 LOGW(
"connect() error: %s", strerror(errno));
176 LOGI(
" Connected to the VM with socket %d", hw_socket);
183 LOGE(
"Cannot allocate memory");
190 rc = pthread_create(&new_thread_id, NULL, conn_thread, (
void*) new_cd);
197 LOGE(
"pthread_create returned %d", rc);
200 LOGI(
"New gl thread created");
202 while (!start_conn_thread)
204 struct timespec duration = {0, 100000};
205 nanosleep(&duration, NULL);
208 pthread_mutex_lock(&mtx);
209 start_conn_thread = 0;
210 pthread_mutex_unlock(&mtx);
#define LOGD(...)
Log at DEBUG level.
Data structure to hold the two connections.
#define OPENGL_PING
Command sent by the VM to check if we’re still alive.
#define LOGE(...)
Log at ERROR level (makes the application abort)
int socket_t
Alias to differenciate between regular ints and socket fds.
#define BUFF_SIZE
Max size of opengl reads.
socket_t open_socket_nodelay(const char *ip, short port)
Open a socket with TCP_NODELAY.
#define OPENGL_START_COMMAND
Command sent to initiate the remote graphics exchange.
#define LOGW(...)
Log at WARNING level.
int manage_socket_gl(void *arg)
Manage the remote OpenGL to the VM.
socket_t open_socket_reuseaddr(const char *ip, short port)
Open a socket with SO_REUSEADDR.
#define SOCKET_ERROR
Alias for the recv() return value in case of error.
Define socket utilities to simplify networking.
#define LOGI(...)
Log at INFO level.
#define OPENGL_PONG
Reply to make tell the VM we’re still alive.