Esempio n. 1
0
        public MessagingListenerClient(Socket socket, IMessageHandlerInvoker messageHandlerInvoker)
        {
            _socket = socket ?? throw new ArgumentNullException(nameof(socket));
            _messageHandlerInvoker = messageHandlerInvoker;

            // For inbound data
            _readAwaitable = new SocketAwaitable(_readArgs);
            _readArgs.SetBuffer(_readBuffer, 0, _readBuffer.Length);
            _receiver = new SocketReceiver(socket, _readArgs, _readAwaitable, _readBuffer);
            _decoder  = new SimpleProtocolDecoder(_serializer);

            // For outbound data
            _encoder        = new SimpleProtocolEncoder(_serializer);
            _writeArgs      = new SocketAsyncEventArgs();
            _writeAwaitable = new SocketAwaitable(_writeArgs);
            _sendState      = new SocketSender(_socket, _writeArgs, _writeAwaitable);
        }
Esempio n. 2
0
        protected async Task Connect()
        {
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _writeArgs.RemoteEndPoint = new DnsEndPoint(_remoteHost, _remotePort);
            var isPending = _socket.ConnectAsync(_writeArgs);

            if (isPending)
            {
                _writeAwaitable.Reset();
                await _writeAwaitable;
            }

            _sender   = new SocketSender(_socket, _writeArgs, _writeAwaitable);
            _receiver = new SocketReceiver(_socket, _readArgs, _readAwaitable, _readBuffer);

            await _encoder.EncodeHandshake(_sender, CurrentVersion);

#pragma warning disable 4014 //Need to run it in a separate task to not block the calling class.
            Task.Run(ReceiveMessages);
#pragma warning restore 4014
        }