@@ -12,6 +12,11 @@ enum KcpState { Connected, Authenticated, Disconnected }
1212
1313 public class KcpPeer
1414 {
15+ // identifier for logging.
16+ // we don't just want to show "KcpPeer" on server and client.
17+ // name it "KcpClient/ServerPeer".
18+ readonly string identifier ;
19+
1520 // kcp reliability algorithm
1621 internal Kcp kcp ;
1722
@@ -171,8 +176,12 @@ public KcpPeer(
171176 Action OnDisconnected ,
172177 Action < ErrorCode , string > OnError ,
173178 KcpConfig config ,
179+ String identifier ,
174180 uint cookie )
175181 {
182+ // initialize logging identifier
183+ this . identifier = identifier ;
184+
176185 // initialize callbacks first to ensure they can be used safely.
177186 this . OnAuthenticated = OnAuthenticated ;
178187 this . OnData = OnData ;
@@ -225,7 +234,7 @@ void HandleTimeout(uint time)
225234 {
226235 // pass error to user callback. no need to log it manually.
227236 // GetType() shows Server/ClientConn instead of just Connection.
228- OnError ( ErrorCode . Timeout , $ "KcpPeer : Connection timed out after not receiving any message for { timeout } ms. Disconnecting.") ;
237+ OnError ( ErrorCode . Timeout , $ "{ identifier } : Connection timed out after not receiving any message for { timeout } ms. Disconnecting.") ;
229238 Disconnect ( ) ;
230239 }
231240 }
@@ -237,7 +246,7 @@ void HandleDeadLink()
237246 {
238247 // pass error to user callback. no need to log it manually.
239248 // GetType() shows Server/ClientConn instead of just Connection.
240- OnError ( ErrorCode . Timeout , $ "KcpPeer : dead_link detected: a message was retransmitted { kcp . dead_link } times without ack. Disconnecting.") ;
249+ OnError ( ErrorCode . Timeout , $ "{ identifier } : dead_link detected: a message was retransmitted { kcp . dead_link } times without ack. Disconnecting.") ;
241250 Disconnect ( ) ;
242251 }
243252 }
@@ -267,7 +276,7 @@ void HandleChoked()
267276 // pass error to user callback. no need to log it manually.
268277 // GetType() shows Server/ClientConn instead of just Connection.
269278 OnError ( ErrorCode . Congestion ,
270- $ "KcpPeer : disconnecting connection because it can't process data fast enough.\n " +
279+ $ "{ identifier } : disconnecting connection because it can't process data fast enough.\n " +
271280 $ "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 " +
272281 $ "* Try to Enable NoDelay, decrease INTERVAL, disable Congestion Window (= enable NOCWND!), increase SEND/RECV WINDOW or compress data.\n " +
273282 $ "* Or perhaps the network is simply too slow on our end, or on the other end.") ;
@@ -299,7 +308,7 @@ bool ReceiveNextReliable(out KcpHeader header, out ArraySegment<byte> message)
299308 // we don't allow sending messages > Max, so this must be an
300309 // attacker. let's disconnect to avoid allocation attacks etc.
301310 // pass error to user callback. no need to log it manually.
302- OnError ( ErrorCode . InvalidReceive , $ "KcpPeer : possible allocation attack for msgSize { msgSize } > buffer { kcpMessageBuffer . Length } . Disconnecting the connection.") ;
311+ OnError ( ErrorCode . InvalidReceive , $ "{ identifier } : possible allocation attack for msgSize { msgSize } > buffer { kcpMessageBuffer . Length } . Disconnecting the connection.") ;
303312 Disconnect ( ) ;
304313 return false ;
305314 }
@@ -311,7 +320,7 @@ bool ReceiveNextReliable(out KcpHeader header, out ArraySegment<byte> message)
311320 // if receive failed, close everything
312321 // pass error to user callback. no need to log it manually.
313322 // GetType() shows Server/ClientConn instead of just Connection.
314- OnError ( ErrorCode . InvalidReceive , $ "KcpPeer : Receive failed with error={ received } . closing connection.") ;
323+ OnError ( ErrorCode . InvalidReceive , $ "{ identifier } : Receive failed with error={ received } . closing connection.") ;
315324 Disconnect ( ) ;
316325 return false ;
317326 }
@@ -346,7 +355,7 @@ void TickIncoming_Connected(uint time)
346355 if ( message . Count != 4 )
347356 {
348357 // pass error to user callback. no need to log it manually.
349- OnError ( ErrorCode . InvalidReceive , $ "KcpPeer : received invalid handshake message with size { message . Count } != 4. Disconnecting the connection.") ;
358+ OnError ( ErrorCode . InvalidReceive , $ "{ identifier } : received invalid handshake message with size { message . Count } != 4. Disconnecting the connection.") ;
350359 Disconnect ( ) ;
351360 return ;
352361 }
@@ -356,7 +365,7 @@ void TickIncoming_Connected(uint time)
356365 Buffer . BlockCopy ( message . Array , message . Offset , receivedCookie , 0 , 4 ) ;
357366 uint prettyCookie = BitConverter . ToUInt32 ( message . Array , message . Offset ) ;
358367
359- Log . Info ( $ "KcpPeer : received handshake with cookie={ prettyCookie } ") ;
368+ Log . Info ( $ "{ identifier } : received handshake with cookie={ prettyCookie } ") ;
360369 state = KcpState . Authenticated ;
361370 OnAuthenticated ? . Invoke ( ) ;
362371 break ;
@@ -372,7 +381,7 @@ void TickIncoming_Connected(uint time)
372381 // everything else is not allowed during handshake!
373382 // pass error to user callback. no need to log it manually.
374383 // GetType() shows Server/ClientConn instead of just Connection.
375- OnError ( ErrorCode . InvalidReceive , $ "KcpPeer : received invalid header { header } while Connected. Disconnecting the connection.") ;
384+ OnError ( ErrorCode . InvalidReceive , $ "{ identifier } : received invalid header { header } while Connected. Disconnecting the connection.") ;
376385 Disconnect ( ) ;
377386 break ;
378387 }
@@ -398,7 +407,7 @@ void TickIncoming_Authenticated(uint time)
398407 {
399408 // should never receive another handshake after auth
400409 // GetType() shows Server/ClientConn instead of just Connection.
401- Log . Warning ( $ "KcpPeer : received invalid header { header } while Authenticated. Disconnecting the connection.") ;
410+ Log . Warning ( $ "{ identifier } : received invalid header { header } while Authenticated. Disconnecting the connection.") ;
402411 Disconnect ( ) ;
403412 break ;
404413 }
@@ -415,7 +424,7 @@ void TickIncoming_Authenticated(uint time)
415424 {
416425 // pass error to user callback. no need to log it manually.
417426 // GetType() shows Server/ClientConn instead of just Connection.
418- OnError ( ErrorCode . InvalidReceive , $ "KcpPeer : received empty Data message while Authenticated. Disconnecting the connection.") ;
427+ OnError ( ErrorCode . InvalidReceive , $ "{ identifier } : received empty Data message while Authenticated. Disconnecting the connection.") ;
419428 Disconnect ( ) ;
420429 }
421430 break ;
@@ -429,7 +438,7 @@ void TickIncoming_Authenticated(uint time)
429438 {
430439 // disconnect might happen
431440 // GetType() shows Server/ClientConn instead of just Connection.
432- Log . Info ( $ "KcpPeer : received disconnect message") ;
441+ Log . Info ( $ "{ identifier } : received disconnect message") ;
433442 Disconnect ( ) ;
434443 break ;
435444 }
@@ -468,23 +477,23 @@ public void TickIncoming()
468477 // this is ok, the connection was closed
469478 // pass error to user callback. no need to log it manually.
470479 // GetType() shows Server/ClientConn instead of just Connection.
471- OnError ( ErrorCode . ConnectionClosed , $ "KcpPeer : Disconnecting because { exception } . This is fine.") ;
480+ OnError ( ErrorCode . ConnectionClosed , $ "{ identifier } : Disconnecting because { exception } . This is fine.") ;
472481 Disconnect ( ) ;
473482 }
474483 catch ( ObjectDisposedException exception )
475484 {
476485 // fine, socket was closed
477486 // pass error to user callback. no need to log it manually.
478487 // GetType() shows Server/ClientConn instead of just Connection.
479- OnError ( ErrorCode . ConnectionClosed , $ "KcpPeer : Disconnecting because { exception } . This is fine.") ;
488+ OnError ( ErrorCode . ConnectionClosed , $ "{ identifier } : Disconnecting because { exception } . This is fine.") ;
480489 Disconnect ( ) ;
481490 }
482491 catch ( Exception exception )
483492 {
484493 // unexpected
485494 // pass error to user callback. no need to log it manually.
486495 // GetType() shows Server/ClientConn instead of just Connection.
487- OnError ( ErrorCode . Unexpected , $ "KcpPeer : unexpected Exception: { exception } ") ;
496+ OnError ( ErrorCode . Unexpected , $ "{ identifier } : unexpected Exception: { exception } ") ;
488497 Disconnect ( ) ;
489498 }
490499 }
@@ -517,23 +526,23 @@ public void TickOutgoing()
517526 // this is ok, the connection was closed
518527 // pass error to user callback. no need to log it manually.
519528 // GetType() shows Server/ClientConn instead of just Connection.
520- OnError ( ErrorCode . ConnectionClosed , $ "KcpPeer : Disconnecting because { exception } . This is fine.") ;
529+ OnError ( ErrorCode . ConnectionClosed , $ "{ identifier } : Disconnecting because { exception } . This is fine.") ;
521530 Disconnect ( ) ;
522531 }
523532 catch ( ObjectDisposedException exception )
524533 {
525534 // fine, socket was closed
526535 // pass error to user callback. no need to log it manually.
527536 // GetType() shows Server/ClientConn instead of just Connection.
528- OnError ( ErrorCode . ConnectionClosed , $ "KcpPeer : Disconnecting because { exception } . This is fine.") ;
537+ OnError ( ErrorCode . ConnectionClosed , $ "{ identifier } : Disconnecting because { exception } . This is fine.") ;
529538 Disconnect ( ) ;
530539 }
531540 catch ( Exception exception )
532541 {
533542 // unexpected
534543 // pass error to user callback. no need to log it manually.
535544 // GetType() shows Server/ClientConn instead of just Connection.
536- OnError ( ErrorCode . Unexpected , $ "KcpPeer : unexpected exception: { exception } ") ;
545+ OnError ( ErrorCode . Unexpected , $ "{ identifier } : unexpected exception: { exception } ") ;
537546 Disconnect ( ) ;
538547 }
539548 }
@@ -545,7 +554,7 @@ void OnRawInputReliable(ArraySegment<byte> message)
545554 if ( input != 0 )
546555 {
547556 // GetType() shows Server/ClientConn instead of just Connection.
548- Log . Warning ( $ "KcpPeer : Input failed with error={ input } for buffer with length={ message . Count - 1 } ") ;
557+ Log . Warning ( $ "{ identifier } : Input failed with error={ input } for buffer with length={ message . Count - 1 } ") ;
549558 }
550559 }
551560
@@ -596,7 +605,7 @@ void OnRawInputUnreliable(ArraySegment<byte> message)
596605 // add another 'round trip time' of latency to the handshake.
597606 //
598607 // it's best to simply ignore invalid unreliable messages here.
599- // Log.Info($"KcpPeer : received unreliable message while not authenticated.");
608+ // Log.Info($"{identifier} : received unreliable message while not authenticated.");
600609 }
601610 }
602611
@@ -621,7 +630,7 @@ public void RawInput(ArraySegment<byte> segment)
621630 // simply drop the message if the cookie doesn't match.
622631 if ( state == KcpState . Authenticated && messageCookie != cookie )
623632 {
624- Log . Warning ( $ "KcpPeer : dropped message with invalid cookie: { messageCookie } expected: { cookie } .") ;
633+ Log . Warning ( $ "{ identifier } : dropped message with invalid cookie: { messageCookie } expected: { cookie } .") ;
625634 return ;
626635 }
627636
@@ -645,7 +654,7 @@ public void RawInput(ArraySegment<byte> segment)
645654 // invalid channel indicates random internet noise.
646655 // servers may receive random UDP data.
647656 // just ignore it, but log for easier debugging.
648- Log . Warning ( $ "KcpPeer : invalid channel header: { channel } , likely internet noise") ;
657+ Log . Warning ( $ "{ identifier } : invalid channel header: { channel } , likely internet noise") ;
649658 break ;
650659 }
651660 }
@@ -678,7 +687,7 @@ void SendReliable(KcpHeader header, ArraySegment<byte> content)
678687 {
679688 // otherwise content is larger than MaxMessageSize. let user know!
680689 // GetType() shows Server/ClientConn instead of just Connection.
681- OnError ( ErrorCode . InvalidSend , $ "KcpPeer : Failed to send reliable message of size { content . Count } because it's larger than ReliableMaxMessageSize={ reliableMax } ") ;
690+ OnError ( ErrorCode . InvalidSend , $ "{ identifier } : Failed to send reliable message of size { content . Count } because it's larger than ReliableMaxMessageSize={ reliableMax } ") ;
682691 return ;
683692 }
684693
@@ -694,7 +703,7 @@ void SendReliable(KcpHeader header, ArraySegment<byte> content)
694703 if ( sent < 0 )
695704 {
696705 // GetType() shows Server/ClientConn instead of just Connection.
697- OnError ( ErrorCode . InvalidSend , $ "KcpPeer : Send failed with error={ sent } for content with length={ content . Count } ") ;
706+ OnError ( ErrorCode . InvalidSend , $ "{ identifier } : Send failed with error={ sent } for content with length={ content . Count } ") ;
698707 }
699708 }
700709
@@ -705,7 +714,7 @@ void SendUnreliable(ArraySegment<byte> message)
705714 {
706715 // otherwise content is larger than MaxMessageSize. let user know!
707716 // GetType() shows Server/ClientConn instead of just Connection.
708- Log . Error ( $ "KcpPeer : Failed to send unreliable message of size { message . Count } because it's larger than UnreliableMaxMessageSize={ unreliableMax } ") ;
717+ Log . Error ( $ "{ identifier } : Failed to send unreliable message of size { message . Count } because it's larger than UnreliableMaxMessageSize={ unreliableMax } ") ;
709718 return ;
710719 }
711720
@@ -744,7 +753,7 @@ public void SendHandshake()
744753 byte [ ] cookieBytes = BitConverter . GetBytes ( cookie ) ;
745754
746755 // GetType() shows Server/ClientConn instead of just Connection.
747- Log . Info ( $ "KcpPeer : sending Handshake to other end with cookie={ cookie } !") ;
756+ Log . Info ( $ "{ identifier } : sending Handshake to other end with cookie={ cookie } !") ;
748757 SendReliable ( KcpHeader . Handshake , new ArraySegment < byte > ( cookieBytes ) ) ;
749758 }
750759
@@ -758,7 +767,7 @@ public void SendData(ArraySegment<byte> data, KcpChannel channel)
758767 {
759768 // pass error to user callback. no need to log it manually.
760769 // GetType() shows Server/ClientConn instead of just Connection.
761- OnError ( ErrorCode . InvalidSend , $ "KcpPeer : tried sending empty message. This should never happen. Disconnecting.") ;
770+ OnError ( ErrorCode . InvalidSend , $ "{ identifier } : tried sending empty message. This should never happen. Disconnecting.") ;
762771 Disconnect ( ) ;
763772 return ;
764773 }
@@ -811,7 +820,7 @@ public void Disconnect()
811820
812821 // set as Disconnected, call event
813822 // GetType() shows Server/ClientConn instead of just Connection.
814- Log . Info ( $ "KcpPeer : Disconnected.") ;
823+ Log . Info ( $ "{ identifier } : Disconnected.") ;
815824 state = KcpState . Disconnected ;
816825 OnDisconnected ? . Invoke ( ) ;
817826 }
0 commit comments