-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocketNetwork.cs
217 lines (175 loc) · 5.42 KB
/
SocketNetwork.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
using System;
using System.Collections.Concurrent;
using System.Windows.Forms;
using Common;
namespace GameClient;
public partial class mainForm
{
ClientSimpleTcp Network = new ClientSimpleTcp();
System.Windows.Forms.Timer dispatcherUITimer = new();
System.Threading.Thread NetworkReadThread = null;
System.Threading.Thread NetworkSendThread = null;
bool IsNetworkThreadRunning = false;
bool IsBackGroundProcessRunning = false;
PacketBufferManager PacketBuffer = new PacketBufferManager();
ConcurrentQueue<byte[]> RecvPacketQueue = new ConcurrentQueue<byte[]>();
ConcurrentQueue<byte[]> SendPacketQueue = new ConcurrentQueue<byte[]>();
void InitSocketNetwork()
{
IsNetworkThreadRunning = true;
NetworkReadThread = new System.Threading.Thread(this.NetworkReadProcess);
NetworkReadThread.Start();
NetworkSendThread = new System.Threading.Thread(this.NetworkSendProcess);
NetworkSendThread.Start();
IsBackGroundProcessRunning = true;
dispatcherUITimer.Tick += new EventHandler(NetworkProcess);
dispatcherUITimer.Interval = 100;
dispatcherUITimer.Start();
}
void ConnectGameServer(string ip, int port)
{
if (Network.Connect(ip, port))
{
labelStatus.Text = string.Format("{0}. 서버에 접속 중", DateTime.Now);
// btnDisconnect.Enabled = true;
DevLog.Write($"서버에 접속 중", LOG_LEVEL.INFO);
}
else
{
labelStatus.Text = string.Format("{0}. 서버에 접속 실패", DateTime.Now);
}
PacketBuffer.Clear();
}
void CloseNetwork()
{
IsNetworkThreadRunning = false;
Network.Close();
NetworkReadThread.Join();
NetworkSendThread.Join();
IsBackGroundProcessRunning = false;
}
void NetworkReadProcess()
{
while (IsNetworkThreadRunning)
{
if (Network.IsConnected() == false)
{
System.Threading.Thread.Sleep(1);
continue;
}
var recvData = Network.Receive();
if (recvData != null)
{
System.Console.WriteLine($"받은 데이터: {recvData.Item1}");
PacketBuffer.Write(recvData.Item2, 0, recvData.Item1);
while (true)
{
var data = PacketBuffer.Read();
if (data == null)
{
break;
}
RecvPacketQueue.Enqueue(data);
}
//DevLog.Write($"받은 데이터: {recvData.Item2}", LOG_LEVEL.INFO);
}
else
{
Network.Close();
SetDisconnectd();
DevLog.Write("서버와 접속 종료 !!!", LOG_LEVEL.INFO);
}
}
}
void NetworkSendProcess()
{
while (IsNetworkThreadRunning)
{
System.Threading.Thread.Sleep(1);
if (Network.IsConnected() == false)
{
continue;
}
if (SendPacketQueue.TryDequeue(out var packet))
{
Network.Send(packet);
}
}
}
void NetworkProcess(object sender, EventArgs e)
{
ProcessLog();
try
{
byte[] packet = null;
if (RecvPacketQueue.TryDequeue(out packet))
{
ParsingPacket(packet);
}
}
catch (Exception ex)
{
MessageBox.Show(string.Format("NetworkProcess. error:{0}", ex.Message));
}
}
public void SetDisconnectd()
{
// if (btnConnect.Enabled == false)
// {
// btnConnect.Enabled = true;
// btnDisconnect.Enabled = false;
// }
while (true)
{
if (SendPacketQueue.TryDequeue(out var temp) == false)
{
break;
}
}
listBoxRoomChatMsg.Items.Clear();
listBoxRoomUserList.Items.Clear();
EndGame();
_isMatching = false;
_isMatchingSuccess = false;
labelStatus.Text = "서버 접속이 끊어짐";
Network.Close();
}
void PostSendPacket<T>(PacketType packetType, T packet) where T : IMessage
{
if (Network.IsConnected() == false)
{
DevLog.Write("서버 연결이 되어 있지 않습니다", LOG_LEVEL.ERROR);
return;
}
byte[] bytes = PacketSerialized(packet, packetType);
SendPacketQueue.Enqueue(bytes);
}
private void ProcessLog()
{
// 너무 이 작업만 할 수 없으므로 일정 작업 이상을 하면 일단 패스한다.
int logWorkCount = 0;
while (IsBackGroundProcessRunning)
{
System.Threading.Thread.Sleep(1);
string msg;
if (DevLog.GetLog(out msg))
{
++logWorkCount;
if (listBoxLog.Items.Count > 512)
{
listBoxLog.Items.Clear();
}
listBoxLog.Items.Add(msg);
listBoxLog.SelectedIndex = listBoxLog.Items.Count - 1;
}
else
{
break;
}
if (logWorkCount > 8)
{
break;
}
}
}
}