renet 0.1.0-universal-darwin
Sign up to get free protection for your applications and to get access to all the features.
- data/README +81 -0
- data/examples/Console-Server.rb +47 -0
- data/examples/Gosu-Client.rb +71 -0
- data/ext/renet/enet/callbacks.c +47 -0
- data/ext/renet/enet/callbacks.h +27 -0
- data/ext/renet/enet/compress.c +654 -0
- data/ext/renet/enet/enet.h +540 -0
- data/ext/renet/enet/host.c +479 -0
- data/ext/renet/enet/list.c +75 -0
- data/ext/renet/enet/list.h +43 -0
- data/ext/renet/enet/packet.c +157 -0
- data/ext/renet/enet/peer.c +816 -0
- data/ext/renet/enet/protocol.c +1671 -0
- data/ext/renet/enet/protocol.h +196 -0
- data/ext/renet/enet/time.h +18 -0
- data/ext/renet/enet/types.h +13 -0
- data/ext/renet/enet/unix.c +438 -0
- data/ext/renet/enet/unix.h +45 -0
- data/ext/renet/enet/utility.h +12 -0
- data/ext/renet/enet/win32.c +348 -0
- data/ext/renet/enet/win32.h +58 -0
- data/ext/renet/extconf.rb +21 -0
- data/ext/renet/renet.c +69 -0
- data/ext/renet/renet.h +35 -0
- data/ext/renet/renet_connection.c +322 -0
- data/ext/renet/renet_connection.h +58 -0
- data/ext/renet/renet_server.c +280 -0
- data/ext/renet/renet_server.h +58 -0
- data/lib/renet.rb +10 -0
- metadata +76 -0
@@ -0,0 +1,196 @@
|
|
1
|
+
/**
|
2
|
+
@file protocol.h
|
3
|
+
@brief ENet protocol
|
4
|
+
*/
|
5
|
+
#ifndef __ENET_PROTOCOL_H__
|
6
|
+
#define __ENET_PROTOCOL_H__
|
7
|
+
|
8
|
+
#include "enet/types.h"
|
9
|
+
|
10
|
+
enum
|
11
|
+
{
|
12
|
+
ENET_PROTOCOL_MINIMUM_MTU = 576,
|
13
|
+
ENET_PROTOCOL_MAXIMUM_MTU = 4096,
|
14
|
+
ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS = 32,
|
15
|
+
ENET_PROTOCOL_MINIMUM_WINDOW_SIZE = 4096,
|
16
|
+
ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE = 32768,
|
17
|
+
ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT = 1,
|
18
|
+
ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT = 255,
|
19
|
+
ENET_PROTOCOL_MAXIMUM_PEER_ID = 0xFFF
|
20
|
+
};
|
21
|
+
|
22
|
+
typedef enum _ENetProtocolCommand
|
23
|
+
{
|
24
|
+
ENET_PROTOCOL_COMMAND_NONE = 0,
|
25
|
+
ENET_PROTOCOL_COMMAND_ACKNOWLEDGE = 1,
|
26
|
+
ENET_PROTOCOL_COMMAND_CONNECT = 2,
|
27
|
+
ENET_PROTOCOL_COMMAND_VERIFY_CONNECT = 3,
|
28
|
+
ENET_PROTOCOL_COMMAND_DISCONNECT = 4,
|
29
|
+
ENET_PROTOCOL_COMMAND_PING = 5,
|
30
|
+
ENET_PROTOCOL_COMMAND_SEND_RELIABLE = 6,
|
31
|
+
ENET_PROTOCOL_COMMAND_SEND_UNRELIABLE = 7,
|
32
|
+
ENET_PROTOCOL_COMMAND_SEND_FRAGMENT = 8,
|
33
|
+
ENET_PROTOCOL_COMMAND_SEND_UNSEQUENCED = 9,
|
34
|
+
ENET_PROTOCOL_COMMAND_BANDWIDTH_LIMIT = 10,
|
35
|
+
ENET_PROTOCOL_COMMAND_THROTTLE_CONFIGURE = 11,
|
36
|
+
ENET_PROTOCOL_COMMAND_COUNT = 12,
|
37
|
+
|
38
|
+
ENET_PROTOCOL_COMMAND_MASK = 0x0F
|
39
|
+
} ENetProtocolCommand;
|
40
|
+
|
41
|
+
typedef enum _ENetProtocolFlag
|
42
|
+
{
|
43
|
+
ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE = (1 << 7),
|
44
|
+
ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED = (1 << 6),
|
45
|
+
|
46
|
+
ENET_PROTOCOL_HEADER_FLAG_COMPRESSED = (1 << 14),
|
47
|
+
ENET_PROTOCOL_HEADER_FLAG_SENT_TIME = (1 << 15),
|
48
|
+
ENET_PROTOCOL_HEADER_FLAG_MASK = ENET_PROTOCOL_HEADER_FLAG_COMPRESSED | ENET_PROTOCOL_HEADER_FLAG_SENT_TIME,
|
49
|
+
|
50
|
+
ENET_PROTOCOL_HEADER_SESSION_MASK = (3 << 12),
|
51
|
+
ENET_PROTOCOL_HEADER_SESSION_SHIFT = 12
|
52
|
+
} ENetProtocolFlag;
|
53
|
+
|
54
|
+
#ifdef _MSC_VER_
|
55
|
+
#pragma pack(push, 1)
|
56
|
+
#define ENET_PACKED
|
57
|
+
#elif defined(__GNUC__)
|
58
|
+
#define ENET_PACKED __attribute__ ((packed))
|
59
|
+
#else
|
60
|
+
#define ENET_PACKED
|
61
|
+
#endif
|
62
|
+
|
63
|
+
typedef struct _ENetProtocolHeader
|
64
|
+
{
|
65
|
+
enet_uint16 peerID;
|
66
|
+
enet_uint16 sentTime;
|
67
|
+
} ENET_PACKED ENetProtocolHeader;
|
68
|
+
|
69
|
+
typedef struct _ENetProtocolCommandHeader
|
70
|
+
{
|
71
|
+
enet_uint8 command;
|
72
|
+
enet_uint8 channelID;
|
73
|
+
enet_uint16 reliableSequenceNumber;
|
74
|
+
} ENET_PACKED ENetProtocolCommandHeader;
|
75
|
+
|
76
|
+
typedef struct _ENetProtocolAcknowledge
|
77
|
+
{
|
78
|
+
ENetProtocolCommandHeader header;
|
79
|
+
enet_uint16 receivedReliableSequenceNumber;
|
80
|
+
enet_uint16 receivedSentTime;
|
81
|
+
} ENET_PACKED ENetProtocolAcknowledge;
|
82
|
+
|
83
|
+
typedef struct _ENetProtocolConnect
|
84
|
+
{
|
85
|
+
ENetProtocolCommandHeader header;
|
86
|
+
enet_uint16 outgoingPeerID;
|
87
|
+
enet_uint8 incomingSessionID;
|
88
|
+
enet_uint8 outgoingSessionID;
|
89
|
+
enet_uint32 mtu;
|
90
|
+
enet_uint32 windowSize;
|
91
|
+
enet_uint32 channelCount;
|
92
|
+
enet_uint32 incomingBandwidth;
|
93
|
+
enet_uint32 outgoingBandwidth;
|
94
|
+
enet_uint32 packetThrottleInterval;
|
95
|
+
enet_uint32 packetThrottleAcceleration;
|
96
|
+
enet_uint32 packetThrottleDeceleration;
|
97
|
+
enet_uint32 connectID;
|
98
|
+
enet_uint32 data;
|
99
|
+
} ENET_PACKED ENetProtocolConnect;
|
100
|
+
|
101
|
+
typedef struct _ENetProtocolVerifyConnect
|
102
|
+
{
|
103
|
+
ENetProtocolCommandHeader header;
|
104
|
+
enet_uint16 outgoingPeerID;
|
105
|
+
enet_uint8 incomingSessionID;
|
106
|
+
enet_uint8 outgoingSessionID;
|
107
|
+
enet_uint32 mtu;
|
108
|
+
enet_uint32 windowSize;
|
109
|
+
enet_uint32 channelCount;
|
110
|
+
enet_uint32 incomingBandwidth;
|
111
|
+
enet_uint32 outgoingBandwidth;
|
112
|
+
enet_uint32 packetThrottleInterval;
|
113
|
+
enet_uint32 packetThrottleAcceleration;
|
114
|
+
enet_uint32 packetThrottleDeceleration;
|
115
|
+
enet_uint32 connectID;
|
116
|
+
} ENET_PACKED ENetProtocolVerifyConnect;
|
117
|
+
|
118
|
+
typedef struct _ENetProtocolBandwidthLimit
|
119
|
+
{
|
120
|
+
ENetProtocolCommandHeader header;
|
121
|
+
enet_uint32 incomingBandwidth;
|
122
|
+
enet_uint32 outgoingBandwidth;
|
123
|
+
} ENET_PACKED ENetProtocolBandwidthLimit;
|
124
|
+
|
125
|
+
typedef struct _ENetProtocolThrottleConfigure
|
126
|
+
{
|
127
|
+
ENetProtocolCommandHeader header;
|
128
|
+
enet_uint32 packetThrottleInterval;
|
129
|
+
enet_uint32 packetThrottleAcceleration;
|
130
|
+
enet_uint32 packetThrottleDeceleration;
|
131
|
+
} ENET_PACKED ENetProtocolThrottleConfigure;
|
132
|
+
|
133
|
+
typedef struct _ENetProtocolDisconnect
|
134
|
+
{
|
135
|
+
ENetProtocolCommandHeader header;
|
136
|
+
enet_uint32 data;
|
137
|
+
} ENET_PACKED ENetProtocolDisconnect;
|
138
|
+
|
139
|
+
typedef struct _ENetProtocolPing
|
140
|
+
{
|
141
|
+
ENetProtocolCommandHeader header;
|
142
|
+
} ENET_PACKED ENetProtocolPing;
|
143
|
+
|
144
|
+
typedef struct _ENetProtocolSendReliable
|
145
|
+
{
|
146
|
+
ENetProtocolCommandHeader header;
|
147
|
+
enet_uint16 dataLength;
|
148
|
+
} ENET_PACKED ENetProtocolSendReliable;
|
149
|
+
|
150
|
+
typedef struct _ENetProtocolSendUnreliable
|
151
|
+
{
|
152
|
+
ENetProtocolCommandHeader header;
|
153
|
+
enet_uint16 unreliableSequenceNumber;
|
154
|
+
enet_uint16 dataLength;
|
155
|
+
} ENET_PACKED ENetProtocolSendUnreliable;
|
156
|
+
|
157
|
+
typedef struct _ENetProtocolSendUnsequenced
|
158
|
+
{
|
159
|
+
ENetProtocolCommandHeader header;
|
160
|
+
enet_uint16 unsequencedGroup;
|
161
|
+
enet_uint16 dataLength;
|
162
|
+
} ENET_PACKED ENetProtocolSendUnsequenced;
|
163
|
+
|
164
|
+
typedef struct _ENetProtocolSendFragment
|
165
|
+
{
|
166
|
+
ENetProtocolCommandHeader header;
|
167
|
+
enet_uint16 startSequenceNumber;
|
168
|
+
enet_uint16 dataLength;
|
169
|
+
enet_uint32 fragmentCount;
|
170
|
+
enet_uint32 fragmentNumber;
|
171
|
+
enet_uint32 totalLength;
|
172
|
+
enet_uint32 fragmentOffset;
|
173
|
+
} ENET_PACKED ENetProtocolSendFragment;
|
174
|
+
|
175
|
+
typedef union _ENetProtocol
|
176
|
+
{
|
177
|
+
ENetProtocolCommandHeader header;
|
178
|
+
ENetProtocolAcknowledge acknowledge;
|
179
|
+
ENetProtocolConnect connect;
|
180
|
+
ENetProtocolVerifyConnect verifyConnect;
|
181
|
+
ENetProtocolDisconnect disconnect;
|
182
|
+
ENetProtocolPing ping;
|
183
|
+
ENetProtocolSendReliable sendReliable;
|
184
|
+
ENetProtocolSendUnreliable sendUnreliable;
|
185
|
+
ENetProtocolSendUnsequenced sendUnsequenced;
|
186
|
+
ENetProtocolSendFragment sendFragment;
|
187
|
+
ENetProtocolBandwidthLimit bandwidthLimit;
|
188
|
+
ENetProtocolThrottleConfigure throttleConfigure;
|
189
|
+
} ENET_PACKED ENetProtocol;
|
190
|
+
|
191
|
+
#ifdef _MSC_VER_
|
192
|
+
#pragma pack(pop)
|
193
|
+
#endif
|
194
|
+
|
195
|
+
#endif /* __ENET_PROTOCOL_H__ */
|
196
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
/**
|
2
|
+
@file time.h
|
3
|
+
@brief ENet time constants and macros
|
4
|
+
*/
|
5
|
+
#ifndef __ENET_TIME_H__
|
6
|
+
#define __ENET_TIME_H__
|
7
|
+
|
8
|
+
#define ENET_TIME_OVERFLOW 86400000
|
9
|
+
|
10
|
+
#define ENET_TIME_LESS(a, b) ((a) - (b) >= ENET_TIME_OVERFLOW)
|
11
|
+
#define ENET_TIME_GREATER(a, b) ((b) - (a) >= ENET_TIME_OVERFLOW)
|
12
|
+
#define ENET_TIME_LESS_EQUAL(a, b) (! ENET_TIME_GREATER (a, b))
|
13
|
+
#define ENET_TIME_GREATER_EQUAL(a, b) (! ENET_TIME_LESS (a, b))
|
14
|
+
|
15
|
+
#define ENET_TIME_DIFFERENCE(a, b) ((a) - (b) >= ENET_TIME_OVERFLOW ? (b) - (a) : (a) - (b))
|
16
|
+
|
17
|
+
#endif /* __ENET_TIME_H__ */
|
18
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/**
|
2
|
+
@file types.h
|
3
|
+
@brief type definitions for ENet
|
4
|
+
*/
|
5
|
+
#ifndef __ENET_TYPES_H__
|
6
|
+
#define __ENET_TYPES_H__
|
7
|
+
|
8
|
+
typedef unsigned char enet_uint8; /**< unsigned 8-bit type */
|
9
|
+
typedef unsigned short enet_uint16; /**< unsigned 16-bit type */
|
10
|
+
typedef unsigned int enet_uint32; /**< unsigned 32-bit type */
|
11
|
+
|
12
|
+
#endif /* __ENET_TYPES_H__ */
|
13
|
+
|
@@ -0,0 +1,438 @@
|
|
1
|
+
/**
|
2
|
+
@file unix.c
|
3
|
+
@brief ENet Unix system specific functions
|
4
|
+
*/
|
5
|
+
#ifndef WIN32
|
6
|
+
|
7
|
+
#include <sys/types.h>
|
8
|
+
#include <sys/socket.h>
|
9
|
+
#include <sys/ioctl.h>
|
10
|
+
#include <sys/time.h>
|
11
|
+
#include <arpa/inet.h>
|
12
|
+
#include <netdb.h>
|
13
|
+
#include <unistd.h>
|
14
|
+
#include <string.h>
|
15
|
+
#include <errno.h>
|
16
|
+
#include <time.h>
|
17
|
+
|
18
|
+
#define ENET_BUILDING_LIB 1
|
19
|
+
#include "enet/enet.h"
|
20
|
+
|
21
|
+
#ifdef HAS_FCNTL
|
22
|
+
#include <fcntl.h>
|
23
|
+
#endif
|
24
|
+
|
25
|
+
#ifdef __APPLE__
|
26
|
+
#undef HAS_POLL
|
27
|
+
#endif
|
28
|
+
|
29
|
+
#ifdef HAS_POLL
|
30
|
+
#include <sys/poll.h>
|
31
|
+
#endif
|
32
|
+
|
33
|
+
#ifndef HAS_SOCKLEN_T
|
34
|
+
typedef int socklen_t;
|
35
|
+
#endif
|
36
|
+
|
37
|
+
#ifndef MSG_NOSIGNAL
|
38
|
+
#define MSG_NOSIGNAL 0
|
39
|
+
#endif
|
40
|
+
|
41
|
+
static enet_uint32 timeBase = 0;
|
42
|
+
|
43
|
+
int
|
44
|
+
enet_initialize (void)
|
45
|
+
{
|
46
|
+
return 0;
|
47
|
+
}
|
48
|
+
|
49
|
+
void
|
50
|
+
enet_deinitialize (void)
|
51
|
+
{
|
52
|
+
}
|
53
|
+
|
54
|
+
enet_uint32
|
55
|
+
enet_time_get (void)
|
56
|
+
{
|
57
|
+
struct timeval timeVal;
|
58
|
+
|
59
|
+
gettimeofday (& timeVal, NULL);
|
60
|
+
|
61
|
+
return timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - timeBase;
|
62
|
+
}
|
63
|
+
|
64
|
+
void
|
65
|
+
enet_time_set (enet_uint32 newTimeBase)
|
66
|
+
{
|
67
|
+
struct timeval timeVal;
|
68
|
+
|
69
|
+
gettimeofday (& timeVal, NULL);
|
70
|
+
|
71
|
+
timeBase = timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - newTimeBase;
|
72
|
+
}
|
73
|
+
|
74
|
+
int
|
75
|
+
enet_address_set_host (ENetAddress * address, const char * name)
|
76
|
+
{
|
77
|
+
struct hostent * hostEntry = NULL;
|
78
|
+
#ifdef HAS_GETHOSTBYNAME_R
|
79
|
+
struct hostent hostData;
|
80
|
+
char buffer [2048];
|
81
|
+
int errnum;
|
82
|
+
|
83
|
+
#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
84
|
+
gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
|
85
|
+
#else
|
86
|
+
hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum);
|
87
|
+
#endif
|
88
|
+
#else
|
89
|
+
hostEntry = gethostbyname (name);
|
90
|
+
#endif
|
91
|
+
|
92
|
+
if (hostEntry == NULL ||
|
93
|
+
hostEntry -> h_addrtype != AF_INET)
|
94
|
+
{
|
95
|
+
#ifdef HAS_INET_PTON
|
96
|
+
if (! inet_pton (AF_INET, name, & address -> host))
|
97
|
+
#else
|
98
|
+
if (! inet_aton (name, (struct in_addr *) & address -> host))
|
99
|
+
#endif
|
100
|
+
return -1;
|
101
|
+
return 0;
|
102
|
+
}
|
103
|
+
|
104
|
+
address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
|
105
|
+
|
106
|
+
return 0;
|
107
|
+
}
|
108
|
+
|
109
|
+
int
|
110
|
+
enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
|
111
|
+
{
|
112
|
+
#ifdef HAS_INET_NTOP
|
113
|
+
if (inet_ntop (AF_INET, & address -> host, name, nameLength) == NULL)
|
114
|
+
#else
|
115
|
+
char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
|
116
|
+
if (addr != NULL)
|
117
|
+
strncpy (name, addr, nameLength);
|
118
|
+
else
|
119
|
+
#endif
|
120
|
+
return -1;
|
121
|
+
return 0;
|
122
|
+
}
|
123
|
+
|
124
|
+
int
|
125
|
+
enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
|
126
|
+
{
|
127
|
+
struct in_addr in;
|
128
|
+
struct hostent * hostEntry = NULL;
|
129
|
+
#ifdef HAS_GETHOSTBYADDR_R
|
130
|
+
struct hostent hostData;
|
131
|
+
char buffer [2048];
|
132
|
+
int errnum;
|
133
|
+
|
134
|
+
in.s_addr = address -> host;
|
135
|
+
|
136
|
+
#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
137
|
+
gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
|
138
|
+
#else
|
139
|
+
hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum);
|
140
|
+
#endif
|
141
|
+
#else
|
142
|
+
in.s_addr = address -> host;
|
143
|
+
|
144
|
+
hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
|
145
|
+
#endif
|
146
|
+
|
147
|
+
if (hostEntry == NULL)
|
148
|
+
return enet_address_get_host_ip (address, name, nameLength);
|
149
|
+
|
150
|
+
strncpy (name, hostEntry -> h_name, nameLength);
|
151
|
+
|
152
|
+
return 0;
|
153
|
+
}
|
154
|
+
|
155
|
+
int
|
156
|
+
enet_socket_bind (ENetSocket socket, const ENetAddress * address)
|
157
|
+
{
|
158
|
+
struct sockaddr_in sin;
|
159
|
+
|
160
|
+
memset (& sin, 0, sizeof (struct sockaddr_in));
|
161
|
+
|
162
|
+
sin.sin_family = AF_INET;
|
163
|
+
|
164
|
+
if (address != NULL)
|
165
|
+
{
|
166
|
+
sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
|
167
|
+
sin.sin_addr.s_addr = address -> host;
|
168
|
+
}
|
169
|
+
else
|
170
|
+
{
|
171
|
+
sin.sin_port = 0;
|
172
|
+
sin.sin_addr.s_addr = INADDR_ANY;
|
173
|
+
}
|
174
|
+
|
175
|
+
return bind (socket,
|
176
|
+
(struct sockaddr *) & sin,
|
177
|
+
sizeof (struct sockaddr_in));
|
178
|
+
}
|
179
|
+
|
180
|
+
int
|
181
|
+
enet_socket_listen (ENetSocket socket, int backlog)
|
182
|
+
{
|
183
|
+
return listen (socket, backlog < 0 ? SOMAXCONN : backlog);
|
184
|
+
}
|
185
|
+
|
186
|
+
ENetSocket
|
187
|
+
enet_socket_create (ENetSocketType type)
|
188
|
+
{
|
189
|
+
return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
|
190
|
+
}
|
191
|
+
|
192
|
+
int
|
193
|
+
enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
|
194
|
+
{
|
195
|
+
int result = -1;
|
196
|
+
switch (option)
|
197
|
+
{
|
198
|
+
case ENET_SOCKOPT_NONBLOCK:
|
199
|
+
#ifdef HAS_FCNTL
|
200
|
+
result = fcntl (socket, F_SETFL, O_NONBLOCK | fcntl (socket, F_GETFL));
|
201
|
+
#else
|
202
|
+
result = ioctl (socket, FIONBIO, & value);
|
203
|
+
#endif
|
204
|
+
break;
|
205
|
+
|
206
|
+
case ENET_SOCKOPT_BROADCAST:
|
207
|
+
result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int));
|
208
|
+
break;
|
209
|
+
|
210
|
+
case ENET_SOCKOPT_REUSEADDR:
|
211
|
+
result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int));
|
212
|
+
break;
|
213
|
+
|
214
|
+
case ENET_SOCKOPT_RCVBUF:
|
215
|
+
result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int));
|
216
|
+
break;
|
217
|
+
|
218
|
+
case ENET_SOCKOPT_SNDBUF:
|
219
|
+
result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int));
|
220
|
+
break;
|
221
|
+
|
222
|
+
default:
|
223
|
+
break;
|
224
|
+
}
|
225
|
+
return result == -1 ? -1 : 0;
|
226
|
+
}
|
227
|
+
|
228
|
+
int
|
229
|
+
enet_socket_connect (ENetSocket socket, const ENetAddress * address)
|
230
|
+
{
|
231
|
+
struct sockaddr_in sin;
|
232
|
+
|
233
|
+
memset (& sin, 0, sizeof (struct sockaddr_in));
|
234
|
+
|
235
|
+
sin.sin_family = AF_INET;
|
236
|
+
sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
|
237
|
+
sin.sin_addr.s_addr = address -> host;
|
238
|
+
|
239
|
+
return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
|
240
|
+
}
|
241
|
+
|
242
|
+
ENetSocket
|
243
|
+
enet_socket_accept (ENetSocket socket, ENetAddress * address)
|
244
|
+
{
|
245
|
+
int result;
|
246
|
+
struct sockaddr_in sin;
|
247
|
+
socklen_t sinLength = sizeof (struct sockaddr_in);
|
248
|
+
|
249
|
+
result = accept (socket,
|
250
|
+
address != NULL ? (struct sockaddr *) & sin : NULL,
|
251
|
+
address != NULL ? & sinLength : NULL);
|
252
|
+
|
253
|
+
if (result == -1)
|
254
|
+
return ENET_SOCKET_NULL;
|
255
|
+
|
256
|
+
if (address != NULL)
|
257
|
+
{
|
258
|
+
address -> host = (enet_uint32) sin.sin_addr.s_addr;
|
259
|
+
address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
|
260
|
+
}
|
261
|
+
|
262
|
+
return result;
|
263
|
+
}
|
264
|
+
|
265
|
+
void
|
266
|
+
enet_socket_destroy (ENetSocket socket)
|
267
|
+
{
|
268
|
+
close (socket);
|
269
|
+
}
|
270
|
+
|
271
|
+
int
|
272
|
+
enet_socket_send (ENetSocket socket,
|
273
|
+
const ENetAddress * address,
|
274
|
+
const ENetBuffer * buffers,
|
275
|
+
size_t bufferCount)
|
276
|
+
{
|
277
|
+
struct msghdr msgHdr;
|
278
|
+
struct sockaddr_in sin;
|
279
|
+
int sentLength;
|
280
|
+
|
281
|
+
memset (& msgHdr, 0, sizeof (struct msghdr));
|
282
|
+
|
283
|
+
if (address != NULL)
|
284
|
+
{
|
285
|
+
memset (& sin, 0, sizeof (struct sockaddr_in));
|
286
|
+
|
287
|
+
sin.sin_family = AF_INET;
|
288
|
+
sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
|
289
|
+
sin.sin_addr.s_addr = address -> host;
|
290
|
+
|
291
|
+
msgHdr.msg_name = & sin;
|
292
|
+
msgHdr.msg_namelen = sizeof (struct sockaddr_in);
|
293
|
+
}
|
294
|
+
|
295
|
+
msgHdr.msg_iov = (struct iovec *) buffers;
|
296
|
+
msgHdr.msg_iovlen = bufferCount;
|
297
|
+
|
298
|
+
sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL);
|
299
|
+
|
300
|
+
if (sentLength == -1)
|
301
|
+
{
|
302
|
+
if (errno == EWOULDBLOCK)
|
303
|
+
return 0;
|
304
|
+
|
305
|
+
return -1;
|
306
|
+
}
|
307
|
+
|
308
|
+
return sentLength;
|
309
|
+
}
|
310
|
+
|
311
|
+
int
|
312
|
+
enet_socket_receive (ENetSocket socket,
|
313
|
+
ENetAddress * address,
|
314
|
+
ENetBuffer * buffers,
|
315
|
+
size_t bufferCount)
|
316
|
+
{
|
317
|
+
struct msghdr msgHdr;
|
318
|
+
struct sockaddr_in sin;
|
319
|
+
int recvLength;
|
320
|
+
|
321
|
+
memset (& msgHdr, 0, sizeof (struct msghdr));
|
322
|
+
|
323
|
+
if (address != NULL)
|
324
|
+
{
|
325
|
+
msgHdr.msg_name = & sin;
|
326
|
+
msgHdr.msg_namelen = sizeof (struct sockaddr_in);
|
327
|
+
}
|
328
|
+
|
329
|
+
msgHdr.msg_iov = (struct iovec *) buffers;
|
330
|
+
msgHdr.msg_iovlen = bufferCount;
|
331
|
+
|
332
|
+
recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL);
|
333
|
+
|
334
|
+
if (recvLength == -1)
|
335
|
+
{
|
336
|
+
if (errno == EWOULDBLOCK)
|
337
|
+
return 0;
|
338
|
+
|
339
|
+
return -1;
|
340
|
+
}
|
341
|
+
|
342
|
+
#ifdef HAS_MSGHDR_FLAGS
|
343
|
+
if (msgHdr.msg_flags & MSG_TRUNC)
|
344
|
+
return -1;
|
345
|
+
#endif
|
346
|
+
|
347
|
+
if (address != NULL)
|
348
|
+
{
|
349
|
+
address -> host = (enet_uint32) sin.sin_addr.s_addr;
|
350
|
+
address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
|
351
|
+
}
|
352
|
+
|
353
|
+
return recvLength;
|
354
|
+
}
|
355
|
+
|
356
|
+
int
|
357
|
+
enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout)
|
358
|
+
{
|
359
|
+
struct timeval timeVal;
|
360
|
+
|
361
|
+
timeVal.tv_sec = timeout / 1000;
|
362
|
+
timeVal.tv_usec = (timeout % 1000) * 1000;
|
363
|
+
|
364
|
+
return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal);
|
365
|
+
}
|
366
|
+
|
367
|
+
int
|
368
|
+
enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
|
369
|
+
{
|
370
|
+
#ifdef HAS_POLL
|
371
|
+
struct pollfd pollSocket;
|
372
|
+
int pollCount;
|
373
|
+
|
374
|
+
pollSocket.fd = socket;
|
375
|
+
pollSocket.events = 0;
|
376
|
+
|
377
|
+
if (* condition & ENET_SOCKET_WAIT_SEND)
|
378
|
+
pollSocket.events |= POLLOUT;
|
379
|
+
|
380
|
+
if (* condition & ENET_SOCKET_WAIT_RECEIVE)
|
381
|
+
pollSocket.events |= POLLIN;
|
382
|
+
|
383
|
+
pollCount = poll (& pollSocket, 1, timeout);
|
384
|
+
|
385
|
+
if (pollCount < 0)
|
386
|
+
return -1;
|
387
|
+
|
388
|
+
* condition = ENET_SOCKET_WAIT_NONE;
|
389
|
+
|
390
|
+
if (pollCount == 0)
|
391
|
+
return 0;
|
392
|
+
|
393
|
+
if (pollSocket.revents & POLLOUT)
|
394
|
+
* condition |= ENET_SOCKET_WAIT_SEND;
|
395
|
+
|
396
|
+
if (pollSocket.revents & POLLIN)
|
397
|
+
* condition |= ENET_SOCKET_WAIT_RECEIVE;
|
398
|
+
|
399
|
+
return 0;
|
400
|
+
#else
|
401
|
+
fd_set readSet, writeSet;
|
402
|
+
struct timeval timeVal;
|
403
|
+
int selectCount;
|
404
|
+
|
405
|
+
timeVal.tv_sec = timeout / 1000;
|
406
|
+
timeVal.tv_usec = (timeout % 1000) * 1000;
|
407
|
+
|
408
|
+
FD_ZERO (& readSet);
|
409
|
+
FD_ZERO (& writeSet);
|
410
|
+
|
411
|
+
if (* condition & ENET_SOCKET_WAIT_SEND)
|
412
|
+
FD_SET (socket, & writeSet);
|
413
|
+
|
414
|
+
if (* condition & ENET_SOCKET_WAIT_RECEIVE)
|
415
|
+
FD_SET (socket, & readSet);
|
416
|
+
|
417
|
+
selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
|
418
|
+
|
419
|
+
if (selectCount < 0)
|
420
|
+
return -1;
|
421
|
+
|
422
|
+
* condition = ENET_SOCKET_WAIT_NONE;
|
423
|
+
|
424
|
+
if (selectCount == 0)
|
425
|
+
return 0;
|
426
|
+
|
427
|
+
if (FD_ISSET (socket, & writeSet))
|
428
|
+
* condition |= ENET_SOCKET_WAIT_SEND;
|
429
|
+
|
430
|
+
if (FD_ISSET (socket, & readSet))
|
431
|
+
* condition |= ENET_SOCKET_WAIT_RECEIVE;
|
432
|
+
|
433
|
+
return 0;
|
434
|
+
#endif
|
435
|
+
}
|
436
|
+
|
437
|
+
#endif
|
438
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
/**
|
2
|
+
@file unix.h
|
3
|
+
@brief ENet Unix header
|
4
|
+
*/
|
5
|
+
#ifndef __ENET_UNIX_H__
|
6
|
+
#define __ENET_UNIX_H__
|
7
|
+
|
8
|
+
#include <stdlib.h>
|
9
|
+
#include <sys/time.h>
|
10
|
+
#include <sys/types.h>
|
11
|
+
#include <netinet/in.h>
|
12
|
+
#include <unistd.h>
|
13
|
+
|
14
|
+
typedef int ENetSocket;
|
15
|
+
|
16
|
+
enum
|
17
|
+
{
|
18
|
+
ENET_SOCKET_NULL = -1
|
19
|
+
};
|
20
|
+
|
21
|
+
#define ENET_HOST_TO_NET_16(value) (htons (value)) /**< macro that converts host to net byte-order of a 16-bit value */
|
22
|
+
#define ENET_HOST_TO_NET_32(value) (htonl (value)) /**< macro that converts host to net byte-order of a 32-bit value */
|
23
|
+
|
24
|
+
#define ENET_NET_TO_HOST_16(value) (ntohs (value)) /**< macro that converts net to host byte-order of a 16-bit value */
|
25
|
+
#define ENET_NET_TO_HOST_32(value) (ntohl (value)) /**< macro that converts net to host byte-order of a 32-bit value */
|
26
|
+
|
27
|
+
typedef struct
|
28
|
+
{
|
29
|
+
void * data;
|
30
|
+
size_t dataLength;
|
31
|
+
} ENetBuffer;
|
32
|
+
|
33
|
+
#define ENET_CALLBACK
|
34
|
+
|
35
|
+
#define ENET_API extern
|
36
|
+
|
37
|
+
typedef fd_set ENetSocketSet;
|
38
|
+
|
39
|
+
#define ENET_SOCKETSET_EMPTY(sockset) FD_ZERO (& (sockset))
|
40
|
+
#define ENET_SOCKETSET_ADD(sockset, socket) FD_SET (socket, & (sockset))
|
41
|
+
#define ENET_SOCKETSET_REMOVE(sockset, socket) FD_CLEAR (socket, & (sockset))
|
42
|
+
#define ENET_SOCKETSET_CHECK(sockset, socket) FD_ISSET (socket, & (sockset))
|
43
|
+
|
44
|
+
#endif /* __ENET_UNIX_H__ */
|
45
|
+
|