public TCPConnector(IPacketFormat packetFormat, IPacketHandlerManager packetHandlerManager) : base(packetFormat, packetHandlerManager) { mSocket = null; mNetStream = new NetStream(INetConnector.MAX_SOCKET_BUFFER_SIZE * 2); tempReadPacketLength = 0; tempReadPacketType = 0; tempReadPacketData = null; mReadCompleteCallback = new AsyncCallback(ReadComplete); mSendCompleteCallback = new AsyncCallback(SendComplete); mSendThread = new AsyncThread(SendLogic); mSendThread.Start(); }
public override void Connect(string address, int port) { SetConnectStatus(ConnectionStatus.CONNECTING); base.Connect(address, port); mSocket = new TcpClient(); AsyncThread connectThread = new AsyncThread((thread) => { try { mSocket.NoDelay = true; // set the nodelay mark. mSocket.Connect(mRemoteHost.GetAddress(), mRemoteHost.GetPort()); mSocket.GetStream().BeginRead(mNetStream.AsyncPipeIn, 0, INetConnector.MAX_SOCKET_BUFFER_SIZE, mReadCompleteCallback, this); SetConnectStatus(ConnectionStatus.CONNECTED); } catch (Exception e) { LoggerSystem.Instance.Error(e.Message); SetConnectStatus(ConnectionStatus.ERROR); } CallbackConnected(IsConnected()); }); connectThread.Start(); }