Skip to content

Instantly share code, notes, and snippets.

@Subv
Last active October 3, 2017 00:11
Show Gist options
  • Save Subv/213e410fb3dad999d6d2c919497d1490 to your computer and use it in GitHub Desktop.
Save Subv/213e410fb3dad999d6d2c919497d1490 to your computer and use it in GitHub Desktop.
Patch to make enet compile under devkitARM for the Nintendo 3DS
diff --git a/unix.c b/unix.c
index c36a082..96b6ae6 100644
--- a/unix.c
+++ b/unix.c
@@ -51,7 +51,7 @@
#endif
#ifdef HAS_POLL
-#include <sys/poll.h>
+#include <poll.h>
#endif
#ifndef HAS_SOCKLEN_T
@@ -288,7 +288,7 @@ enet_socket_get_address (ENetSocket socket, ENetAddress * address)
int
enet_socket_listen (ENetSocket socket, int backlog)
{
- return listen (socket, backlog < 0 ? SOMAXCONN : backlog);
+ return listen (socket, backlog < 0 ? 16 : backlog);
}
ENetSocket
@@ -311,10 +311,6 @@ enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
#endif
break;
- case ENET_SOCKOPT_BROADCAST:
- result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int));
- break;
-
case ENET_SOCKOPT_REUSEADDR:
result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int));
break;
@@ -327,24 +323,6 @@ enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int));
break;
- case ENET_SOCKOPT_RCVTIMEO:
- {
- struct timeval timeVal;
- timeVal.tv_sec = value / 1000;
- timeVal.tv_usec = (value % 1000) * 1000;
- result = setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & timeVal, sizeof (struct timeval));
- break;
- }
-
- case ENET_SOCKOPT_SNDTIMEO:
- {
- struct timeval timeVal;
- timeVal.tv_sec = value / 1000;
- timeVal.tv_usec = (value % 1000) * 1000;
- result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & timeVal, sizeof (struct timeval));
- break;
- }
-
case ENET_SOCKOPT_NODELAY:
result = setsockopt (socket, IPPROTO_TCP, TCP_NODELAY, (char *) & value, sizeof (int));
break;
@@ -434,11 +412,11 @@ enet_socket_send (ENetSocket socket,
const ENetBuffer * buffers,
size_t bufferCount)
{
- struct msghdr msgHdr;
+ /*struct msghdr msgHdr;*/
struct sockaddr_in sin;
int sentLength;
- memset (& msgHdr, 0, sizeof (struct msghdr));
+ /*memset (& msgHdr, 0, sizeof (struct msghdr));*/
if (address != NULL)
{
@@ -448,15 +426,41 @@ enet_socket_send (ENetSocket socket,
sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
sin.sin_addr.s_addr = address -> host;
- msgHdr.msg_name = & sin;
- msgHdr.msg_namelen = sizeof (struct sockaddr_in);
+ /*msgHdr.msg_name = & sin;
+ msgHdr.msg_namelen = sizeof (struct sockaddr_in);*/
}
- msgHdr.msg_iov = (struct iovec *) buffers;
+ /*msgHdr.msg_iov = (struct iovec *) buffers;
msgHdr.msg_iovlen = bufferCount;
- sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL);
+ sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL);*/
+
+ size_t total_size = 0;
+ for (size_t i = 0; i < bufferCount; ++i)
+ {
+ total_size += buffers[i].dataLength;
+ }
+
+ ENetBuffer finalBuffer;
+ finalBuffer.data = malloc(total_size);
+ finalBuffer.dataLength = total_size;
+ size_t copied_size = 0;
+ for (size_t i = 0; i < bufferCount; ++i)
+ {
+ memcpy(finalBuffer.data + copied_size, buffers[i].data, buffers[i].dataLength);
+ copied_size += buffers[i].dataLength;
+ }
+
+ if (address != NULL)
+ {
+ sentLength = sendto (socket, finalBuffer.data, finalBuffer.dataLength, 0, (struct sockaddr *)&sin, sizeof (struct sockaddr_in));
+ }
+ else
+ {
+ sentLength = send (socket, finalBuffer.data, finalBuffer.dataLength, 0);
+ }
+
if (sentLength == -1)
{
if (errno == EWOULDBLOCK)
@@ -474,11 +478,11 @@ enet_socket_receive (ENetSocket socket,
ENetBuffer * buffers,
size_t bufferCount)
{
- struct msghdr msgHdr;
+ /*struct msghdr msgHdr;*/
struct sockaddr_in sin;
int recvLength;
- memset (& msgHdr, 0, sizeof (struct msghdr));
+ /*memset (& msgHdr, 0, sizeof (struct msghdr));
if (address != NULL)
{
@@ -489,7 +493,46 @@ enet_socket_receive (ENetSocket socket,
msgHdr.msg_iov = (struct iovec *) buffers;
msgHdr.msg_iovlen = bufferCount;
- recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL);
+ recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL);*/
+
+ size_t total_size = 0;
+ for (size_t i = 0; i < bufferCount; ++i)
+ {
+ total_size += buffers[i].dataLength;
+ }
+
+ ENetBuffer finalBuffer;
+ finalBuffer.data = malloc(total_size);
+ finalBuffer.dataLength = total_size;
+
+ if (address != NULL)
+ {
+ socklen_t addrlen = sizeof (struct sockaddr_in);
+ recvLength = recvfrom (socket, finalBuffer.data, finalBuffer.dataLength, 0, (struct sockaddr *)&sin, &addrlen);
+ }
+ else
+ {
+ recvLength = send (socket, finalBuffer.data, finalBuffer.dataLength, 0);
+ }
+
+ size_t copied_size = 0;
+ size_t current_buffer = 0;
+ while (copied_size < recvLength && current_buffer < bufferCount)
+ {
+ size_t remaining = recvLength - copied_size;
+ if (buffers[current_buffer].dataLength > remaining)
+ {
+ memcpy(buffers[current_buffer].data, finalBuffer.data + copied_size, remaining);
+ copied_size += remaining;
+ }
+ else
+ {
+ memcpy(buffers[current_buffer].data, finalBuffer.data + copied_size, buffers[current_buffer].dataLength);
+ copied_size += buffers[current_buffer].dataLength;
+ }
+
+ current_buffer++;
+ }
if (recvLength == -1)
{
@@ -499,11 +542,6 @@ enet_socket_receive (ENetSocket socket,
return -1;
}
-#ifdef HAS_MSGHDR_FLAGS
- if (msgHdr.msg_flags & MSG_TRUNC)
- return -1;
-#endif
-
if (address != NULL)
{
address -> host = (enet_uint32) sin.sin_addr.s_addr;
diff --git a/include/enet/unix.h b/include/enet/unix.h
index a59e340..d3f833c 100644
--- a/include/enet/unix.h
+++ b/include/enet/unix.h
@@ -1,10 +1,11 @@
-/**
+/**
@file unix.h
@brief ENet Unix header
*/
#ifndef __ENET_UNIX_H__
#define __ENET_UNIX_H__
+#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
@@ -42,6 +43,6 @@ typedef fd_set ENetSocketSet;
#define ENET_SOCKETSET_ADD(sockset, socket) FD_SET (socket, & (sockset))
#define ENET_SOCKETSET_REMOVE(sockset, socket) FD_CLR (socket, & (sockset))
#define ENET_SOCKETSET_CHECK(sockset, socket) FD_ISSET (socket, & (sockset))
-
+
#endif /* __ENET_UNIX_H__ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment