/// <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); }
/// <summary> /// Creates and initializes a new <see cref="LiteSender"/> base instance. /// </summary> public LiteSender(ILiteConnection connection) { _sendingCollection = new BlockingCollection <byte[]>(); _cancellationTokenSource = new CancellationTokenSource(); _cancellationToken = _cancellationTokenSource.Token; _connection = connection; _socketAsyncEvent = new SocketAsyncEventArgs(); _socketAsyncEvent.Completed += OnSendCompleted; }
/// <summary> /// Process a single received message. /// </summary> /// <param name="connection">Connection that received the message.</param> /// <param name="messageData">Message data.</param> internal async void ProcessReceivedMessage(ILiteConnection connection, byte[] messageData) { try { using ILitePacketStream packetStream = _packetProcessor.CreatePacket(messageData); await connection.HandleMessageAsync(packetStream).ConfigureAwait(false); } catch (Exception e) { OnError(e); } }
/// <summary> /// Creates a new <see cref="LiteDefaultConnectionToken"/> instance with a <see cref="ILiteConnection"/>. /// </summary> /// <param name="connection">Current connection.</param> /// <param name="handlerAction">Action to execute when a packet message is being processed.</param> public LiteQueuedConnectionToken(ILiteConnection connection, Action <ILiteConnection, byte[]> handlerAction) { Connection = connection; _handlerAction = handlerAction; DataToken = new LiteDataToken(Connection); _receiveMessageQueue = new BlockingCollection <byte[]>(); _receiveCancellationTokenSource = new CancellationTokenSource(); _receiveCancellationToken = _receiveCancellationTokenSource.Token; Task.Factory.StartNew(OnProcessMessageQueue, _receiveCancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default); }
/// <summary> /// Builds an user connection token with the given <see cref="ILiteConnection"/>. /// </summary> /// <param name="connection">The connection associated with the token.</param> /// <returns>A new connection token implementation.</returns> private ILiteConnectionToken BuildConnectionToken(ILiteConnection connection) => ReceiveStrategy switch {
/// <summary> /// Called when a client has been disconnected. /// </summary> /// <param name="client">Disconnected client.</param> private void OnDisconnected(ILiteConnection client) => Disconnected?.Invoke(this, client);
/// <summary> /// Creates a new <see cref="LiteDataToken"/> instance. /// </summary> /// <param name="connection">Current connection attached to this data token.</param> /// <exception cref="ArgumentNullException">Thrown when the given connection is null.</exception> public LiteDataToken(ILiteConnection connection) { Connection = connection ?? throw new ArgumentNullException(nameof(connection)); }
/// <summary> /// Creates a new <see cref="LiteReceiverException"/> instance with a default receiver message error. /// </summary> /// <param name="connection">Connection that thrown the exception.</param> /// <param name="socketError">Socket error.</param> /// <param name="innerException">Inner exception.</param> public LiteReceiverException(ILiteConnection connection, SocketError socketError, Exception?innerException = null) : base($"An error as occured while receiving data for connection with id: '{connection.Id}'.", innerException) { Connection = connection; SocketError = socketError; }
/// <summary> /// Creates a new <see cref="LiteDefaultConnectionToken"/> instance with a <see cref="ILiteConnection"/>. /// </summary> /// <param name="connection">Current connection.</param> /// <param name="handlerAction">Action to execute when a packet message is being processed.</param> public LiteDefaultConnectionToken(ILiteConnection connection, Action <ILiteConnection, byte[]> handlerAction) { Connection = connection; _handlerAction = handlerAction; DataToken = new LiteDataToken(); }
/// <summary> /// Creates a new <see cref="LiteReceivedMessage"/> instance. /// </summary> /// <param name="connection">Connection that received the message.</param> /// <param name="message">Current message byte array.</param> /// <param name="handle">Handler to process data.</param> public LiteReceivedMessage(ILiteConnection connection, byte[] message, Func <ILiteConnection, byte[], Task> handle) { _connection = connection; _message = message; _handler = handle; }
private void OnDisconnected(object?sender, ILiteConnection e) { DisconnectUser(e.Id); }