-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkClient.cs
386 lines (307 loc) · 12.1 KB
/
NetworkClient.cs
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace GreatKingdomClient
{
public class NetworkClient
{
const int BUFFER_MAX_SIZE = 2048*2*2*2;
private TcpClient tc;
private NetworkStream stream;
private Queue<GameRoomInfo> updateQueue = new Queue<GameRoomInfo>();
bool isInGame;
private Thread readPacketThread;
private object readLock = new object();
public NetworkClient(string serverIP, int serverPort)
{
tc = new TcpClient(serverIP, serverPort);
stream = tc.GetStream();
}
~NetworkClient()
{
tc.Close();
}
//업데이트 정보가 있는가
public bool IsUpdate()
{
bool isUpdate;
if (!isInGame)
{
Console.WriteLine("Not good in this context");
return false;
}
lock (readLock)
{
isUpdate = updateQueue.Count > 0;
}
return isUpdate;
}
//업데이트 정보 얻기
public GameRoomInfo GetUpdateData()
{
GameRoomInfo info;
if (!isInGame)
{
Console.WriteLine("Not good in this context");
throw new Exception("Isnt in game");
}
lock (readLock)
{
info = updateQueue.Dequeue();
}
return info;
}
public int SetGameID(int id)
{
if (isInGame)
{
Console.WriteLine("Not good in this context");
throw new Exception("In game");
}
return SendSetClntIDPacket(id);
}
public RoomDatas GetGameRooms(int offset)
{
if (isInGame)
{
Console.WriteLine("Not good in this context");
throw new Exception("In game");
}
return SendGetGameRoomPacket(offset);
}
public int CreateGameRoom(int roomID)
{
if (isInGame)
{
Console.WriteLine("Not good in this context");
throw new Exception("In game");
}
return SendCreateGameRoomPacket(roomID);
}
public int JoinGameRoom(int roomID)
{
if (isInGame)
{
Console.WriteLine("Not good in this context");
throw new Exception("In game");
}
if (SendJoinGameRoomPacket(roomID) < 0)
return -1;
StartReadThread();
return 0;
}
public void UpdateGameRoom(GameRoomInfo roomInfo)
{
if (!isInGame)
{
Console.WriteLine("Not good in this context");
throw new Exception("Isnt in game");
}
SendUpdateGameRoomPacket(roomInfo);
}
public int OutGameRoom(int roomID)
{
if (!isInGame)
{
Console.WriteLine("Not good in this context");
throw new Exception("Isnt in game");
}
EndReadThread();
if (SendOutGameRoomPacket(roomID) < 0)
return -1;
return 0;
}
public int SendClose()
{
return SendClosePacket();
}
private void StartReadThread()
{
readPacketThread = new Thread(new ThreadStart(ReadPacket));
isInGame = true;
readPacketThread.Start();
}
private void EndReadThread()
{
isInGame = false;
//readPacketThread.Join(); // 테스트용 주석 // 유니티에서는 스레드가 안사라지는 느낌?? //
}
//Todo: 룸 업데이트 정보 패킷일 경우 큐에 집어넣는 기능
private void ReadPacket()
{
while (isInGame)
{
byte[] buffer = new byte[BUFFER_MAX_SIZE];
ReturnRoomData retData;
if (ReadData(buffer, out retData) < 0)
{
Console.WriteLine("read data failed!");
stream.Flush();
return;
}
lock(readLock)
{
updateQueue.Enqueue(retData.roomInfo);
}
Console.WriteLine("새 데이터가 들어왔습니다.");
}
Console.WriteLine("읽기 스레드 종료");
}
private int SendSetClntIDPacket(int clnt_id)
{
byte[] buffer = new byte[BUFFER_MAX_SIZE];
int packetLen;
BasePacketHeader header = new BasePacketHeader(PacketDefine.TCP_PACKET_START_CODE, Convert.ToUInt32(Marshal.SizeOf(typeof(SetClntIDData))), PacketDefine.HANDLER_USER, PacketDefine.HANDLER_USER_SETCLNTID, 0, 0);
BasePacketTrailer trailer = new BasePacketTrailer(PacketDefine.TCP_PACKET_END_CODE);
SetClntIDData data = new SetClntIDData(clnt_id);
ReturnUserData retData;
packetLen = PacketUtility.MakePacket(buffer, header, data, trailer);
stream.Write(buffer, 0, packetLen);
Console.WriteLine("packetLen: " + packetLen);
if (ReadData(buffer, out retData) < 0)
return -1;
if (retData.isSuccess == 0)
return -1;
return 0;
}
private int SendClosePacket()
{
byte[] buffer = new byte[BUFFER_MAX_SIZE];
int packetLen;
BasePacketHeader header = new BasePacketHeader(PacketDefine.TCP_PACKET_START_CODE, 0, PacketDefine.HANDLER_USER, PacketDefine.HANDLER_USER_CLOSE, 0, 0);
BasePacketTrailer trailer = new BasePacketTrailer(PacketDefine.TCP_PACKET_END_CODE);
ReturnUserData retData;
packetLen = PacketUtility.MakePacket(buffer, header, trailer);
stream.Write(buffer, 0, packetLen);
if (ReadData(buffer, out retData) < 0)
return -1;
if (retData.isSuccess == 0)
return -1;
return 0;
}
private RoomDatas SendGetGameRoomPacket(int offset)
{
byte[] buffer = new byte[BUFFER_MAX_SIZE];
int packetLen;
BasePacketHeader header = new BasePacketHeader(PacketDefine.TCP_PACKET_START_CODE, Convert.ToUInt32(Marshal.SizeOf(typeof(GetRoomData))), PacketDefine.HANDLER_GAMEROOM, PacketDefine.HANDLER_GAMEROOM_GET, 0, 0);
BasePacketTrailer trailer = new BasePacketTrailer(PacketDefine.TCP_PACKET_END_CODE);
GetRoomData data = new GetRoomData(offset);
RoomDatas retData;
packetLen = PacketUtility.MakePacket(buffer, header, data, trailer);
stream.Write(buffer, 0, packetLen);
if (ReadData(buffer, out retData) < 0)
return retData;
return retData;
}
private int SendCreateGameRoomPacket(int roomID)
{
byte[] buffer = new byte[BUFFER_MAX_SIZE];
int packetLen;
BasePacketHeader header = new BasePacketHeader(PacketDefine.TCP_PACKET_START_CODE, Convert.ToUInt32(Marshal.SizeOf(typeof(CreateRoomData))), PacketDefine.HANDLER_GAMEROOM, PacketDefine.HANDLER_GAMEROOM_CREATE, 0, 0);
BasePacketTrailer trailer = new BasePacketTrailer(PacketDefine.TCP_PACKET_END_CODE);
CreateRoomData data = new CreateRoomData(roomID);
ReturnRoomData retData;
packetLen = PacketUtility.MakePacket(buffer, header, data, trailer);
stream.Write(buffer, 0, packetLen);
if (ReadData(buffer, out retData) < 0)
return -1;
if (retData.isSuccess == 0)
return -1;
return 0;
}
private int SendJoinGameRoomPacket(int roomID)
{
byte[] buffer = new byte[BUFFER_MAX_SIZE];
int packetLen;
BasePacketHeader header = new BasePacketHeader(PacketDefine.TCP_PACKET_START_CODE, Convert.ToUInt32(Marshal.SizeOf(typeof(JoinRoomData))), PacketDefine.HANDLER_GAMEROOM, PacketDefine.HANDLER_GAMEROOM_JOIN, 0, 0);
BasePacketTrailer trailer = new BasePacketTrailer(PacketDefine.TCP_PACKET_END_CODE);
JoinRoomData data = new JoinRoomData(roomID);
ReturnRoomData retData;
packetLen = PacketUtility.MakePacket(buffer, header, data, trailer);
stream.Write(buffer, 0, packetLen);
if (ReadData(buffer, out retData) < 0)
{
Console.WriteLine("ReadDataError()");
return -1;
}
if (retData.isSuccess == 0)
{
Console.WriteLine("isSuccess is 0");
return -1;
}
return 0;
}
private int SendOutGameRoomPacket(int roomID)
{
byte[] buffer = new byte[BUFFER_MAX_SIZE];
int packetLen;
BasePacketHeader header = new BasePacketHeader(PacketDefine.TCP_PACKET_START_CODE, Convert.ToUInt32(Marshal.SizeOf(typeof(OutRoomData))), PacketDefine.HANDLER_GAMEROOM, PacketDefine.HANDLER_GAMEROOM_OUT, 0, 0);
BasePacketTrailer trailer = new BasePacketTrailer(PacketDefine.TCP_PACKET_END_CODE);
OutRoomData data = new OutRoomData(roomID);
ReturnRoomData retData;
packetLen = PacketUtility.MakePacket(buffer, header, data, trailer);
stream.Write(buffer, 0, packetLen);
/*
if (ReadData(buffer, out retData) < 0)
return -1;
if (retData.isSuccess == 0)
return -1;
*/
return 0;
}
private void SendUpdateGameRoomPacket(GameRoomInfo updateInfo)
{
byte[] buffer = new byte[BUFFER_MAX_SIZE];
int packetLen;
BasePacketHeader header = new BasePacketHeader(PacketDefine.TCP_PACKET_START_CODE, Convert.ToUInt32(Marshal.SizeOf(typeof(UpdateRoomData))), PacketDefine.HANDLER_GAMEROOM, PacketDefine.HANDLER_GAMEROOM_UPDATE, 0, 0);
BasePacketTrailer trailer = new BasePacketTrailer(PacketDefine.TCP_PACKET_END_CODE);
UpdateRoomData data = new UpdateRoomData(updateInfo);
packetLen = PacketUtility.MakePacket(buffer, header, data, trailer);
stream.Write(buffer, 0, packetLen);
}
private int ReadData<T>(byte[] buffer, out T data)
{
BasePacketHeader header;
BasePacketTrailer trailer;
//Todo: 링버퍼 구현하면 바꾸기
int packetSize = Marshal.SizeOf(typeof(BasePacketHeader)) + Marshal.SizeOf(typeof(T)) + Marshal.SizeOf(typeof(BasePacketTrailer));
int readLen = 0;
int offset = 0;
for (int i = 0; i < 10; i++)
{
if ((readLen += stream.Read(buffer, readLen, packetSize - readLen)) >= packetSize)
break;
if (i == 9)
{
Console.WriteLine("try Read Over");
data = default(T);
return -1;
}
}
offset += PacketUtility.ByteToObject(buffer, offset, out header);
offset += PacketUtility.ByteToObject(buffer, offset, out data);
offset += PacketUtility.ByteToObject(buffer, offset, out trailer);
if ((CheckHeader(header) < 0) || (CheckTrailer(trailer) < 0))
return -1;
return 0;
}
private int CheckHeader(BasePacketHeader header)
{
if (header.startCode != PacketDefine.TCP_PACKET_START_CODE)
return -1;
return 0;
}
private int CheckTrailer(BasePacketTrailer trailer)
{
if (trailer.endCode != PacketDefine.TCP_PACKET_END_CODE)
return -1;
return 0;
}
}
}