KChannel.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Runtime.InteropServices;
  8. namespace ET
  9. {
  10. public struct KcpWaitPacket
  11. {
  12. public long ActorId;
  13. public MemoryStream MemoryStream;
  14. }
  15. public class KChannel : AChannel
  16. {
  17. public KService Service;
  18. // 保存所有的channel
  19. public static readonly Dictionary<uint, KChannel> kChannels = new Dictionary<uint, KChannel>();
  20. public static readonly ConcurrentDictionary<long, ulong> idLocalRemoteConn = new ConcurrentDictionary<long, ulong>();
  21. private Socket socket;
  22. private IntPtr kcp;
  23. private readonly Queue<KcpWaitPacket> sendBuffer = new Queue<KcpWaitPacket>();
  24. private uint lastRecvTime;
  25. public readonly uint CreateTime;
  26. public uint LocalConn { get; set; }
  27. public uint RemoteConn { get; set; }
  28. private readonly byte[] sendCache = new byte[1024 * 1024];
  29. public bool IsConnected { get; private set; }
  30. public string RealAddress { get; set; }
  31. private void InitKcp()
  32. {
  33. switch (this.Service.ServiceType)
  34. {
  35. case ServiceType.Inner:
  36. Kcp.KcpNodelay(kcp, 1, 10, 2, 1);
  37. Kcp.KcpWndsize(kcp, 1024 * 100, 1024 * 100);
  38. Kcp.KcpSetmtu(kcp, 1400); // 默认1400
  39. Kcp.KcpSetminrto(kcp, 10);
  40. break;
  41. case ServiceType.Outer:
  42. Kcp.KcpNodelay(kcp, 1, 10, 2, 1);
  43. Kcp.KcpWndsize(kcp, 128, 128);
  44. Kcp.KcpSetmtu(kcp, 470);
  45. Kcp.KcpSetminrto(kcp, 10);
  46. break;
  47. }
  48. }
  49. // connect
  50. public KChannel(long id, uint localConn, Socket socket, IPEndPoint remoteEndPoint, KService kService)
  51. {
  52. this.LocalConn = localConn;
  53. if (kChannels.ContainsKey(this.LocalConn))
  54. {
  55. throw new Exception($"channel create error: {this.LocalConn} {remoteEndPoint} {this.ChannelType}");
  56. }
  57. this.Id = id;
  58. this.ChannelType = ChannelType.Connect;
  59. Log.Info($"channel create: {this.Id} {this.LocalConn} {remoteEndPoint} {this.ChannelType}");
  60. this.Service = kService;
  61. this.RemoteAddress = remoteEndPoint;
  62. this.socket = socket;
  63. this.kcp = Kcp.KcpCreate(this.RemoteConn, (IntPtr) this.LocalConn);
  64. kChannels.Add(this.LocalConn, this);
  65. this.lastRecvTime = kService.TimeNow;
  66. this.CreateTime = kService.TimeNow;
  67. this.Connect();
  68. }
  69. // accept
  70. public KChannel(long id, uint localConn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService)
  71. {
  72. if (kChannels.ContainsKey(this.LocalConn))
  73. {
  74. throw new Exception($"channel create error: {localConn} {remoteEndPoint} {this.ChannelType}");
  75. }
  76. this.Id = id;
  77. this.ChannelType = ChannelType.Accept;
  78. Log.Info($"channel create: {this.Id} {localConn} {remoteConn} {remoteEndPoint} {this.ChannelType}");
  79. this.Service = kService;
  80. this.LocalConn = localConn;
  81. this.RemoteConn = remoteConn;
  82. this.RemoteAddress = remoteEndPoint;
  83. this.socket = socket;
  84. this.kcp = Kcp.KcpCreate(this.RemoteConn, (IntPtr) localConn);
  85. kChannels.Add(this.LocalConn, this);
  86. this.lastRecvTime = kService.TimeNow;
  87. this.CreateTime = kService.TimeNow;
  88. this.InitKcp();
  89. }
  90. #region 网络线程
  91. public override void Dispose()
  92. {
  93. if (this.IsDisposed)
  94. {
  95. return;
  96. }
  97. uint localConn = this.LocalConn;
  98. uint remoteConn = this.RemoteConn;
  99. Log.Info($"channel dispose: {this.Id} {localConn} {remoteConn}");
  100. kChannels.Remove(localConn);
  101. idLocalRemoteConn.TryRemove(this.Id, out ulong _);
  102. long id = this.Id;
  103. this.Id = 0;
  104. this.Service.Remove(id);
  105. try
  106. {
  107. //this.Service.Disconnect(localConn, remoteConn, this.Error, this.RemoteAddress, 3);
  108. }
  109. catch (Exception e)
  110. {
  111. Log.Error(e);
  112. }
  113. if (this.kcp != IntPtr.Zero)
  114. {
  115. Kcp.KcpRelease(this.kcp);
  116. this.kcp = IntPtr.Zero;
  117. }
  118. this.socket = null;
  119. }
  120. public void HandleConnnect()
  121. {
  122. // 如果连接上了就不用处理了
  123. if (this.IsConnected)
  124. {
  125. return;
  126. }
  127. this.kcp = Kcp.KcpCreate(this.RemoteConn, new IntPtr(this.LocalConn));
  128. this.InitKcp();
  129. ulong localRmoteConn = ((ulong) this.RemoteConn << 32) | this.LocalConn;
  130. idLocalRemoteConn.TryAdd(this.Id, localRmoteConn);
  131. Log.Info($"channel connected: {this.Id} {this.LocalConn} {this.RemoteConn} {this.RemoteAddress}");
  132. this.IsConnected = true;
  133. this.lastRecvTime = this.Service.TimeNow;
  134. while (true)
  135. {
  136. if (this.sendBuffer.Count <= 0)
  137. {
  138. break;
  139. }
  140. KcpWaitPacket buffer = this.sendBuffer.Dequeue();
  141. this.KcpSend(buffer);
  142. }
  143. }
  144. /// <summary>
  145. /// 发送请求连接消息
  146. /// </summary>
  147. private void Connect()
  148. {
  149. try
  150. {
  151. uint timeNow = this.Service.TimeNow;
  152. this.lastRecvTime = timeNow;
  153. byte[] buffer = sendCache;
  154. buffer.WriteTo(0, KcpProtocalType.SYN);
  155. buffer.WriteTo(1, this.LocalConn);
  156. buffer.WriteTo(5, this.RemoteConn);
  157. this.socket.SendTo(buffer, 0, 9, SocketFlags.None, this.RemoteAddress);
  158. Log.Info($"kchannel connect {this.Id} {this.LocalConn} {this.RemoteConn} {this.RealAddress} {this.socket.LocalEndPoint}");
  159. // 200毫秒后再次update发送connect请求
  160. this.Service.AddToUpdateNextTime(timeNow + 300, this.Id);
  161. }
  162. catch (Exception e)
  163. {
  164. Log.Error(e);
  165. this.OnError(ErrorCode.ERR_SocketCantSend);
  166. }
  167. }
  168. public void Update()
  169. {
  170. if (this.IsDisposed)
  171. {
  172. return;
  173. }
  174. uint timeNow = this.Service.TimeNow;
  175. // 如果还没连接上,发送连接请求
  176. if (!this.IsConnected)
  177. {
  178. // 20秒没连接上则报错
  179. if (timeNow - this.CreateTime > 10 * 1000)
  180. {
  181. Log.Error($"kChannel connect timeout: {this.Id} {this.RemoteConn} {timeNow} {this.CreateTime} {this.ChannelType} {this.RemoteAddress}");
  182. this.OnError(ErrorCode.ERR_KcpConnectTimeout);
  183. return;
  184. }
  185. switch (ChannelType)
  186. {
  187. case ChannelType.Connect:
  188. this.Connect();
  189. break;
  190. }
  191. return;
  192. }
  193. try
  194. {
  195. Kcp.KcpUpdate(this.kcp, timeNow);
  196. }
  197. catch (Exception e)
  198. {
  199. Log.Error(e);
  200. this.OnError(ErrorCode.ERR_SocketError);
  201. return;
  202. }
  203. if (this.kcp != IntPtr.Zero)
  204. {
  205. uint nextUpdateTime = Kcp.KcpCheck(this.kcp, timeNow);
  206. this.Service.AddToUpdateNextTime(nextUpdateTime, this.Id);
  207. }
  208. }
  209. public void HandleRecv(byte[] date, int offset, int length)
  210. {
  211. if (this.IsDisposed)
  212. {
  213. return;
  214. }
  215. this.IsConnected = true;
  216. Kcp.KcpInput(this.kcp, date, offset, length);
  217. this.Service.AddToUpdateNextTime(0, this.Id);
  218. while (true)
  219. {
  220. if (this.IsDisposed)
  221. {
  222. break;
  223. }
  224. int n = Kcp.KcpPeeksize(this.kcp);
  225. if (n < 0)
  226. {
  227. break;
  228. }
  229. if (n == 0)
  230. {
  231. this.OnError((int)SocketError.NetworkReset);
  232. break;
  233. }
  234. MemoryStream ms = MessageSerializeHelper.GetStream(n);
  235. ms.SetLength(n);
  236. ms.Seek(0, SeekOrigin.Begin);
  237. byte[] buffer = ms.GetBuffer();
  238. int count = Kcp.KcpRecv(this.kcp, buffer, n);
  239. if (n != count)
  240. {
  241. break;
  242. }
  243. switch (this.Service.ServiceType)
  244. {
  245. case ServiceType.Inner:
  246. ms.Seek(Packet.ActorIdLength + Packet.OpcodeLength, SeekOrigin.Begin);
  247. break;
  248. case ServiceType.Outer:
  249. ms.Seek(Packet.OpcodeLength, SeekOrigin.Begin);
  250. break;
  251. }
  252. this.lastRecvTime = this.Service.TimeNow;
  253. this.OnRead(ms);
  254. }
  255. }
  256. public void Output(IntPtr bytes, int count)
  257. {
  258. if (this.IsDisposed)
  259. {
  260. return;
  261. }
  262. try
  263. {
  264. // 没连接上 kcp不往外发消息, 其实本来没连接上不会调用update,这里只是做一层保护
  265. if (!this.IsConnected)
  266. {
  267. return;
  268. }
  269. if (count == 0)
  270. {
  271. Log.Error($"output 0");
  272. return;
  273. }
  274. byte[] buffer = this.sendCache;
  275. buffer.WriteTo(0, KcpProtocalType.MSG);
  276. // 每个消息头部写下该channel的id;
  277. buffer.WriteTo(1, this.LocalConn);
  278. Marshal.Copy(bytes, buffer, 5, count);
  279. this.socket.SendTo(buffer, 0, count + 5, SocketFlags.None, this.RemoteAddress);
  280. }
  281. catch (Exception e)
  282. {
  283. Log.Error(e);
  284. this.OnError(ErrorCode.ERR_SocketCantSend);
  285. }
  286. }
  287. private void KcpSend(KcpWaitPacket kcpWaitPacket)
  288. {
  289. if (this.IsDisposed)
  290. {
  291. return;
  292. }
  293. MemoryStream memoryStream = kcpWaitPacket.MemoryStream;
  294. if (this.Service.ServiceType == ServiceType.Inner)
  295. {
  296. memoryStream.GetBuffer().WriteTo(0, kcpWaitPacket.ActorId);
  297. }
  298. int count = (int) (memoryStream.Length - memoryStream.Position);
  299. Kcp.KcpSend(this.kcp, memoryStream.GetBuffer(), (int)memoryStream.Position, count);
  300. this.Service.AddToUpdateNextTime(0, this.Id);
  301. }
  302. public void Send(long actorId, MemoryStream stream)
  303. {
  304. if (this.kcp != IntPtr.Zero)
  305. {
  306. // 检查等待发送的消息,如果超出最大等待大小,应该断开连接
  307. int n = Kcp.KcpWaitsnd(this.kcp);
  308. int maxWaitSize = 0;
  309. switch (this.Service.ServiceType)
  310. {
  311. case ServiceType.Inner:
  312. maxWaitSize = Kcp.InnerMaxWaitSize;
  313. break;
  314. case ServiceType.Outer:
  315. maxWaitSize = Kcp.OuterMaxWaitSize;
  316. break;
  317. default:
  318. throw new ArgumentOutOfRangeException();
  319. }
  320. if (n > maxWaitSize)
  321. {
  322. Log.Error($"kcp wait snd too large: {n}: {this.Id} {this.RemoteConn}");
  323. this.OnError(ErrorCode.ERR_KcpWaitSendSizeTooLarge);
  324. return;
  325. }
  326. }
  327. KcpWaitPacket kcpWaitPacket = new KcpWaitPacket() { ActorId = actorId, MemoryStream = stream };
  328. if (!this.IsConnected)
  329. {
  330. this.sendBuffer.Enqueue(kcpWaitPacket);
  331. return;
  332. }
  333. this.KcpSend(kcpWaitPacket);
  334. }
  335. private void OnRead(MemoryStream memoryStream)
  336. {
  337. this.Service.OnRead(this.Id, memoryStream);
  338. }
  339. public void OnError(int error)
  340. {
  341. long channelId = this.Id;
  342. this.Service.Remove(channelId);
  343. this.Service.OnError(channelId, error);
  344. }
  345. #endregion
  346. }
  347. }