/// <summary> /// Receive data from a client. /// </summary> /// <param name="userConnectionToken">User connection token.</param> /// <param name="socketAsyncEvent">Socket async event arguments.</param> private void ReceiveData(ILiteConnectionToken userConnectionToken, SocketAsyncEventArgs socketAsyncEvent) { if (!userConnectionToken.Connection.Socket.ReceiveAsync(socketAsyncEvent)) { ProcessReceive(userConnectionToken, socketAsyncEvent); } }
/// <summary> /// Starts the receive process for the given connection and socket. /// </summary> /// <param name="connection">User connection.</param> public void StartReceiving(ILiteConnection connection) { ILiteConnectionToken token = BuildConnectionToken(connection); SocketAsyncEventArgs socketAsyncEvent = GetSocketEvent(); socketAsyncEvent.UserToken = token; ReceiveData(token, socketAsyncEvent); }
private void ProcessReceive(ILiteConnectionToken clientToken, SocketAsyncEventArgs socketAsyncEvent) { try { if (socketAsyncEvent.BytesTransferred > 0) { if (socketAsyncEvent.SocketError == SocketError.Success) { if (socketAsyncEvent.Buffer is null) { throw new LiteNetworkException("A network error occurred: socket buffer is null."); } IEnumerable <byte[]> messages = _packetParser.ParseIncomingData(clientToken.DataToken, socketAsyncEvent.Buffer, socketAsyncEvent.BytesTransferred); if (messages.Any()) { clientToken.ProcessReceivedMessages(messages); } if (clientToken.DataToken.DataStartOffset >= socketAsyncEvent.BytesTransferred) { clientToken.DataToken.Reset(); } ReceiveData(clientToken, socketAsyncEvent); } else { throw new LiteReceiverException(clientToken.Connection, socketAsyncEvent.SocketError); } } else { clientToken.Dispose(); ClearSocketEvent(socketAsyncEvent); OnDisconnected(clientToken.Connection); } } catch (Exception e) { OnError(e); } }