Skip to content

Commit

Permalink
simplify BIO interface to support Network.framework
Browse files Browse the repository at this point in the history
  • Loading branch information
mman committed Mar 11, 2024
1 parent befe5e2 commit 8c9b491
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 25 deletions.
1 change: 0 additions & 1 deletion host.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/

ENetHostBIO ENET_SOCKET_BIO = {
.context = NULL,
.enet_socket_create = &enet_socket_create,
.enet_socket_bind = &enet_socket_bind,
.enet_socket_send = &enet_socket_send,
Expand Down
12 changes: 6 additions & 6 deletions include/enet/enet.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,11 @@ typedef int (ENET_CALLBACK * ENetInterceptCallback) (struct _ENetHost * host, st
*/
typedef struct _ENetHistBIO
{
void * context;
ENetSocket (*enet_socket_create) (ENetSocketType);
int (*enet_socket_bind) (ENetSocket, const ENetAddress *);
int (*enet_socket_get_address) (ENetSocket, ENetAddress *);
int (*enet_socket_send) (void *, ENetSocket, const ENetAddress *, const ENetBuffer *, size_t, const ENetAddress *, void *);
int (*enet_socket_receive) (void *, ENetSocket, ENetAddress *, ENetBuffer *, size_t, ENetAddress *, void **);
int (*enet_socket_send) (/*ENetPeer*/ void *, ENetSocket, const ENetAddress *, const ENetBuffer *, size_t, const ENetAddress *);
int (*enet_socket_receive) (/*ENetHost*/ void *, ENetSocket, ENetAddress *, ENetBuffer *, size_t, ENetAddress *);
int (*enet_socket_wait) (ENetSocket, enet_uint32 *, enet_uint32);
int (*enet_socket_set_option) (ENetSocket, ENetSocketOption, int);
int (*enet_socket_get_option) (ENetSocket, ENetSocketOption, int *);
Expand Down Expand Up @@ -432,7 +431,8 @@ typedef struct _ENetHost
size_t maximumPacketSize; /**< the maximum allowable packet size that may be sent or received on a peer */
size_t maximumWaitingData; /**< the maximum aggregate amount of buffer space a peer may use waiting for packets to be delivered */
ENetHostBIO bio;
void * connection;
void * data; /**< Application private data, may be freely modified */
void * connection; /**< Application private data, may be freely modified */
} ENetHost;

/**
Expand Down Expand Up @@ -535,8 +535,8 @@ ENET_API int enet_socket_get_address (ENetSocket, ENetAddress *);
ENET_API int enet_socket_listen (ENetSocket, int);
ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress *);
ENET_API int enet_socket_connect (ENetSocket, const ENetAddress *);
ENET_API int enet_socket_send (void *, ENetSocket, const ENetAddress *, const ENetBuffer *, size_t, const ENetAddress *, void *);
ENET_API int enet_socket_receive (void *, ENetSocket, ENetAddress *, ENetBuffer *, size_t, ENetAddress *, void **);
ENET_API int enet_socket_send (/*ENetPeer*/ void *, ENetSocket, const ENetAddress *, const ENetBuffer *, size_t, const ENetAddress *);
ENET_API int enet_socket_receive (/*ENetHost*/ void *, ENetSocket, ENetAddress *, ENetBuffer *, size_t, ENetAddress *);
ENET_API int enet_socket_wait (ENetSocket, enet_uint32 *, enet_uint32);
ENET_API int enet_socket_set_option (ENetSocket, ENetSocketOption, int);
ENET_API int enet_socket_get_option (ENetSocket, ENetSocketOption, int *);
Expand Down
32 changes: 26 additions & 6 deletions protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,15 @@ enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)

command -> header.reliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> header.reliableSequenceNumber);

#if ENET_DEBUG
printf("<<< host: %p, port %d, peer: %p, state: %d, channel: %d, command: 0x%x, .header.reliableSequenceNumber: %d\n",
host, host -> address.port,
peer, peer != NULL ? peer->state : -1,
command -> header.channelID,
command -> header.command,
command -> header.reliableSequenceNumber);
#endif

switch (commandNumber)
{
case ENET_PROTOCOL_COMMAND_ACKNOWLEDGE:
Expand Down Expand Up @@ -1246,13 +1255,12 @@ enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event)
buffer.data = host -> packetData [0];
buffer.dataLength = sizeof (host -> packetData [0]);

receivedLength = (*host -> bio.enet_socket_receive) (host -> bio.context,
receivedLength = (*host -> bio.enet_socket_receive) (host,
host -> socket,
& host -> peerAddress,
& buffer,
1,
& host -> localAddress,
&host -> connection);
& host -> localAddress);

if (receivedLength == -2)
continue;
Expand Down Expand Up @@ -1743,13 +1751,25 @@ enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch
(*host -> bio.enet_socket_set_option) (host -> socket, ENET_SOCKOPT_QOS, 0);
}

sentLength = (*host -> bio.enet_socket_send) (host -> bio.context,
#if ENET_DEBUG
printf(">>> host: %p, port: %d START %d commands\n", host, host -> address.port, host -> commandCount);
for (int i = 0; i < host -> commandCount; i ++) {
printf(">>> host: %p, port: %d, peer: %p, state: %d, channel: %d, command: 0x%x, .header.reliableSequenceNumber: %d\n",
host, host -> address.port,
currentPeer, currentPeer->state,
host -> commands[i].header.channelID,
host -> commands[i].header.command,
host -> commands[i].header.reliableSequenceNumber);
}
printf(">>> host: %p, port: %d END\n", host, host -> address.port);
#endif

sentLength = (*host -> bio.enet_socket_send) (currentPeer,
host -> socket,
& currentPeer -> peerAddress,
host -> buffers,
host -> bufferCount,
& currentPeer -> localAddress,
currentPeer -> connection);
& currentPeer -> localAddress);

enet_protocol_remove_sent_unreliable_commands (currentPeer, & sentUnreliableCommands);

Expand Down
11 changes: 5 additions & 6 deletions unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,11 @@ enet_socket_destroy (ENetSocket socket)
}

int
enet_socket_send (void * context, ENetSocket socket,
enet_socket_send (void * enetPeer, ENetSocket socket,
const ENetAddress * destinationAddress,
const ENetBuffer * buffers,
size_t bufferCount,
const ENetAddress * sourceAddress,
void * connection)
const ENetAddress * sourceAddress)
{
struct msghdr msgHdr;
struct sockaddr_in6 sin;
Expand Down Expand Up @@ -567,12 +566,12 @@ enet_socket_send (void * context, ENetSocket socket,
}

int
enet_socket_receive (void * context, ENetSocket socket,
enet_socket_receive (void * host,
ENetSocket socket,
ENetAddress * sourceAddress,
ENetBuffer * buffers,
size_t bufferCount,
ENetAddress * destinationAddress,
void ** connection)
ENetAddress * destinationAddress)
{
struct msghdr msgHdr;
struct sockaddr_in6 sin;
Expand Down
11 changes: 5 additions & 6 deletions win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,11 @@ enet_socket_destroy (ENetSocket socket)
}

int
enet_socket_send (void * context, ENetSocket socket,
enet_socket_send (void * enetPeer, ENetSocket socket,
const ENetAddress * address,
const ENetBuffer * buffers,
size_t bufferCount,
const ENetAddress * sourceAddress,
void * connection)
const ENetAddress * sourceAddress)
{
struct sockaddr_in6 sin;
DWORD sentLength = 0;
Expand Down Expand Up @@ -404,12 +403,12 @@ enet_socket_send (void * context, ENetSocket socket,
}

int
enet_socket_receive (void * context, ENetSocket socket,
enet_socket_receive (void * host,
ENetSocket socket,
ENetAddress * address,
ENetBuffer * buffers,
size_t bufferCount,
const ENetAddress * destinationAddress,
void ** connection)
const ENetAddress * destinationAddress)
{
INT sinLength = sizeof (struct sockaddr_in6);
DWORD flags = 0,
Expand Down

0 comments on commit 8c9b491

Please sign in to comment.