Skip to content

Commit c1e7639

Browse files
authored
Merge pull request #1 from chiuan/修复例子在osx运行,fix结束回调2次,V6环境靠谱设置
修复例子在osx运行,fix结束回调2次,v6环境靠谱设置
2 parents 16c4949 + c817ed8 commit c1e7639

File tree

3 files changed

+55
-40
lines changed

3 files changed

+55
-40
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*/*.meta
2+
*.meta
3+
.DS_Store

Network.cs

+40-37
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using UnityObject = UnityEngine.Object;
1111

1212
namespace TinyNet
13-
{
13+
{
1414
#region 发送和接收缓冲区
1515
/// <summary>
1616
/// 填充数据包
@@ -65,8 +65,8 @@ public static BufferSegment Make(byte onebyte)
6565
};
6666
return seg;
6767
}
68-
}
69-
68+
}
69+
7070
/// <summary>
7171
/// 复用缓冲区
7272
/// </summary>
@@ -192,9 +192,9 @@ public void Reset()
192192
_offset = 0;
193193
_length = 0;
194194
}
195-
}
195+
}
196196
#endregion
197-
197+
198198
#region 解析网络请求
199199
/// <summary>
200200
/// 基础请求类
@@ -239,8 +239,8 @@ public virtual void Reset()
239239
{
240240
buffer.Reset();
241241
}
242-
}
243-
242+
}
243+
244244
/// <summary>
245245
/// 无ID分组
246246
/// </summary>
@@ -259,8 +259,8 @@ protected virtual void Handle(NetHandler handler, T t)
259259
}
260260

261261
public static event Action<NetHandler, T> DefaultHandler;
262-
}
263-
262+
}
263+
264264
/// <summary>
265265
/// 按ID分组
266266
/// </summary>
@@ -296,9 +296,9 @@ public static Dictionary<TKey, Action<NetHandler, T>> Handlers
296296
}
297297
}
298298
public static event Action<NetHandler, TKey, T> DefaultHandler;
299-
}
299+
}
300300
#endregion
301-
301+
302302
#region 设置相关
303303
public class Settings
304304
{
@@ -322,9 +322,9 @@ public Settings()
322322
capacity = 2 << 24;
323323
worklimit = 4;
324324
}
325-
}
325+
}
326326
#endregion
327-
327+
328328
/// <summary>
329329
/// 网络连接抽象句柄
330330
/// </summary>
@@ -340,8 +340,8 @@ public abstract class NetHandler
340340
public abstract IPEndPoint Local { get; }
341341
public abstract short TTL { get; set; }
342342
public abstract bool NoDelay { get; set; }
343-
}
344-
343+
}
344+
345345
/// <summary>
346346
/// 网络管理全局接口
347347
/// </summary>
@@ -355,16 +355,16 @@ public abstract class NetManager : IDisposable
355355
private readonly Dictionary<int, Control> listens = new Dictionary<int, Control>();
356356
private readonly Dictionary<NetHandlerImpl, bool> sockets = new Dictionary<NetHandlerImpl, bool>();
357357

358-
private static readonly Dictionary<NetManager, bool> managers = new Dictionary<NetManager, bool>();
359-
358+
private static readonly Dictionary<NetManager, bool> managers = new Dictionary<NetManager, bool>();
359+
360360
#region 内部使用的类
361361
private class Control
362362
{
363363
public bool running;
364364
public Action<NetHandler> action;
365-
}
365+
}
366366
#endregion
367-
367+
368368
protected NetManager(Settings settings)
369369
{
370370
this.settings = settings;
@@ -383,7 +383,7 @@ public void Listen(int port, Action<NetHandler> callback)
383383
{
384384
if (!running)
385385
throw new ObjectDisposedException(ToString());
386-
new Thread(delegate()
386+
new Thread(delegate ()
387387
{
388388
Thread.CurrentThread.Name = "Network Server";
389389
TcpListener server = new TcpListener(IPAddress.Any, port);
@@ -533,8 +533,8 @@ public void Dispose()
533533
public void Close()
534534
{
535535
Dispose();
536-
}
537-
536+
}
537+
538538
#region 连接
539539
private void ConnectImpl(string host, int port, int timeout, Action<NetHandler, Exception> callback)
540540
{
@@ -551,12 +551,12 @@ private void ConnectImpl(string host, int port, int timeout, Action<NetHandler,
551551
IPAddress[] iplist = Dns.EndGetHostAddresses(ar);
552552
if (timeout > 0)
553553
{
554-
AddressFamily family = AddressFamily.InterNetworkV6;
554+
AddressFamily family = AddressFamily.InterNetwork;
555555
for (int i = 0; i < iplist.Length; ++i)
556556
{
557-
if (iplist[i].AddressFamily == AddressFamily.InterNetwork)
557+
if (iplist[i].AddressFamily == AddressFamily.InterNetworkV6)
558558
{
559-
family = AddressFamily.InterNetwork;
559+
family = AddressFamily.InterNetworkV6;
560560
break;
561561
}
562562
}
@@ -604,9 +604,9 @@ private void ConnectImpl(string host, int port, int timeout, Action<NetHandler,
604604
callback(null, e);
605605
}
606606
}, null);
607-
}
607+
}
608608
#endregion
609-
609+
610610
private void SendQueue(NetHandlerImpl handler)
611611
{
612612
lock (workqueue)
@@ -625,7 +625,7 @@ private void SendQueue(NetHandlerImpl handler)
625625
Interlocked.Decrement(ref workcount);
626626
return;
627627
}
628-
new Thread(delegate()
628+
new Thread(delegate ()
629629
{
630630
Thread.CurrentThread.Name = "Network Write";
631631
while (running)
@@ -671,8 +671,8 @@ private void SendQueue(NetHandlerImpl handler)
671671
}
672672
}
673673
}).Start();
674-
}
675-
674+
}
675+
676676
#region 网络连接的具体类
677677
private class NetHandlerImpl : NetHandler
678678
{
@@ -702,7 +702,7 @@ public void OnConnected()
702702
{
703703
manager.sockets.Add(this, true);
704704
}
705-
new Thread(delegate()
705+
new Thread(delegate ()
706706
{
707707
Thread.CurrentThread.Name = "Network Read";
708708
byte[] bytes = new byte[64 * 1024];
@@ -805,7 +805,10 @@ public override void Close()
805805
Loop.Run(() =>
806806
{
807807
if (manager.settings.events.close != null)
808+
{
808809
manager.settings.events.close(this);
810+
manager.settings.events.close = null;
811+
}
809812
});
810813
}
811814
}
@@ -945,7 +948,7 @@ private void OnReceive(byte[] bytes, int length)
945948
size = length - offset;
946949
Request old = request;
947950
request = manager.NewRequest();
948-
Loop.Run(delegate()
951+
Loop.Run(delegate ()
949952
{
950953
if (manager.settings.events.request != null)
951954
{
@@ -961,15 +964,15 @@ private void OnReceive(byte[] bytes, int length)
961964
Socket.Close();
962965
if (manager.settings.events.close != null)
963966
{
964-
Loop.Run(delegate()
967+
Loop.Run(delegate ()
965968
{
966969
if (manager.settings.events.close != null)
967970
manager.settings.events.close(this);
968971
});
969972
}
970973
if (manager.settings.events.exception != null)
971974
{
972-
Loop.Run(delegate()
975+
Loop.Run(delegate ()
973976
{
974977
if (manager.settings.events.exception != null)
975978
manager.settings.events.exception(this, e);
@@ -980,9 +983,9 @@ private void OnReceive(byte[] bytes, int length)
980983
}
981984

982985
protected abstract Request NewRequest();
983-
protected abstract void ReleaseRequest(Request request);
986+
protected abstract void ReleaseRequest(Request request);
984987
#endregion
985-
988+
986989
#region 主循环调用
987990
private static class Loop
988991
{
@@ -1112,7 +1115,7 @@ void OnApplicationQuit()
11121115
}
11131116
}
11141117
}
1115-
}
1118+
}
11161119
#endregion
11171120
}
11181121

UnitTest/TestCase.cs

+12-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ public static NetManager NetMgr
2323
{
2424
Debug.Log(req.Value);
2525
}
26+
},
27+
read = (handler, len) => {
28+
Debug.Log("read " + len);
29+
},
30+
close = (handler) => {
31+
Debug.Log("handler close");
32+
},
33+
write = (handler, len) => {
34+
Debug.Log("write " + len);;
2635
}
2736
}
2837
};
@@ -34,7 +43,7 @@ public static NetManager NetMgr
3443

3544
public static void StartListen(int port)
3645
{
37-
NetMgr.Listen(port, delegate(NetHandler socket)
46+
NetMgr.Listen(port, delegate (NetHandler socket)
3847
{
3948
Debug.Log("New Connection");
4049
});
@@ -60,8 +69,8 @@ public static void StartConnect(string ipport)
6069
public static void Main(string[] args)
6170
{
6271
NetManager.Initialize();
63-
StartListen(12306);
64-
StartConnect("localhost:12306");
72+
StartListen(12306);
73+
StartConnect("127.0.0.1:12306");
6574
}
6675
}
6776
}

0 commit comments

Comments
 (0)