/// <summary>
        /// This method is used as callback method in _clientSocket's BeginReceive method. It
        /// reveives bytes from socker.
        /// </summary>
        /// <param name="result">Asyncronous call result</param>
        private void ReceiveCallback(IAsyncResult result)
        {
            if (!_running)
            {
                return;
            }

            try
            {
                int bytesRead = -1;

                // Get received bytes count
                bytesRead = _clientSocket.EndReceive(result);

                if (bytesRead > 0)
                {
                    switch (BitConverter.ToUInt16(_buffer, 0))
                    {
                    case PING_REQUEST:
                        _clientSocket.Send(BitConverter.GetBytes(PING_RESPONSE));
                        goto CONT_RECEIVE;

                    case PING_RESPONSE:
                        _running = false;
                        return;
                    }

                    LastReceivedMessageTime = DateTime.Now;

                    // Copy received bytes to a new byte array
                    byte[] receivedBytes = new byte[bytesRead];
                    Array.Copy(_buffer, receivedBytes, bytesRead);

                    // Read messages according to current wire protocol and raise MessageReceived
                    // event for all received messages
                    foreach (IScsMessage message in WireProtocol.CreateMessages(receivedBytes))
                    {
                        OnMessageReceived(message, DateTime.Now);
                    }
                }
                else
                {
                    Logger.Warn(Language.Instance.GetMessageFromKey("CLIENT_DISCONNECTED"));
                    Disconnect();
                }

CONT_RECEIVE:
                // Read more bytes if still running
                if (_running)
                {
                    _clientSocket.BeginReceive(_buffer, 0, _buffer.Length, 0, ReceiveCallback, null);
                }
            }
            catch (Exception)
            {
                Disconnect();
            }
        }
Beispiel #2
0
        private void OnPacketArrival(object sender, CaptureEventArgs e)
        {
            var tcp           = (TcpPacket)Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data).Extract(typeof(TcpPacket));
            var ipv4          = (IPv4Packet)tcp.ParentPacket;
            var sourceAddress = ipv4.SourceAddress.ToString();
            var data          = tcp.PayloadData;

            if (sourceAddress == _serverIp)
            {
                if (IsZlibStartPacket(data))
                {
                    _libFaolanCompression = new LibFaolanCompression();
                }

                if (_libFaolanCompression == null)
                {
                    throw new Exception("_libFaolanCompression == null");
                }

                var decompressedData = _libFaolanCompression.Inflate(data);
                if (decompressedData == null)
                {
                    throw new Exception("decompressedData == null");
                }
                if (decompressedData.Length == 0)
                {
                    return;
                }

                foreach (var cp in _serverWireProtocol.CreateMessages(decompressedData))
                {
                    _capturedPackets.Add(new PacketWrapper(_packetCounter++, ipv4, tcp.SourcePort, tcp.DestinationPort,
                                                           (ConanPacket)cp));
                }
            }
            else if (sourceAddress == _clientIp)
            {
                // Client sometimes sends the zlib start packet, but never actually uses compression (anymore)
                if (IsZlibStartPacket(data))
                {
                    Console.WriteLine("Client send zlib start packet");
                    return;
                }

                foreach (var cp in _clientWireProtocol.CreateMessages(data))
                {
                    _capturedPackets.Add(new PacketWrapper(_packetCounter++, ipv4, tcp.SourcePort, tcp.DestinationPort,
                                                           (ConanPacket)cp));
                }
            }
            else
            {
                throw new Exception("Weird stuff is happening");
            }
        }
Beispiel #3
0
        /// <summary>
        /// This method is used as callback method in _clientSocket's BeginReceive method.
        /// It reveives bytes from socker.
        /// </summary>
        /// <param name="ar">Asyncronous call result</param>
        private void ReceiveCallback(IAsyncResult ar)
        {
            if (!_running)
            {
                return;
            }

            try
            {
                //Get received bytes count
                var bytesRead = _clientSocket.EndReceive(ar);
                if (bytesRead > 0)
                {
                    LastReceivedMessageTime = DateTime.Now;

                    //Copy received bytes to a new byte array
                    var receivedBytes = new byte[bytesRead];
                    Array.Copy(_buffer, 0, receivedBytes, 0, bytesRead);

                    try
                    {
                        //Read messages according to current wire protocol
                        var messages = WireProtocol.CreateMessages(receivedBytes);
                        //Raise MessageReceived event for all received messages
                        foreach (var message in messages)
                        {
                            OnMessageReceived(message);
                        }
                    }
                    catch (SerializationException ex)
                    {
                        System.Diagnostics.Trace.Write($"Error while deserializing message: {ex}");
                    }
                }
                else
                {
                    throw new CommunicationException("Tcp socket is closed");
                }

                //Read more bytes if still running
                if (_running)
                {
                    _clientSocket.BeginReceive(_buffer, 0, _buffer.Length, 0, new AsyncCallback(ReceiveCallback), null);
                }
            }
            catch (Exception exception)
            {
                System.Diagnostics.Trace.Write($"ReceiveCallback: {exception}");
                Disconnect();
            }
        }
        /// <summary>
        /// This method is used as callback method in _clientSocket's BeginReceive method.
        /// It reveives bytes from socker.
        /// </summary>
        /// <param name="ar">Asyncronous call result</param>
        private void ReceiveCallback(IAsyncResult ar)
        {
            if (!_running)
            {
                return;
            }

            // stop timer to receives bytes from socket
            _timerConnect.Stop();

            try
            {
                //Get received bytes count
                var bytesRead = _clientSocket.EndReceive(ar);
                if (bytesRead > 0)
                {
                    LastReceivedMessageTime = DateTime.Now;

                    //Copy received bytes to a new byte array
                    var receivedBytes = new byte[bytesRead];
                    Array.Copy(_buffer, 0, receivedBytes, 0, bytesRead);

                    //Read messages according to current wire protocol
                    var messages = WireProtocol.CreateMessages(receivedBytes);

                    //Raise MessageReceived event for all received messages
                    foreach (var message in messages)
                    {
                        OnMessageReceived(message);
                    }
                }
                else
                {
                    throw new CommunicationException("Tcp socket is closed");
                }

                //Read more bytes if still running
                if (_running)
                {
                    _clientSocket.BeginReceive(_buffer, 0, _buffer.Length, 0, new AsyncCallback(ReceiveCallback), null);
                }
            }
            catch
            {
                Disconnect();
            }

            // Start timer again
            _timerConnect.Start();
        }
        /// <summary>
        /// This method is used as callback method in _clientSocket's BeginReceive method. It
        /// reveives bytes from socker.
        /// </summary>
        /// <param name="ar">Asyncronous call result</param>
        private void ReceiveCallback(IAsyncResult ar)
        {
            if (!_running)
            {
                return;
            }

            try
            {
                var bytesRead = -1;

                // Get received bytes count
                bytesRead = _clientSocket.EndReceive(ar);

                if (bytesRead > 0)
                {
                    LastReceivedMessageTime = DateTime.Now;

                    // Copy received bytes to a new byte array
                    var receivedBytes = new byte[bytesRead];
                    Array.Copy(_buffer, receivedBytes, bytesRead);

                    // Read messages according to current wire protocol
                    var messages = WireProtocol.CreateMessages(receivedBytes);

                    // Raise MessageReceived event for all received messages
                    foreach (var message in messages)
                    {
                        OnMessageReceived(message, DateTime.Now);
                    }
                }
                else
                {
                    Logger.Log.Warn(Language.Instance.GetMessageFromKey("CLIENT_DISCONNECTED"));
                    Disconnect();
                }

                // Read more bytes if still running
                if (_running)
                {
                    _clientSocket.BeginReceive(_buffer, 0, _buffer.Length, 0, new AsyncCallback(ReceiveCallback), null);
                }
            }
            catch
            {
                Disconnect();
            }
        }
        /// <summary>
        /// This method is used as callback method in _clientSocket's BeginReceive method.
        /// It reveives bytes from socker.
        /// </summary>
        /// <param name="ar">Asyncronous call result</param>
        private void ReceiveCallback(IAsyncResult ar)
        {
            if (!_running)
            {
                return;
            }

#if UTILIZA_DESCONEXION_AUTOMATICA
            //int valorAnterior = Interlocked.CompareExchange(ref timeoutFlag, 2, 1);
            if (Interlocked.CompareExchange(ref timeoutFlag, 2, 1) /*valorAnterior*/ != 0)
            {
                //El flag ya ha sido seteado con lo cual nada!!
                return;
            }

            if (timerTimeout != null)
            {
                timerTimeout.Stop();
            }
#endif

            try
            {
                //Get received bytes count
                var bytesRead = _clientSocket.EndReceive(ar);
                if (bytesRead > 0)
                {
                    LastReceivedMessageTime = DateTime.Now;

                    //Copy received bytes to a new byte array
                    var receivedBytes = new byte[bytesRead];
                    Array.Copy(_buffer, 0, receivedBytes, 0, bytesRead);

                    //Read messages according to current wire protocol
                    var messages = WireProtocol.CreateMessages(receivedBytes);

                    //Raise MessageReceived event for all received messages
                    foreach (var message in messages)
                    {
                        OnMessageReceived(message);
                    }
                }
                else
                {
                    throw new CommunicationException("Tcp socket is closed");
                }

                //Read more bytes if still running
                if (_running)
                {
                    _clientSocket.BeginReceive(_buffer, 0, _buffer.Length, 0, new AsyncCallback(ReceiveCallback), null);
#if UTILIZA_DESCONEXION_AUTOMATICA
                    timerTimeout.Start();
#endif
                }
            }
            catch
            {
                Disconnect();
            }
        }
Beispiel #7
0
        /// <summary>
        /// This method is used as callback method in _clientSocket's BeginReceive method.
        /// It reveives bytes from socker.
        /// </summary>
        /// <param name="ar">Asyncronous call result</param>
        private void ReceiveCallback(IAsyncResult ar)
        {
            Socket __clientSocket = ar.AsyncState as Socket;

            if (!_running)
            {
                return;
            }

            try
            {
                //Get received bytes count
                var bytesRead = __clientSocket.EndReceive(ar);
                if (bytesRead > 0)
                {
                    LastReceivedMessageTime = DateTime.Now;

                    //Copy received bytes to a new byte array
                    var receivedBytes = new byte[bytesRead];
                    Array.Copy(_buffer, 0, receivedBytes, 0, bytesRead);

                    //Read messages according to current wire protocol
                    var messages = WireProtocol.CreateMessages(receivedBytes);

                    //Raise MessageReceived event for all received messages
                    foreach (var message in messages)
                    {
                        OnMessageReceived(message);
                    }
                }
                else
                {
                    throw new CommunicationException("Tcp socket is closed");
                }

                //Read more bytes if still running
                if (_running)
                {
                    __clientSocket.BeginReceive(_buffer, 0, _buffer.Length, 0, new AsyncCallback(ReceiveCallback), __clientSocket);
                }
            }
            catch (SocketException ex)
            {
                if ((ex.SocketErrorCode == SocketError.ConnectionReset) ||
                    (ex.SocketErrorCode == SocketError.ConnectionAborted) ||
                    (ex.SocketErrorCode == SocketError.NotConnected) ||
                    (ex.SocketErrorCode == SocketError.Shutdown) ||
                    (ex.SocketErrorCode == SocketError.Disconnecting))
                {
                    Disconnect();
                }
                else
                {
                    OnMessageError(ex);
                }
            }
            catch (CommunicationException ex)
            {
                Disconnect();
            }
            catch (Exception ex)
            {
                OnMessageError(ex);
            }
        }