KcpConnection.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. using System;
  2. using System.Diagnostics;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. namespace kcp2k
  6. {
  7. enum KcpState { Connected, Authenticated, Disconnected }
  8. public abstract class KcpConnection
  9. {
  10. protected Socket socket;
  11. protected EndPoint remoteEndPoint;
  12. internal Kcp kcp;
  13. // kcp can have several different states, let's use a state machine
  14. KcpState state = KcpState.Disconnected;
  15. public Action OnAuthenticated;
  16. public Action<ArraySegment<byte>, KcpChannel> OnData;
  17. public Action OnDisconnected;
  18. // If we don't receive anything these many milliseconds
  19. // then consider us disconnected
  20. public const int DEFAULT_TIMEOUT = 10000;
  21. public int timeout = DEFAULT_TIMEOUT;
  22. uint lastReceiveTime;
  23. // internal time.
  24. // StopWatch offers ElapsedMilliSeconds and should be more precise than
  25. // Unity's time.deltaTime over long periods.
  26. readonly Stopwatch refTime = new Stopwatch();
  27. // we need to subtract the channel byte from every MaxMessageSize
  28. // calculation.
  29. // we also need to tell kcp to use MTU-1 to leave space for the byte.
  30. const int CHANNEL_HEADER_SIZE = 1;
  31. // reliable channel (= kcp) MaxMessageSize so the outside knows largest
  32. // allowed message to send. the calculation in Send() is not obvious at
  33. // all, so let's provide the helper here.
  34. //
  35. // kcp does fragmentation, so max message is way larger than MTU.
  36. //
  37. // -> runtime MTU changes are disabled: mss is always MTU_DEF-OVERHEAD
  38. // -> Send() checks if fragment count < rcv_wnd, so we use rcv_wnd - 1.
  39. // NOTE that original kcp has a bug where WND_RCV default is used
  40. // instead of configured rcv_wnd, limiting max message size to 144 KB
  41. // https://github.com/skywind3000/kcp/pull/291
  42. // we fixed this in kcp2k.
  43. // -> we add 1 byte KcpHeader enum to each message, so -1
  44. //
  45. // IMPORTANT: max message is MTU * rcv_wnd, in other words it completely
  46. // fills the receive window! due to head of line blocking,
  47. // all other messages have to wait while a maxed size message
  48. // is being delivered.
  49. // => in other words, DO NOT use max size all the time like
  50. // for batching.
  51. // => sending UNRELIABLE max message size most of the time is
  52. // best for performance (use that one for batching!)
  53. static int ReliableMaxMessageSize_Unconstrained(uint rcv_wnd) => (Kcp.MTU_DEF - Kcp.OVERHEAD - CHANNEL_HEADER_SIZE) * ((int)rcv_wnd - 1) - 1;
  54. // kcp encodes 'frg' as 1 byte.
  55. // max message size can only ever allow up to 255 fragments.
  56. // WND_RCV gives 127 fragments.
  57. // WND_RCV * 2 gives 255 fragments.
  58. // so we can limit max message size by limiting rcv_wnd parameter.
  59. public static int ReliableMaxMessageSize(uint rcv_wnd) =>
  60. ReliableMaxMessageSize_Unconstrained(Math.Min(rcv_wnd, Kcp.FRG_MAX));
  61. // unreliable max message size is simply MTU - channel header size
  62. public const int UnreliableMaxMessageSize = Kcp.MTU_DEF - CHANNEL_HEADER_SIZE;
  63. // buffer to receive kcp's processed messages (avoids allocations).
  64. // IMPORTANT: this is for KCP messages. so it needs to be of size:
  65. // 1 byte header + MaxMessageSize content
  66. byte[] kcpMessageBuffer;// = new byte[1 + ReliableMaxMessageSize];
  67. // send buffer for handing user messages to kcp for processing.
  68. // (avoids allocations).
  69. // IMPORTANT: needs to be of size:
  70. // 1 byte header + MaxMessageSize content
  71. byte[] kcpSendBuffer;// = new byte[1 + ReliableMaxMessageSize];
  72. // raw send buffer is exactly MTU.
  73. byte[] rawSendBuffer = new byte[Kcp.MTU_DEF];
  74. // send a ping occasionally so we don't time out on the other end.
  75. // for example, creating a character in an MMO could easily take a
  76. // minute of no data being sent. which doesn't mean we want to time out.
  77. // same goes for slow paced card games etc.
  78. public const int PING_INTERVAL = 1000;
  79. uint lastPingTime;
  80. // if we send more than kcp can handle, we will get ever growing
  81. // send/recv buffers and queues and minutes of latency.
  82. // => if a connection can't keep up, it should be disconnected instead
  83. // to protect the server under heavy load, and because there is no
  84. // point in growing to gigabytes of memory or minutes of latency!
  85. // => 2k isn't enough. we reach 2k when spawning 4k monsters at once
  86. // easily, but it does recover over time.
  87. // => 10k seems safe.
  88. //
  89. // note: we have a ChokeConnectionAutoDisconnects test for this too!
  90. internal const int QueueDisconnectThreshold = 10000;
  91. // getters for queue and buffer counts, used for debug info
  92. public int SendQueueCount => kcp.snd_queue.Count;
  93. public int ReceiveQueueCount => kcp.rcv_queue.Count;
  94. public int SendBufferCount => kcp.snd_buf.Count;
  95. public int ReceiveBufferCount => kcp.rcv_buf.Count;
  96. // maximum send rate per second can be calculated from kcp parameters
  97. // source: https://translate.google.com/translate?sl=auto&tl=en&u=https://wetest.qq.com/lab/view/391.html
  98. //
  99. // KCP can send/receive a maximum of WND*MTU per interval.
  100. // multiple by 1000ms / interval to get the per-second rate.
  101. //
  102. // example:
  103. // WND(32) * MTU(1400) = 43.75KB
  104. // => 43.75KB * 1000 / INTERVAL(10) = 4375KB/s
  105. //
  106. // returns bytes/second!
  107. public uint MaxSendRate =>
  108. kcp.snd_wnd * kcp.mtu * 1000 / kcp.interval;
  109. public uint MaxReceiveRate =>
  110. kcp.rcv_wnd * kcp.mtu * 1000 / kcp.interval;
  111. // SetupKcp creates and configures a new KCP instance.
  112. // => useful to start from a fresh state every time the client connects
  113. // => NoDelay, interval, wnd size are the most important configurations.
  114. // let's force require the parameters so we don't forget it anywhere.
  115. protected void SetupKcp(bool noDelay, uint interval = Kcp.INTERVAL, int fastResend = 0, bool congestionWindow = true, uint sendWindowSize = Kcp.WND_SND, uint receiveWindowSize = Kcp.WND_RCV, int timeout = DEFAULT_TIMEOUT, uint maxRetransmits = Kcp.DEADLINK)
  116. {
  117. // set up kcp over reliable channel (that's what kcp is for)
  118. kcp = new Kcp(0, RawSendReliable);
  119. // set nodelay.
  120. // note that kcp uses 'nocwnd' internally so we negate the parameter
  121. kcp.SetNoDelay(noDelay ? 1u : 0u, interval, fastResend, !congestionWindow);
  122. kcp.SetWindowSize(sendWindowSize, receiveWindowSize);
  123. // IMPORTANT: high level needs to add 1 channel byte to each raw
  124. // message. so while Kcp.MTU_DEF is perfect, we actually need to
  125. // tell kcp to use MTU-1 so we can still put the header into the
  126. // message afterwards.
  127. kcp.SetMtu(Kcp.MTU_DEF - CHANNEL_HEADER_SIZE);
  128. // set maximum retransmits (aka dead_link)
  129. kcp.dead_link = maxRetransmits;
  130. // create message buffers AFTER window size is set
  131. // see comments on buffer definition for the "+1" part
  132. kcpMessageBuffer = new byte[1 + ReliableMaxMessageSize(receiveWindowSize)];
  133. kcpSendBuffer = new byte[1 + ReliableMaxMessageSize(receiveWindowSize)];
  134. this.timeout = timeout;
  135. state = KcpState.Connected;
  136. refTime.Start();
  137. }
  138. void HandleTimeout(uint time)
  139. {
  140. // note: we are also sending a ping regularly, so timeout should
  141. // only ever happen if the connection is truly gone.
  142. if (time >= lastReceiveTime + timeout)
  143. {
  144. Log.Warning($"KCP: Connection timed out after not receiving any message for {timeout}ms. Disconnecting.");
  145. Disconnect();
  146. }
  147. }
  148. void HandleDeadLink()
  149. {
  150. // kcp has 'dead_link' detection. might as well use it.
  151. if (kcp.state == -1)
  152. {
  153. Log.Warning($"KCP Connection dead_link detected: a message was retransmitted {kcp.dead_link} times without ack. Disconnecting.");
  154. Disconnect();
  155. }
  156. }
  157. // send a ping occasionally in order to not time out on the other end.
  158. void HandlePing(uint time)
  159. {
  160. // enough time elapsed since last ping?
  161. if (time >= lastPingTime + PING_INTERVAL)
  162. {
  163. // ping again and reset time
  164. //Log.Debug("KCP: sending ping...");
  165. SendPing();
  166. lastPingTime = time;
  167. }
  168. }
  169. void HandleChoked()
  170. {
  171. // disconnect connections that can't process the load.
  172. // see QueueSizeDisconnect comments.
  173. // => include all of kcp's buffers and the unreliable queue!
  174. int total = kcp.rcv_queue.Count + kcp.snd_queue.Count +
  175. kcp.rcv_buf.Count + kcp.snd_buf.Count;
  176. if (total >= QueueDisconnectThreshold)
  177. {
  178. Log.Warning($"KCP: disconnecting connection because it can't process data fast enough.\n" +
  179. $"Queue total {total}>{QueueDisconnectThreshold}. rcv_queue={kcp.rcv_queue.Count} snd_queue={kcp.snd_queue.Count} rcv_buf={kcp.rcv_buf.Count} snd_buf={kcp.snd_buf.Count}\n" +
  180. $"* Try to Enable NoDelay, decrease INTERVAL, disable Congestion Window (= enable NOCWND!), increase SEND/RECV WINDOW or compress data.\n" +
  181. $"* Or perhaps the network is simply too slow on our end, or on the other end.\n");
  182. // let's clear all pending sends before disconnting with 'Bye'.
  183. // otherwise a single Flush in Disconnect() won't be enough to
  184. // flush thousands of messages to finally deliver 'Bye'.
  185. // this is just faster and more robust.
  186. kcp.snd_queue.Clear();
  187. Disconnect();
  188. }
  189. }
  190. // reads the next reliable message type & content from kcp.
  191. // -> to avoid buffering, unreliable messages call OnData directly.
  192. bool ReceiveNextReliable(out KcpHeader header, out ArraySegment<byte> message)
  193. {
  194. int msgSize = kcp.PeekSize();
  195. if (msgSize > 0)
  196. {
  197. // only allow receiving up to buffer sized messages.
  198. // otherwise we would get BlockCopy ArgumentException anyway.
  199. if (msgSize <= kcpMessageBuffer.Length)
  200. {
  201. // receive from kcp
  202. int received = kcp.Receive(kcpMessageBuffer, msgSize);
  203. if (received >= 0)
  204. {
  205. // extract header & content without header
  206. header = (KcpHeader)kcpMessageBuffer[0];
  207. message = new ArraySegment<byte>(kcpMessageBuffer, 1, msgSize - 1);
  208. lastReceiveTime = (uint)refTime.ElapsedMilliseconds;
  209. return true;
  210. }
  211. else
  212. {
  213. // if receive failed, close everything
  214. Log.Warning($"Receive failed with error={received}. closing connection.");
  215. Disconnect();
  216. }
  217. }
  218. // we don't allow sending messages > Max, so this must be an
  219. // attacker. let's disconnect to avoid allocation attacks etc.
  220. else
  221. {
  222. Log.Warning($"KCP: possible allocation attack for msgSize {msgSize} > buffer {kcpMessageBuffer.Length}. Disconnecting the connection.");
  223. Disconnect();
  224. }
  225. }
  226. message = default;
  227. header = KcpHeader.Disconnect;
  228. return false;
  229. }
  230. void TickIncoming_Connected(uint time)
  231. {
  232. // detect common events & ping
  233. HandleTimeout(time);
  234. HandleDeadLink();
  235. HandlePing(time);
  236. HandleChoked();
  237. // any reliable kcp message received?
  238. if (ReceiveNextReliable(out KcpHeader header, out ArraySegment<byte> message))
  239. {
  240. // message type FSM. no default so we never miss a case.
  241. switch (header)
  242. {
  243. case KcpHeader.Handshake:
  244. {
  245. // we were waiting for a handshake.
  246. // it proves that the other end speaks our protocol.
  247. Log.Info("KCP: received handshake");
  248. state = KcpState.Authenticated;
  249. OnAuthenticated?.Invoke();
  250. break;
  251. }
  252. case KcpHeader.Ping:
  253. {
  254. // ping keeps kcp from timing out. do nothing.
  255. break;
  256. }
  257. case KcpHeader.Data:
  258. case KcpHeader.Disconnect:
  259. {
  260. // everything else is not allowed during handshake!
  261. Log.Warning($"KCP: received invalid header {header} while Connected. Disconnecting the connection.");
  262. Disconnect();
  263. break;
  264. }
  265. }
  266. }
  267. }
  268. void TickIncoming_Authenticated(uint time)
  269. {
  270. // detect common events & ping
  271. HandleTimeout(time);
  272. HandleDeadLink();
  273. HandlePing(time);
  274. HandleChoked();
  275. // process all received messages
  276. while (ReceiveNextReliable(out KcpHeader header, out ArraySegment<byte> message))
  277. {
  278. // message type FSM. no default so we never miss a case.
  279. switch (header)
  280. {
  281. case KcpHeader.Handshake:
  282. {
  283. // should never receive another handshake after auth
  284. Log.Warning($"KCP: received invalid header {header} while Authenticated. Disconnecting the connection.");
  285. Disconnect();
  286. break;
  287. }
  288. case KcpHeader.Data:
  289. {
  290. // call OnData IF the message contained actual data
  291. if (message.Count > 0)
  292. {
  293. //Log.Warning($"Kcp recv msg: {BitConverter.ToString(message.Array, message.Offset, message.Count)}");
  294. OnData?.Invoke(message, KcpChannel.Reliable);
  295. }
  296. // empty data = attacker, or something went wrong
  297. else
  298. {
  299. Log.Warning("KCP: received empty Data message while Authenticated. Disconnecting the connection.");
  300. Disconnect();
  301. }
  302. break;
  303. }
  304. case KcpHeader.Ping:
  305. {
  306. // ping keeps kcp from timing out. do nothing.
  307. break;
  308. }
  309. case KcpHeader.Disconnect:
  310. {
  311. // disconnect might happen
  312. Log.Info("KCP: received disconnect message");
  313. Disconnect();
  314. break;
  315. }
  316. }
  317. }
  318. }
  319. public void TickIncoming()
  320. {
  321. uint time = (uint)refTime.ElapsedMilliseconds;
  322. try
  323. {
  324. switch (state)
  325. {
  326. case KcpState.Connected:
  327. {
  328. TickIncoming_Connected(time);
  329. break;
  330. }
  331. case KcpState.Authenticated:
  332. {
  333. TickIncoming_Authenticated(time);
  334. break;
  335. }
  336. case KcpState.Disconnected:
  337. {
  338. // do nothing while disconnected
  339. break;
  340. }
  341. }
  342. }
  343. catch (SocketException exception)
  344. {
  345. // this is ok, the connection was closed
  346. Log.Info($"KCP Connection: Disconnecting because {exception}. This is fine.");
  347. Disconnect();
  348. }
  349. catch (ObjectDisposedException exception)
  350. {
  351. // fine, socket was closed
  352. Log.Info($"KCP Connection: Disconnecting because {exception}. This is fine.");
  353. Disconnect();
  354. }
  355. catch (Exception ex)
  356. {
  357. // unexpected
  358. Log.Error(ex.ToString());
  359. Disconnect();
  360. }
  361. }
  362. public void TickOutgoing()
  363. {
  364. uint time = (uint)refTime.ElapsedMilliseconds;
  365. try
  366. {
  367. switch (state)
  368. {
  369. case KcpState.Connected:
  370. case KcpState.Authenticated:
  371. {
  372. // update flushes out messages
  373. kcp.Update(time);
  374. break;
  375. }
  376. case KcpState.Disconnected:
  377. {
  378. // do nothing while disconnected
  379. break;
  380. }
  381. }
  382. }
  383. catch (SocketException exception)
  384. {
  385. // this is ok, the connection was closed
  386. Log.Info($"KCP Connection: Disconnecting because {exception}. This is fine.");
  387. Disconnect();
  388. }
  389. catch (ObjectDisposedException exception)
  390. {
  391. // fine, socket was closed
  392. Log.Info($"KCP Connection: Disconnecting because {exception}. This is fine.");
  393. Disconnect();
  394. }
  395. catch (Exception ex)
  396. {
  397. // unexpected
  398. Log.Error(ex.ToString());
  399. Disconnect();
  400. }
  401. }
  402. public void RawInput(byte[] buffer, int msgLength)
  403. {
  404. // parse channel
  405. if (msgLength > 0)
  406. {
  407. byte channel = buffer[0];
  408. switch (channel)
  409. {
  410. case (byte)KcpChannel.Reliable:
  411. {
  412. // input into kcp, but skip channel byte
  413. int input = kcp.Input(buffer, 1, msgLength - 1);
  414. if (input != 0)
  415. {
  416. Log.Warning($"Input failed with error={input} for buffer with length={msgLength - 1}");
  417. }
  418. break;
  419. }
  420. case (byte)KcpChannel.Unreliable:
  421. {
  422. // ideally we would queue all unreliable messages and
  423. // then process them in ReceiveNext() together with the
  424. // reliable messages, but:
  425. // -> queues/allocations/pools are slow and complex.
  426. // -> DOTSNET 10k is actually slower if we use pooled
  427. // unreliable messages for transform messages.
  428. //
  429. // DOTSNET 10k benchmark:
  430. // reliable-only: 170 FPS
  431. // unreliable queued: 130-150 FPS
  432. // unreliable direct: 183 FPS(!)
  433. //
  434. // DOTSNET 50k benchmark:
  435. // reliable-only: FAILS (queues keep growing)
  436. // unreliable direct: 18-22 FPS(!)
  437. //
  438. // -> all unreliable messages are DATA messages anyway.
  439. // -> let's skip the magic and call OnData directly if
  440. // the current state allows it.
  441. if (state == KcpState.Authenticated)
  442. {
  443. ArraySegment<byte> message = new ArraySegment<byte>(buffer, 1, msgLength - 1);
  444. OnData?.Invoke(message, KcpChannel.Unreliable);
  445. // set last receive time to avoid timeout.
  446. // -> we do this in ANY case even if not enabled.
  447. // a message is a message.
  448. // -> we set last receive time for both reliable and
  449. // unreliable messages. both count.
  450. // otherwise a connection might time out even
  451. // though unreliable were received, but no
  452. // reliable was received.
  453. lastReceiveTime = (uint)refTime.ElapsedMilliseconds;
  454. }
  455. else
  456. {
  457. // should never
  458. Log.Warning($"KCP: received unreliable message in state {state}. Disconnecting the connection.");
  459. Disconnect();
  460. }
  461. break;
  462. }
  463. default:
  464. {
  465. // not a valid channel. random data or attacks.
  466. Log.Info($"Disconnecting connection because of invalid channel header: {channel}");
  467. Disconnect();
  468. break;
  469. }
  470. }
  471. }
  472. }
  473. // raw send puts the data into the socket
  474. protected abstract void RawSend(byte[] data, int length);
  475. // raw send called by kcp
  476. void RawSendReliable(byte[] data, int length)
  477. {
  478. // copy channel header, data into raw send buffer, then send
  479. rawSendBuffer[0] = (byte)KcpChannel.Reliable;
  480. Buffer.BlockCopy(data, 0, rawSendBuffer, 1, length);
  481. RawSend(rawSendBuffer, length + 1);
  482. }
  483. void SendReliable(KcpHeader header, ArraySegment<byte> content)
  484. {
  485. // 1 byte header + content needs to fit into send buffer
  486. if (1 + content.Count <= kcpSendBuffer.Length) // TODO
  487. {
  488. // copy header, content (if any) into send buffer
  489. kcpSendBuffer[0] = (byte)header;
  490. if (content.Count > 0)
  491. Buffer.BlockCopy(content.Array, content.Offset, kcpSendBuffer, 1, content.Count);
  492. // send to kcp for processing
  493. int sent = kcp.Send(kcpSendBuffer, 0, 1 + content.Count);
  494. if (sent < 0)
  495. {
  496. Log.Warning($"Send failed with error={sent} for content with length={content.Count}");
  497. }
  498. }
  499. // otherwise content is larger than MaxMessageSize. let user know!
  500. else Log.Error($"Failed to send reliable message of size {content.Count} because it's larger than ReliableMaxMessageSize={ReliableMaxMessageSize(kcp.rcv_wnd)}");
  501. }
  502. void SendUnreliable(ArraySegment<byte> message)
  503. {
  504. // message size needs to be <= unreliable max size
  505. if (message.Count <= UnreliableMaxMessageSize)
  506. {
  507. // copy channel header, data into raw send buffer, then send
  508. rawSendBuffer[0] = (byte)KcpChannel.Unreliable;
  509. Buffer.BlockCopy(message.Array, message.Offset, rawSendBuffer, 1, message.Count);
  510. RawSend(rawSendBuffer, message.Count + 1);
  511. }
  512. // otherwise content is larger than MaxMessageSize. let user know!
  513. else Log.Error($"Failed to send unreliable message of size {message.Count} because it's larger than UnreliableMaxMessageSize={UnreliableMaxMessageSize}");
  514. }
  515. // server & client need to send handshake at different times, so we need
  516. // to expose the function.
  517. // * client should send it immediately.
  518. // * server should send it as reply to client's handshake, not before
  519. // (server should not reply to random internet messages with handshake)
  520. // => handshake info needs to be delivered, so it goes over reliable.
  521. public void SendHandshake()
  522. {
  523. Log.Info("KcpConnection: sending Handshake to other end!");
  524. SendReliable(KcpHeader.Handshake, default);
  525. }
  526. public void SendData(ArraySegment<byte> data, KcpChannel channel)
  527. {
  528. // sending empty segments is not allowed.
  529. // nobody should ever try to send empty data.
  530. // it means that something went wrong, e.g. in Mirror/DOTSNET.
  531. // let's make it obvious so it's easy to debug.
  532. if (data.Count == 0)
  533. {
  534. Log.Warning("KcpConnection: tried sending empty message. This should never happen. Disconnecting.");
  535. Disconnect();
  536. return;
  537. }
  538. switch (channel)
  539. {
  540. case KcpChannel.Reliable:
  541. SendReliable(KcpHeader.Data, data);
  542. break;
  543. case KcpChannel.Unreliable:
  544. SendUnreliable(data);
  545. break;
  546. }
  547. }
  548. // ping goes through kcp to keep it from timing out, so it goes over the
  549. // reliable channel.
  550. void SendPing() => SendReliable(KcpHeader.Ping, default);
  551. // disconnect info needs to be delivered, so it goes over reliable
  552. void SendDisconnect() => SendReliable(KcpHeader.Disconnect, default);
  553. protected virtual void Dispose() {}
  554. // disconnect this connection
  555. public void Disconnect()
  556. {
  557. // only if not disconnected yet
  558. if (state == KcpState.Disconnected)
  559. return;
  560. // send a disconnect message
  561. if (socket.Connected)
  562. {
  563. try
  564. {
  565. SendDisconnect();
  566. kcp.Flush();
  567. }
  568. catch (SocketException)
  569. {
  570. // this is ok, the connection was already closed
  571. }
  572. catch (ObjectDisposedException)
  573. {
  574. // this is normal when we stop the server
  575. // the socket is stopped so we can't send anything anymore
  576. // to the clients
  577. // the clients will eventually timeout and realize they
  578. // were disconnected
  579. }
  580. }
  581. // set as Disconnected, call event
  582. Log.Info("KCP Connection: Disconnected.");
  583. state = KcpState.Disconnected;
  584. OnDisconnected?.Invoke();
  585. }
  586. // get remote endpoint
  587. public EndPoint GetRemoteEndPoint() => remoteEndPoint;
  588. }
  589. }