KcpConnection.cs 29 KB

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