-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyUDP.cs
More file actions
279 lines (234 loc) · 6.97 KB
/
myUDP.cs
File metadata and controls
279 lines (234 loc) · 6.97 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
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine;
public class myUDP : MonoBehaviour
{
public List<ClientData> ref_clientList;
public ClientData myData = null;
public List<RecvData> recvList = new List<RecvData>();
public UdpClient severSock;
public int myPort;
public string myName = "null";
void Update()
{
if (recvList != null && recvList.Count > 0)
{
RecvData recvData = recvList[0];
if (recvList[0].m_todo != null)
{
recvData.m_todo(recvData);
recvList.RemoveAt(0);
}
}
}
public void InitRECV(UDP_Manager udpM, int port = 0)
{
ref_clientList = udpM.clientList;
try
{
if (severSock == null)
{
SocketManager.setPort(myName, port);
severSock = new UdpClient(port);
myPort = ((IPEndPoint)severSock.Client.LocalEndPoint).Port;
SocketManager.setListen(severSock, ("JOIN"));
Debug.Log("Listen Start - " + myName + " at " + SocketManager.GetLocalIPAddress() + " / " + myPort);
severSock.BeginReceive(new AsyncCallback(RECV_CALLBACK), null);
}
}
catch (SocketException e)
{
Debug.Log(e.Message);
}
}
public void InitRECV()
{
try
{
if (severSock == null)
{
SocketManager.setPort(myName, myPort);
severSock = new UdpClient(0);
severSock.BeginReceive(new AsyncCallback(RECV_CALLBACK), null);
SocketManager.setListen(severSock, ("SYNC"));
myPort = ((IPEndPoint)severSock.Client.LocalEndPoint).Port;
Debug.Log("Listen Start - " + myName + " at " + SocketManager.GetLocalIPAddress() + " / " + myPort);
}
}
catch (SocketException e)
{
Debug.Log(e.Message);
}
}
public void RECV_CALLBACK(IAsyncResult ar)
{
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, myPort);
byte[] m_ReceivedBytes;
if (severSock == null)
{
return;
}
m_ReceivedBytes = severSock.EndReceive(ar, ref ipEndPoint);
if (m_ReceivedBytes.Length > 1)
{
Debug.Log("RECV - " + myName + " / " + m_ReceivedBytes.Length);
RecvData newData = new RecvData(m_ReceivedBytes, ipEndPoint);
workFunc(newData);
if (newData.m_todo != null)
recvList.Add(newData);
else
Debug.Log("trash Data");
}
severSock.BeginReceive(new AsyncCallback(RECV_CALLBACK), null);
}
public void SEND_CALLBACK(IAsyncResult ar)
{
Debug.Log(myName + " called SEND_CALLBACK");
}
public void SEND_ALL_CLIENT(byte[] sendData)
{
for (int i = 0; i < ref_clientList.Count; i++)
{
ref_clientList[i].sendMSG(myName, sendData, new AsyncCallback(SEND_CALLBACK));
}
}
public virtual void workFunc(RecvData recvData)
{
//Debug.Log("time to work UDP");
//recvList.Clear();
}
}
[Serializable]
public class RecvData
{
public static int index = 1;
public int m_OP;
public UdpClient m_sender;
public IPEndPoint m_adr;
public byte[] m_buffer;
public int port;
public string ip;
public int process = 0;
public TODO m_todo;
public RecvData(byte[] i_data, IPEndPoint i_adr)
{
m_adr = i_adr;
port = m_adr.Port;
ip = m_adr.Address + "";
m_buffer = i_data;
m_OP = RECV_INT();
}
public void sendMSG(byte[] msg, AsyncCallback func)
{
if (m_sender == null)
{
m_sender = new UdpClient(); SocketManager.setSocket(m_sender, ("JOIN"));
}
byte[] packets = msg;
m_sender.BeginSend(packets, packets.Length, m_adr, new AsyncCallback(func), this);
}
public void sendMSG(byte[] msg)
{
if (m_sender == null)
{
m_sender = new UdpClient(); SocketManager.setSocket(m_sender, ("JOIN"));
}
Debug.Log(port);
byte[] packets = msg;
m_sender.Send(packets, packets.Length, m_adr);
}
public void sendMSG(byte[] msg, String str)
{
if (m_sender == null)
{
m_sender = new UdpClient(); SocketManager.setSocket(m_sender, str);
}
Debug.Log(port);
byte[] packets = msg;
m_sender.Send(packets, packets.Length, m_adr);
}
public int RECV_INT()
{
process += 4;
return BitConverter.ToInt32(m_buffer, process-4);
}
public float RECV_FLOAT()
{
process += 4;
return BitConverter.ToSingle(m_buffer, process - 4);
}
public long RECV_IPADDR()
{
process += 8;
return BitConverter.ToInt64(m_buffer, process - 8);
}
public string RECV_STR()
{
int temp = process; process = m_buffer.Length;
return Encoding.Default.GetString(m_buffer ,temp , m_buffer.Length - temp);
}
public delegate void TODO(RecvData recvData);
}
public static class SendData
{
public static int SEND_INT(this byte[] m_buffer,int process, int input)
{
int size = 4;
int space = 0;
space = m_buffer.Length - process;
//space 버린단 마인드, 필요 여백공간 출력
space = size - space;
if (space > 0)
{
return space;
}
Array.Copy(BitConverter.GetBytes(input), 0, m_buffer, process, size);
return 0;
}
public static int SEND_FLOAT(this byte[] m_buffer, int process, float input)
{
int size = 4;
int space = 0;
space = m_buffer.Length - process;
//space 버린단 마인드, 필요 여백공간 출력
space = size - space;
if (space > 0)
{
return space;
}
Array.Copy(BitConverter.GetBytes(input), 0, m_buffer, process, size);
return 0;
}
public static int SEND_IPADDR(this byte[] m_buffer, int process, IPAddress input)
{
byte[] byteIP = input.GetAddressBytes();
int size = byteIP.Length;
int space = 0;
space = m_buffer.Length - process;
//space 버린단 마인드, 필요 여백공간 출력
space = size - space;
if (space > 0)
{
return space;
}
Array.Copy(byteIP, 0, m_buffer, process, size);
return 0;
}
public static int SEND_STR(this byte[] m_buffer, int process, string input)
{
int size = input.Length;
int space = 0;
space = m_buffer.Length - process;
//space 버린단 마인드, 필요 여백공간 출력
space = size - space;
if (space > 0)
{
return space;
}
Array.Copy(Encoding.ASCII.GetBytes(input), 0, m_buffer, process, size);
return 0;
}
}