-
-
Save mgood7123/fefe03b3fc1c89a875fdcb729834ab90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if _WIN32 | |
int | |
wl_connection_flush(struct wl_connection *connection) | |
{ | |
struct iovec iov[2]; | |
int len = 0, count; | |
size_t clen; | |
uint32_t tail; | |
if (!connection->want_flush) | |
return 0; | |
tail = connection->out.tail; | |
uid_t uid = 0; gid_t gid = 0; pid_t pid = 0; | |
struct wl_ring_buffer * buffer = &connection->fds_out; | |
struct RING_BUFFER_FD_STORAGE s = {0}; | |
RING_BUFFER_INIT_STORAGE(buffer, s); | |
do { | |
len = send(connection->fd, s.len, sizeof(size_t), 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
if (s.len > 0) { | |
wl_os_socket_peercred(connection->fd, &uid, &gid, &pid); | |
} | |
if (s.len > 0) { | |
// send fd's first | |
size_t handle_array_item_size = sizeof(FD_HANDLE_T); | |
size_t handle_array_bytes = handle_array_item_size * s.len; | |
FD_HANDLE_T * handle_array = zalloc(handle_array_bytes); | |
FD_HANDLE_T * handle_array_start = handle_array; | |
int handle_count = 0; | |
size_t socket_array_item_size = sizeof(WSAPROTOCOL_INFO); | |
size_t socket_array_bytes = socket_array_item_size * s.len; | |
WSAPROTOCOL_INFO * socket_array = zalloc(socket_array_bytes); | |
WSAPROTOCOL_INFO * socket_array_start = socket_array; | |
int socket_count = 0; | |
for (FD_HANDLE_T * fd = RING_BUFFER_GET_NEXT_FD(s); fd != NULL; fd = RING_BUFFER_GET_NEXT_FD(s)) { | |
if (FD_HANDLE_IS_SOCKET(*fd)) { | |
if (WSADuplicateSocket(*fd, pid, socket_array_start) == SOCKET_ERROR) { | |
wl_log("WSADuplicateSocket(): failed. Error = %d\n", WSAGetLastError()); | |
continue; | |
} | |
socket_array_start += socket_array_item_size; | |
socket_count++; | |
} else { | |
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); | |
if (!DuplicateHandle( | |
GetCurrentProcess(), | |
*fd, | |
hProcess, | |
handle_array_start, | |
DUPLICATE_SAME_ACCESS, | |
FALSE, | |
DUPLICATE_SAME_ACCESS | |
)) { | |
wl_log("DuplicateHandle(): failed. Error = %d\n", GetLastError()); | |
CloseHandle(hProcess); | |
continue; | |
} | |
handle_array_start += handle_array_item_size; | |
handle_count++; | |
CloseHandle(hProcess); | |
} | |
} | |
bool has[2] = { socket_count != 0, handle_count != 0}; | |
do { | |
len = send(connection->fd, has, sizeof(bool)*2, 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
if (has[0]) { | |
size_t size = socket_array_item_size * socket_count; | |
do { | |
len = send(connection->fd, size, sizeof(size_t), 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
do { | |
len = send(connection->fd, socket_array, size, 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
} | |
if (has[1]) { | |
size_t size = handle_array_item_size * handle_count; | |
do { | |
len = send(connection->fd, size, sizeof(size_t), 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
do { | |
len = send(connection->fd, handle_array, size, 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
} | |
free(handle_array); | |
free(socket_array); | |
close_fds(&connection->fds_out, MAX_FDS_OUT); | |
} | |
if (len == SOCKET_ERROR) | |
return -1; | |
// send data | |
while (connection->out.head - connection->out.tail > 0) { | |
ring_buffer_get_iov(&connection->out, iov, &count); | |
if (len == SOCKET_ERROR) | |
return -1; | |
struct wl_ring_buffer * buffer = &connection->out; | |
do { | |
len = send(connection->fd, ring_buffer_size(buffer), sizeof(size_t), 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
do { | |
len = send(connection->fd, connection->out.data, ring_buffer_size(buffer), 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
connection->out.tail += len; | |
} | |
connection->want_flush = 0; | |
return connection->out.head - tail; | |
} | |
uint32_t | |
wl_connection_pending_input(struct wl_connection *connection) | |
{ | |
return ring_buffer_size(&connection->in); | |
} | |
int | |
wl_connection_read(struct wl_connection *connection) | |
{ | |
struct iovec iov[2]; | |
char cmsg[CLEN]; | |
int len, count, ret; | |
if (ring_buffer_size(&connection->in) >= sizeof(connection->in.data)) { | |
errno = EOVERFLOW; | |
return -1; | |
} | |
size_t s_len; | |
do { | |
len = recv(connection->fd, &s_len, sizeof(size_t), 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
if (s_len > 0) { | |
bool has[2]; | |
do { | |
len = recv(connection->fd, has, sizeof(bool)*2, 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
if (has[0]) { | |
size_t size; | |
do { | |
len = recv(connection->fd, &size, sizeof(size_t), 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
WSAPROTOCOL_INFO * socket_array = zalloc(size); | |
do { | |
len = recv(connection->fd, socket_array, size, 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
} | |
if (has[1]) { | |
size_t size; | |
do { | |
len = recv(connection->fd, &size, sizeof(size_t), 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
FD_HANDLE_T * handle_array = zalloc(size); | |
do { | |
len = recv(connection->fd, handle_array, size, 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
} | |
} | |
// recieve data | |
size_t ring_buf_len; | |
do { | |
len = recv(connection->fd, &ring_buf_len, sizeof(size_t), 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
RING_BUFFER_TYPE * ring_buf_data = zalloc(ring_buf_len); | |
do { | |
len = recv(connection->fd, ring_buf_data, ring_buf_len, 0); | |
} while (len == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); | |
if (len <= 0) | |
return len; | |
connection->in.head += len; | |
return wl_connection_pending_input(connection); | |
} | |
#else |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment