/// <summary> /// stop connection thread and tcp connection if established /// </summary> public void Stop() { if(m_ConnectionState == State.Disconnected) { if (m_Client != null) { //if (m_Client.Connected) { if (m_NetworkStream != null) { m_StreamOut.Close(); m_StreamIn.Close(); m_NetworkStream.Close(); m_Reader.Stop(); m_Reader = null; m_NetworkStream = null; m_StreamOut = null; m_StreamIn = null; } m_Client.Close(); m_Client = null; } } } else if (m_ConnectionState != State.Disconnecting) { lock (this) { m_ConnectionState = State.Disconnecting; if (m_ConnectionThread != null) { m_bContinue = false; m_ConnectionThread.Abort(); m_ConnectionThread.Join(); m_ConnectionThread = null; } if (m_Reader != null) { SetEventFIXMessageHandler(false); } m_ConnectionState = State.Disconnected; } } }
/// <summary> /// connection to tcp server /// </summary> public void Connect() { if(m_ConnectionState != State.Disconnecting) { while (m_bContinue) { try { m_ConnectionState = State.Connecting; //attempt tcp connect Socket soc = m_Client.Client; AddressFamily fam = soc.AddressFamily; ProtocolType tpye = soc.ProtocolType; m_Client.Connect(m_sIPAddress, m_nPort); m_bContinue = false; m_ConnectionState = State.Connected; //raise connection event if(ConnectionEvent!=null) ConnectionEvent(this,new ConnectionEventArgs(this.m_ConnectionState)); //get the io data streams m_NetworkStream = m_Client.GetStream(); m_StreamOut = new StreamWriter(m_NetworkStream); m_StreamIn = new StreamReader(m_NetworkStream,Encoding.ASCII,true,1024); //set up the stream reader m_Reader = new ClientReader(m_StreamIn); //set up event handler to recieve data events from reader class on packet arrival from tcp server SetEventFIXMessageHandler(true); //start thresad to read data from server m_Reader.Start(); } catch(Exception) { //log SetEventFIXMessageHandler(false); Thread.Sleep(m_nTimeOut); } } } m_ConnectionThread = null; }