Пример #1
0
        private static async Task CustomProtocol(IServiceProvider serviceProvider)
        {
            var client = new ClientBuilder(serviceProvider)
                         .UseSockets()
                         .UseConnectionLogging()
                         .Build();

            await using var connection = await client.ConnectAsync(new IPEndPoint (IPAddress.Loopback, 5005));

            Console.WriteLine($"Connected to {connection.LocalEndPoint}");

            var protocol = new LengthPrefixedProtocol();
            var reader   = connection.CreateReader();
            var writer   = connection.CreateWriter();

            while (true)
            {
                var line = Console.ReadLine();
                await writer.WriteAsync(protocol, new Message(Encoding.UTF8.GetBytes(line)));

                var result = await reader.ReadAsync(protocol);

                if (result.IsCompleted)
                {
                    break;
                }

                reader.Advance();
            }
        }
Пример #2
0
        public override async Task OnConnectedAsync(ConnectionContext connection)
        {
            // Use a length prefixed protocol
            var protocol = new LengthPrefixedProtocol();
            var reader   = connection.CreateReader();
            var writer   = connection.CreateWriter();

            while (true)
            {
                try
                {
                    var result = await reader.ReadAsync(protocol);

                    var message = result.Message;

                    _logger.LogInformation("Received a message of {Length} bytes", message.Payload.Length);

                    if (result.IsCompleted)
                    {
                        break;
                    }

                    await writer.WriteAsync(protocol, message);
                }
                finally
                {
                    reader.Advance();
                }
            }
        }
Пример #3
0
        public override async Task OnConnectedAsync(ConnectionContext connection)
        {
            // Use a length prefixed protocol
            var protocol = new LengthPrefixedProtocol();
            var reader   = Protocol.CreateReader(connection, protocol);
            var writer   = Protocol.CreateWriter(connection, protocol);

            while (true)
            {
                var message = await reader.ReadAsync();

                _logger.LogInformation("Received a message of {Length} bytes", message.Payload.Length);

                // REVIEW: We need a ReadResult<T> to indicate completion and cancellation
                if (message.Payload == null)
                {
                    break;
                }
            }
        }
Пример #4
0
        // Protected implementation of Dispose pattern.
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            if (disposing)
            {
                // Free any other managed objects here.
                //
                this._logger  = null;
                this.protocol = null;
                this.writer   = null;
                this.reader   = null;
            }

            // Free any unmanaged objects here.
            //
            disposed = true;
        }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MyCustomProtocol"/> class.
 /// </summary>
 /// <param name="logger">The logger.</param>
 public MyCustomProtocol(ILogger <MyCustomProtocol> logger)
 {
     this.disposed = false;
     _logger       = logger;// Use a length prefixed protocol
     this.protocol = new LengthPrefixedProtocol();
 }