Exemplo n.º 1
0
        // method for the read thread
        // TODO why client ?
        private void ClientReadLoop()
        {
            try
            {
                OnAuthenticated.Call(session.LocalEndPoint, session.RemoteEndPoint);

                connection.State = ConnectionState.Connected;

                byte channel = 255;
                var  buffer  = new byte[0];
                while (session.Read(ref channel, ref buffer)) // The plan is that after session.Dispose throws an exception
                {
                    listener?.OnMessage(channel, buffer);     // TODO send also client
                }
            }
            catch (Exception e)
            {
                logger.Log($"[{RemoteEndPoint}] Disconnecting, error in ClientReadLoop {e.Message}");

                Disconnect(DisconnectReason.ErrorInReadLoop);
            }
        }
Exemplo n.º 2
0
        private void Socket_onMessage(byte[] buffer, int offset, int length)
        {
            var inputN = _kcp.Input(buffer, offset, length, true, ackNoDelay);

            if (inputN < 0)
            {
                return;
            }
            _buffer.Clear();
            while (true)
            {
                var size = _kcp.PeekSize();
                if (size <= 0)
                {
                    break;
                }

                _buffer.EnsureWritableBytes(size);

                var n = _kcp.Recv(_buffer.buffer, _buffer.writer, size);
                if (n > 0)
                {
                    _buffer.writer += n;
                }
            }

            // 有数据待接收
            if (_buffer.canRead > 0)
            {
                _listen.OnMessage(socket, _buffer.buffer, _buffer.reader, _buffer.canRead);
                // 读完重置读写指针
                if (_buffer.reader == _buffer.writer)
                {
                    _buffer.Clear();
                }
            }
        }