Example #1
0
 public bool Init(ConnectCallBack connectCb         = null, DisconnectCallBack disconnectCb = null,
                  RecvMessageCallBack recvMessageCb = null, int netCommandQueueSize         = 0, int netEventQueueSize     = 0
                  , int netSendQueueSize            = 0, int socketConnectTimeoutMSec       = 0, int socketSendTimeoutMSec = 0)
 {
     this.connectCb                = connectCb;
     this.disconnectCb             = disconnectCb;
     this.recvMessageCb            = recvMessageCb;
     this.socketConnectTimeOutMSec = socketConnectTimeoutMSec;
     this.socketSendTimeOutMSec    = socketSendTimeoutMSec;
     this.netCommandPool           = new ConcurrentObjectPool <NetCommand>();
     this.netEventPool             = new ConcurrentObjectPool <NetEvent>();
     this.netSendPool              = new ConcurrentObjectPool <SocketSendStateObject>();
     this.netCommandQueue          = new ConcurrentQueue <NetCommand>();
     this.netEventQueue            = new ConcurrentQueue <NetEvent>();
     this.netSendQueue             = new ConcurrentQueue <SocketSendStateObject>();
     this.netSendThread            = new Thread(NetSendThreadFunc);
     this.netThread                = new Thread(NetThreadFunc);
     return(true);
 }
Example #2
0
 public bool ConnectToHost(String ipAddr, int port, int timeout, DisconnectCallBack callBack)
 {
     try
     {
         channelDataMap.Clear();
         recvBufferWritePos       = 0;
         socket                   = new TcpClient();
         socket.ReceiveBufferSize = socket.SendBufferSize = 100 * 1024 * 1024;
         disconnectCallBack       = callBack;
         IAsyncResult result   = socket.BeginConnect(ipAddr, port, null, null);
         bool         bSuccess = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(timeout));
         if (bSuccess)
         {
             bIsConnected = true;
             socket.EndConnect(result);
             BeginReadNetworkData();
         }
         return(bSuccess);
     }
     catch (Exception)
     {
         return(false);
     }
 }