Exemplo n.º 1
0
 public void Stop()
 {
     if (_kcpLink != null)
     {
         _kcpLink?.Stop(_Stop);
     }
     else
     {
         _Stop();
     }
 }
Exemplo n.º 2
0
        private async Task ParseCmd(ReadOnlyMemory <byte> recvBufMemory, int length)
        {
            byte cmd = recvBufMemory.Span[0];

            if ((cmd & NetworkCmd.SYN) != 0)
            {
                if ((cmd & NetworkCmd.ACK) != 0)
                {
                    //  Debug.LogFormat("recv SYN +ACK");
                    //  syn ack
                    uint conv = BinaryPrimitives.ReadUInt32LittleEndian(recvBufMemory.Span.Slice(1));
                    if (_kcpLink == null)
                    {
                        _kcpLink = new KcpLink();
                        _kcpLink.Run(conv, new KcpUdpCallback(_socket, _remoteEP));
                        _kcpLink.OnRecvKcpPackage += KcpLink_OnRecvKcpPackage;
                    }
                    //  发送 ayn ack
                    var sendBytes = new byte[5];
                    sendBytes[0] = NetworkCmd.SYN | NetworkCmd.ACK;
                    BinaryPrimitives.WriteUInt32LittleEndian(new Span <byte>(sendBytes, 1, 4), conv);
                    //  Debug.LogFormat("send SYN + ACK");
                    _socket.Send(sendBytes, SocketFlags.None);
                    _synTaskCancellationTokenSource.Cancel();
                }
                else
                {
                    // syn 客户端不存在这种情况
                }
            }
            else if ((cmd & NetworkCmd.PUSH) != 0)
            {
                //  Debug.LogFormat("recv PUSH");
                await _kcpLink.RecvFromRemoteAsync(recvBufMemory.Slice(1, length - 1));
            }
            else if ((cmd & NetworkCmd.FIN) != 0)
            {
                //  Debug.LogFormat("recv FIN");
                _kcpLink.Stop();
            }
        }