示例#1
0
        /// <summary>
        /// The callback from received message from connected server.
        /// </summary>
        /// <param name="asyncResult">The async result from a received message from connected server.</param>
        private void ReceiveDataCallback(IAsyncResult asyncResult)
        {
            try
            {
                if (_listener.Connected)
                {
                    var endRead = _stream.EndRead(asyncResult);

                    if (endRead != 0)
                    {
                        var numArray = new byte[endRead];
                        Buffer.BlockCopy(_buffer, 0, numArray, 0, endRead);

                        _stream.BeginRead(_buffer, 0, _listener.ReceiveBufferSize, ReceiveDataCallback, null);

                        var buffer = KingBufferReader.Create(numArray, 0, numArray.Length);

                        _messageReceivedHandler(buffer);

                        return;
                    }
                }

                _stream.Close();
                _disconnectedHandler();
            }
            catch (Exception ex)
            {
                _stream.Close();
                _disconnectedHandler();
                Console.WriteLine($"Error: {ex.Message}.");
            }
        }
示例#2
0
        /// <summary>
        /// The callback from received message from connected server.
        /// </summary>
        /// <param name="asyncResult">The async result from a received message from connected server.</param>
        private void ReceiveDataCallback(IAsyncResult asyncResult)
        {
            try
            {
                if (_listener.Connected)
                {
                    int bytesRead = _listener.EndReceive(asyncResult);

                    if (bytesRead > 0)
                    {
                        var kingBufferReader = KingBufferReader.Create((byte[])asyncResult.AsyncState, 0, bytesRead);

                        _messageReceivedHandler.Invoke(kingBufferReader);

                        _listener.BeginReceiveFrom(_buffer, 0, _buffer.Length, SocketFlags.None, ref _remoteEndPoint, new AsyncCallback(this.ReceiveDataCallback), (byte[])asyncResult.AsyncState);
                    }
                }
                else
                {
                    _disconnectedHandler();
                }
            }
            catch (Exception ex)
            {
                _disconnectedHandler();
                Console.WriteLine($"Error: {ex.Message}.");
            }
        }
示例#3
0
        /// <summary>
        /// The callback from received message from connected client.
        /// </summary>
        /// <param name="asyncResult">The async result from a received message from connected client.</param>
        private void ReceiveDataCallback(IAsyncResult asyncResult)
        {
            try
            {
                if (_socketClient.Connected)
                {
                    var endRead = _stream.EndRead(asyncResult);

                    var numArray = new byte[endRead];

                    if (endRead != 0)
                    {
                        Buffer.BlockCopy(_buffer, 0, numArray, 0, endRead);

                        _stream.BeginRead(_buffer, 0, _socketClient.ReceiveBufferSize, ReceiveDataCallback, null);

                        var buffer = KingBufferReader.Create(numArray, 0, numArray.Length);

                        _messageReceivedHandler(this, buffer);

                        return;
                    }
                }

                _socketClient.Close();
                _clientDisconnectedHandler(this);
            }
            catch (Exception ex)
            {
                _socketClient.Dispose();
                _clientDisconnectedHandler(this);
            }

            Console.WriteLine($"Client '{IpAddress}' Disconnected.");
        }
示例#4
0
 /// <summary>
 /// The callback from received message from connected client.
 /// </summary>
 /// <param name="data">The data of message received.</param>
 public void ReceiveDataCallback(byte[] data)
 {
     try
     {
         _messageReceivedHandler?.Invoke(this, KingBufferReader.Create(data, 0, data.Length));
     }
     catch (Exception ex)
     {
         _clientDisconnectedHandler(this);
         Console.WriteLine($"Client '{IpAddress}' Disconnected.");
     }
 }
示例#5
0
        /// <summary>
        /// This method is reponsible for wait for connections.
        /// </summary>
        private async void WaitConnection()
        {
            while (_webSocket.State == WebSocketState.Open)
            {
                try
                {
                    var ret = await _webSocket.ReceiveAsync(_buff, CancellationToken.None);

                    if (ret.MessageType == WebSocketMessageType.Text)
                    {
                        if (_listenerContext.Request.RemoteEndPoint != null)
                        {
                            var data = _buff.Take(ret.Count).ToArray();

                            var writer = KingBufferWriter.Create();
                            writer.Write(data);

                            var reader = KingBufferReader.Create(writer.BufferData, 0, writer.Length);

                            _messageReceivedHandler(this, reader);
                        }
                    }
                    else if (ret.MessageType == WebSocketMessageType.Binary)
                    {
                        if (_listenerContext.Request.RemoteEndPoint != null)
                        {
                            var reader = KingBufferReader.Create(_buff.Take(ret.Count).ToArray(), 0, ret.Count);
                            _messageReceivedHandler(this, reader);
                        }
                    }
                    else if (ret.MessageType == WebSocketMessageType.Close)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    if (_webSocket.State != WebSocketState.Open)
                    {
                        _clientDisconnectedHandler(this);
                        Console.WriteLine($"Client '{IpAddress}' Disconnected.");
                    }
                    else
                    {
                        Console.WriteLine($"Error: {ex.Message}.");
                    }
                    break;
                }
            }
        }
        /// <summary>
        /// Method responsible for starts the web socket connection.
        /// </summary>
        /// <param name="uri">The uri to start the connection</param>
        public async void StartConnection(Uri uri)
        {
            try
            {
                if (_clientWebSocket.State == WebSocketState.Open)
                {
                    return;
                }
                await _clientWebSocket.ConnectAsync(uri, CancellationToken.None);

                while (IsConnected)
                {
                    var ret = await _clientWebSocket.ReceiveAsync(_buff, CancellationToken.None);

                    if (ret.MessageType == WebSocketMessageType.Text)
                    {
                        var data = _buff.Take(ret.Count).ToArray();

                        var writer = KingBufferWriter.Create();
                        writer.Write(data);

                        var reader = KingBufferReader.Create(writer.BufferData, 0, writer.Length);

                        _messageReceivedHandler(reader);
                    }
                    else if (ret.MessageType == WebSocketMessageType.Binary)
                    {
                        var reader = KingBufferReader.Create(_buff.Take(ret.Count).ToArray(), 0, ret.Count);
                        _messageReceivedHandler(reader);
                    }
                }
            }
            catch (Exception ex)
            {
                if (IsConnected)
                {
                    _disconnectedHandler();
                }
                else
                {
                    Console.WriteLine($"Error: {ex.Message}.");
                }
            }
        }