Exemplo n.º 1
0
 public bool Connect()
 {
     DebugHelper.LogInfo("Begin TcpClient Connect");
     this.mStructCopyer        = new StructCopyer();
     this.mIsRunning           = false;
     this.mRecvLock            = new object();
     this.mRecvingMsgQueue     = new Queue <DataPacket>();
     this.mRecvWaitingMsgQueue = new Queue <DataPacket>();
     try
     {
         this.mIsRunning             = true;
         this.mTcpClient             = new TcpClient(mHost, mPort);
         this.mTcpClient.NoDelay     = true;
         this.mTcpClient.SendTimeout = 5;
         this.mStream = mTcpClient.GetStream();
         OnRegister();
         this.mRecvThread = new Thread(new ThreadStart(ReceiveMessage));
         this.mRecvThread.Start();
         this.mRecvThread.IsBackground = true;
         DebugHelper.LogInfo("TcpClient Connect Suceess");
     }
     catch (System.Exception)
     {
         this.mIsRunning = false;
         OnDisconnected(DisType.None, "TcpClient Connect--->>Fail");
     }
     return(mIsRunning);
 }
Exemplo n.º 2
0
 public void InitializeSocket(string ip, int port, bool isDomain = false)
 {
     mStructCopyer   = new StructCopyer();
     mAsyncTcpClient = new AsyncTcpClient(ip, port, isDomain);
     mAsyncTcpClient.ConnectedEventArgs    += OnConnectedEventArgs;
     mAsyncTcpClient.DisconnectedEventArgs += OnDisconnectedEventArgs;
     mAsyncTcpClient.ExceptionEventArgs    += OnExceptionEventArgs;
     mAsyncTcpClient.DataPacketReceived    += OnDataPacketReceived;
     mAsyncTcpClient.Connect();
 }
Exemplo n.º 3
0
 public byte[] ToBytes(StructCopyer mStructCopyer)
 {
     byte[] totalByte = new byte[Size];
     Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Header.HeadSign)), 0, totalByte, 0, 4);
     Buffer.BlockCopy(new byte[1] {
         Header.Crc
     }, 0, totalByte, 4, 1);
     Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Header.DataSize)), 0, totalByte, 5, 2);
     Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Header.Command)), 0, totalByte, 7, 2);
     //Buffer.BlockCopy(mStructCopyer.StructToBytes(Header), 0, totalByte, 0, PacketConst.HeaderLen);
     Buffer.BlockCopy(Data, 0, totalByte, PacketConst.HeaderLen, Data.Length);
     return(totalByte);
 }
Exemplo n.º 4
0
 public void InitializeSocket(string ip, int port)
 {
     mStructCopyer = new StructCopyer();
     mSocketClient = new SocketClient(ip, port);
     mSocketClient.Connect();
 }