Ejemplo n.º 1
0
        private void ReceiveThread()
        {
            try
            {
                byte[] buffer = new byte[4];

                while (_processReceiveThread)
                {
                    if (!Receive(buffer))
                    {
                        break;
                    }

                    byte[] packet = new byte[IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, 0))];

                    if (!Receive(packet))
                    {
                        break;
                    }

                    IrssMessage         message = IrssMessage.FromBytes(packet);
                    MessageManagerCombo combo   = new MessageManagerCombo(message, this);

                    if (_messageSink != null)
                    {
                        _messageSink(combo);
                    }
                }
            }
#if TRACE
            catch (SocketException socketException)
            {
                Trace.WriteLine(socketException.ToString());
            }
#else
            catch (SocketException)
            {
            }
#endif
            finally
            {
                if (_connection != null)
                {
                    _connection.Close(100);
                    _connection = null;
                }

                _disconnectCallback(this);
            }
        }
Ejemplo n.º 2
0
        private void ConnectionThread()
        {
            // Outer loop is for reconnection attempts ...
            while (_processConnectionThread)
            {
                _connected = false;

                _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                #region Attempt to connect

                while (_processConnectionThread)
                {
                    try
                    {
                        _serverSocket.Connect(_serverEndpoint);
                        break;
                    }
                    catch (SocketException socketException)
                    {
                        if (!_processConnectionThread)
                        {
                            return;
                        }

                        if (socketException.SocketErrorCode == SocketError.ConnectionRefused)
                        {
                            Thread.Sleep(1000);
                            continue;
                        }

                        if (_commsFailureCallback != null)
                        {
                            _commsFailureCallback(socketException);
                        }
                        else
                        {
                            throw;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!_processConnectionThread)
                        {
                            return;
                        }

                        if (_commsFailureCallback != null)
                        {
                            _commsFailureCallback(ex);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }

                #endregion Attempt to connect

                if (!_processConnectionThread)
                {
                    return;
                }

                _connected = true;

                if (_connectCallback != null)
                {
                    _connectCallback(null);
                }

                #region Read from socket

                try
                {
                    byte[] buffer = new byte[4];

                    // Read data from socket ...
                    while (_processConnectionThread)
                    {
                        if (!Receive(buffer))
                        {
                            break;
                        }

                        byte[] packet = new byte[IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, 0))];

                        if (!Receive(packet))
                        {
                            break;
                        }

                        IrssMessage message = IrssMessage.FromBytes(packet);
                        _messageQueue.Enqueue(message);
                    }

                    if (!_processConnectionThread)
                    {
                        return;
                    }

                    if (_disconnectCallback != null)
                    {
                        _disconnectCallback(null);
                    }
                }
                catch (SocketException socketException)
                {
                    if (!_processConnectionThread)
                    {
                        return;
                    }

                    if (socketException.SocketErrorCode == SocketError.ConnectionReset)
                    {
                        if (_disconnectCallback != null)
                        {
                            _disconnectCallback(null);
                        }
                    }
                    else
                    {
                        if (_commsFailureCallback != null)
                        {
                            _commsFailureCallback(socketException);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (!_processConnectionThread)
                    {
                        return;
                    }

                    if (_commsFailureCallback != null)
                    {
                        _commsFailureCallback(ex);
                    }
                    else
                    {
                        throw;
                    }
                }

                #endregion Read from socket
            }
        }