-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPServer.cs
More file actions
245 lines (175 loc) · 6.39 KB
/
TCPServer.cs
File metadata and controls
245 lines (175 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/************************************************************************************
SOURCE FILE: TCPServer.cs
PROGRAM: game
FUNCTIONS: TCPServer()
Init()
AcceptConnection()
Recv()
Send()
CloseClientSocket()
CloseListenSocket()
DATE: Mar. 14, 2018
REVISIONS:
DESIGNER: Delan Elliot, Wilson Hu, Jeremy Lee, Jeff Chou
PROGRAMMER: Delan Elliot, Wilson Hu
NOTES:
This class represents a TCP server object that handles
TCP connections. Its methods are C# wrappers that call the
networking library's functions.
**********************************************************************************/
using System;
namespace Networking
{
public unsafe class TCPServer
{
private IntPtr tcpServer;
private Int32 serverSocket;
/************************************************************************************
FUNCTION: TCPServer
DATE: Mar. 14, 2018
REVISIONS:
DESIGNER: Delan Elliot
PROGRAMMER: Delan Elliot
INTERFACE: TCPServer()
NOTES:
This constructor calls the library's TCPServer_CreateServer
function, which creates a corresponding C++ TCPServer object.
**********************************************************************************/
public TCPServer()
{
tcpServer = ServerLibrary.TCPServer_CreateServer();
}
/************************************************************************************
FUNCTION: Init
DATE: Mar. 14, 2018
REVISIONS:
DESIGNER: Delan Elliot
PROGRAMMER: Delan Elliot
INTERFACE: public Int32 Init(ushort port)
ushort port: port number for the listening socket
RETURNS: Returns an Int32 indicating success or failure
- <=0 on failure, >0 listen socket descriptor on success
NOTES:
This function is the C# wrapper around the library's TCPServer::initializeSocket
function.
**********************************************************************************/
public Int32 Init(ushort port, ushort timeout)
{
serverSocket = ServerLibrary.TCPServer_initServer(tcpServer, port, timeout);
return serverSocket;
}
/************************************************************************************
FUNCTION: AcceptConnection
DATE: Mar. 14, 2018
REVISIONS:
DESIGNER: Delan Elliot
PROGRAMMER: Delan Elliot
INTERFACE: public Int32 AcceptConnection(ref EndPoint ep)
ref EndPoint ep: EndPoint struct reference that holds the newly
connected client's IP & port.
RETURNS: Returns an Int32 value depending on success or failure
- <= 0 on failure, >0 socket descriptor (client) on success
NOTES:
This function is the C# wrapper around the library's TCPServer::acceptConnection
function.
**********************************************************************************/
public Int32 AcceptConnection(ref EndPoint ep)
{
fixed(EndPoint* p = &ep)
{
return ServerLibrary.TCPServer_acceptConnection(tcpServer, p);
}
}
/************************************************************************************
FUNCTION: Recv
DATE: Mar. 14, 2018
REVISIONS:
DESIGNER: Delan Elliot
PROGRAMMER: Delan Elliot
INTERFACE: public Int32 Recv(Int32 socket, byte[] buffer, Int32 len)
Int32 socket: client socket descriptor
byte[] buffer: receive buffer
Int32 len: number of bytes to receive
RETURNS: Returns the number of bytes received
- len on success, <len on failure
NOTES:
This function is the C# wrapper for the library's TCPServer::receiveBytes
function.
It reads len bytes from the input socket descriptor into the input buffer.
**********************************************************************************/
public Int32 Recv(Int32 socket, byte[] buffer, Int32 len)
{
Int32 length;
fixed (byte* tmpBuf = buffer)
{
UInt32 bufLen = Convert.ToUInt32(len);
length = ServerLibrary.TCPServer_recvBytes(tcpServer, socket, new IntPtr(tmpBuf), bufLen);
return length;
}
}
/************************************************************************************
FUNCTION: Send
DATE: Mar. 14, 2018
REVISIONS:
DESIGNER: Delan Elliot
PROGRAMMER: Delan Elliot
INTERFACE: public Int32 Send(Int32 socket, byte[] buffer, Int32 len)
Int32 socket: client socket descriptor
byte[] buffer: buffer to send
Int32 len: number of bytes to send
RETURNS: Returns the number of bytes sent.
- len on success, <len on failure
NOTES:
This function is the C# wrapper for the library's TCPServer::sendBytes
function.
It sends len bytes from the buffer to the input socket descriptor.
**********************************************************************************/
public Int32 Send(Int32 socket, byte[] buffer, Int32 len)
{
fixed( byte* tmpBuf = buffer)
{
UInt32 bufLen = Convert.ToUInt32 (len);
Int32 ret = ServerLibrary.TCPServer_sendBytes(tcpServer, socket, new IntPtr(tmpBuf), bufLen);
return ret;
}
}
/************************************************************************************
FUNCTION: CloseClientSocket
DATE: Mar. 14, 2018
REVISIONS:
DESIGNER: Wilson Hu
PROGRAMMER: Wilson Hu
INTERFACE: public Int32 CloseClientSocket(Int32 sockfd)
Int32 sockfd: client socket descriptor
RETURNS: Returns Int32 value depending on success or failure
- -1 on failure, 0 on success.
NOTES:
This function is the C# wrapper for the library's TCPServer::closeClientSocket
function.
It closes the input socket descriptor (clientSocket)
**********************************************************************************/
public Int32 CloseClientSocket(Int32 sockfd)
{
return ServerLibrary.TCPServer_closeClientSocket(sockfd);
}
/************************************************************************************
FUNCTION: CloseListenSocket
DATE: Mar. 14, 2018
REVISIONS:
DESIGNER: Wilson Hu
PROGRAMMER: Wilson Hu
INTERFACE: public Int32 CloseListenSocket(Int32 sockfd)
RETURNS: Returns Int32 value depending on success or failure
- -1 on failure, 0 on success.
NOTES:
This function is the C# wrapper for the library's TCPServer::closeListenSocket
function.
It closes the input socket descriptor (sockfd).
**********************************************************************************/
public Int32 CloseListenSocket(Int32 sockfd)
{
Int32 result = ServerLibrary.TCPServer_closeListenSocket(serverSocket);
return result;
}
}
}