-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetManager.cs
339 lines (252 loc) · 8.35 KB
/
NetManager.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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine.UI;
using System;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class NetManager : NetworkManager
{
//get the instance
private static NetManager _instance;
public static NetManager GetInstance
{
get
{
if (_instance == null)
{
//_instance = new NetManager();
_instance = FindObjectOfType<NetManager>();
}
return _instance;
}
}
//start of the game, should use start() not awake()
private void Start()
{
_instance = this;
autoCreatePlayer = false;
//register
NetworkServer.RegisterHandler(MsgType.Highest + 1, OnMsgArrive);
InitialRegist();
//TryHost();
}
bool hasConnected = false;
void InitialRegist()
{
onClientConnect += ((NetworkConnection conn) => waitToConnect.Add(new Pear(conn)));
RegistNet("connected it", (object ob) =>
{
if (hasConnected) return;
myNetId = (int)ob;
SendToServer("RemoveWait", ob);
if (onConnectToServer_Stable != null)
onConnectToServer_Stable.Invoke((int)ob);
});
//pear
RegistNet("RemoveWait", (object ob) =>
{
Pear p = waitToConnect.Find(x => x.conn.connectionId == (int)ob);
if (p == null)
return;
pears.Add(p);
waitToConnect.Remove(p);
});
}
public int myNetId = -1;
#region tools
public string GetIP()
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); ;
foreach (NetworkInterface adapter in adapters)
{
if (adapter.Supports(NetworkInterfaceComponent.IPv4))
{
UnicastIPAddressInformationCollection uniCast = adapter.GetIPProperties().UnicastAddresses;
if (uniCast.Count > 0)
{
foreach (UnicastIPAddressInformation uni in uniCast)
{
if (uni.Address.AddressFamily == AddressFamily.InterNetwork)
{
return uni.Address.ToString();
}
}
}
}
}
return "no ip…";
}
public bool logOut = true;
void log(object ob) { if (logOut) Debug.Log(ob); }
#endregion
#region syncTools
public static byte[] SerializeObj(object obj)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, obj);
byte[] data = ms.ToArray();
return data;
}
public static T DeSerializeObj<T>(byte[] data)
{
MemoryStream ms1 = new MemoryStream(data);
BinaryFormatter bf1 = new BinaryFormatter();
T type = (T)bf1.Deserialize(ms1);
return type;
}
#endregion
public static bool host = false;
public void TryConnectIp(string ip)
{
networkAddress = ip;
networkPort = 7777;
hasConnected = false;
StartClient();
host = false;
}
public void TryConnectIp(Text ip)
{
TryConnectIp(ip.text);
}
public void TryHost()
{
waitToConnect.Clear();
networkPort = 7777;
StartHost();
host = true;
NetworkServer.RegisterHandler(MsgType.Highest + 1, OnMsgArrive);
}
int cd = 0;
List<Pear> waitToConnect = new List<Pear>();
private void FixedUpdate()
{
//wait actions
if (waitToConnect.Count > 0)
{
if (cd < 1)
{
foreach (var p in waitToConnect)
{
SendToPear(p, "connected it", p.conn.connectionId);
}
cd = 5;
}
else { cd--; }
}
}
//user is connecting
public override void OnServerConnect(NetworkConnection conn)
{
base.OnServerConnect(conn);
log("player in:" + conn.address + " id:" + conn.connectionId);
if(onClientConnect!=null)
onClientConnect.Invoke(conn);
}
public delegate void Del_connect_id(int id);
public delegate void Del_connect(NetworkConnection conn);
public event Del_connect_id onConnectToServer_Stable;//connect to server
public event Del_connect onConnectToServer;//connecting to server
public event Del_connect onClientConnect;//player is connected
public event Del_connect onDisconnect;//disconnected from server
public event Del_connect onPlayerLeft;//disconnected
public delegate void MsgCallBack(string type, object msg);
// called when connected to a server
public override void OnClientConnect(NetworkConnection conn)
{
base.OnClientConnect(conn);
log("connected to server:" + conn.address + " NetId:" + conn.connectionId);
if(onConnectToServer!=null)
onConnectToServer.Invoke(conn);
client.RegisterHandler(MsgType.Highest + 1, OnMsgArrive);
}
public List<Pear> pears = new List<Pear>();
public class Pear
{
public Pear(NetworkConnection _conn) { conn = _conn; }
public NetworkConnection conn;
}
/// <summary>
/// sentoserver
/// </summary>
/// <param name="_type"></param>
/// <param name="_msg"></param>
/// send to server
public void SendToServer(string _type, object _msg = null)
{
client.Send(MsgType.Highest + 1,
new MsgBase { bytes = SerializeObj(new MsgPacket { msg = _msg, type = _type }) }
);
}
//server send to all players
public void SendToAllPears(string _type, object _msg = null)
{
if (logOut) { Debug.Log("server to ALL" + " type:" + _type + " msg: " + _msg); }
foreach (var p in pears)
{
SendToPear(p, _type, _msg);
}
}
public void SendToPear(Pear p, string _type, object _msg)
{
SendToNetId(p.conn.connectionId, _type, _msg);
}
public void SendToNetId(int netId, string _type, object _msg)
{
log("server to " + netId + " type:" + _type + " msg: " + _msg);
NetworkServer.SendToClient(netId, MsgType.Highest + 1,
new MsgBase { bytes = SerializeObj(new MsgPacket { msg = _msg, type = _type }) }
);
}
//convert msg
void OnMsgArrive(NetworkMessage msg)
{
MsgPacket packet = DeSerializeObj<MsgPacket>(msg.ReadMessage<MsgBase>().bytes);
ReceiveMsg(packet.type, packet.msg);
}
[Serializable]
class MsgBase : MessageBase
{
public byte[] bytes;
}
[Serializable]
class MsgPacket
{
public string type;
public object msg;
}
public delegate void d_netCallBack(object ob);
Dictionary<string, d_netCallBack> netCallBacks = new Dictionary<string, d_netCallBack>();
void ReceiveMsg(string type, object msg)
{
log("get:" + type + " info:" + msg);
d_netCallBack callback;
if (netCallBacks.TryGetValue(type, out callback))
{ callback(msg); }
else { log("nofound:" + type); }
}
public void RegistNet(string type, d_netCallBack callback)
{
netCallBacks.Add(type, callback);
}
//player disconnect
public override void OnClientDisconnect(NetworkConnection conn)
{
StopClient();
if(onDisconnect!=null)
onDisconnect.Invoke(conn);
}
//player leave
public override void OnServerDisconnect(NetworkConnection conn)
{
NetworkServer.DestroyPlayersForConnection(conn);
if(onPlayerLeft!=null)
onPlayerLeft.Invoke(conn);
waitToConnect.Remove(waitToConnect.Find(x => x.conn.connectionId == conn.connectionId));
log(conn.connectionId + " disconnected");
}
}