32 #include <sys/select.h>
36 #ifndef UDP_THREAD_PRIO
37 #define UDP_THREAD_PRIO 10
40 static void *
udp_thread(
void *data __attribute__((unused)));
41 static pthread_mutex_t
udp_mutex = PTHREAD_MUTEX_INITIALIZER;
58 if (pthread_create(&tid, NULL,
udp_thread, NULL) != 0) {
59 fprintf(stderr,
"udp_arch_init: Could not create UDP reading thread.\n");
63 pthread_setname_np(tid,
"udp");
75 p->network = (
void *)sock;
86 int available =
p->rx_insert_idx -
p->rx_extract_idx;
102 uint8_t ret =
p->rx_buf[
p->rx_extract_idx];
113 if (
p == NULL) {
return; }
114 if (
p->network == NULL) {
return; }
121 if (available <= 0) {
125 socklen_t slen =
sizeof(
struct sockaddr_in);
126 ssize_t byte_read = recvfrom(sock->
sockfd, buf, available, MSG_DONTWAIT,
127 (
struct sockaddr *)&sock->
addr_in, &slen);
132 for (i = 0; i < byte_read; i++) {
133 p->rx_buf[
p->rx_insert_idx] = buf[i];
146 if (
p == NULL) {
return; }
147 if (
p->network == NULL) {
return; }
151 if (
p->tx_insert_idx > 0) {
152 ssize_t bytes_sent = sendto(sock->
sockfd,
p->tx_buf,
p->tx_insert_idx, MSG_DONTWAIT,
154 if (bytes_sent !=
p->tx_insert_idx) {
155 if (bytes_sent < 0) {
156 perror(
"udp_send_message failed");
158 fprintf(stderr,
"udp_send_message: only sent %d bytes instead of %d\n",
159 (
int)bytes_sent,
p->tx_insert_idx);
162 p->tx_insert_idx = 0;
171 if (
p == NULL) {
return; }
172 if (
p->network == NULL) {
return; }
175 ssize_t test __attribute__((unused)) = sendto(sock->
sockfd, buffer, size, MSG_DONTWAIT,
192 FD_ZERO(&socks_master);
194 int fd __attribute__((unused));
197 FD_SET(
fd, &socks_master);
204 FD_SET(
fd, &socks_master);
211 FD_SET(
fd, &socks_master);
222 socks = socks_master;
224 if (select(fdmax + 1, &socks, NULL, NULL, NULL) < 0) {
225 fprintf(stderr,
"udp_thread: select failed!");
229 if (FD_ISSET(
fd, &socks)) {
235 if (FD_ISSET(
fd, &socks)) {
241 if (FD_ISSET(
fd, &socks)) {