Exemple #1
0
 private void SendCallback(IAsyncResult ar)
 {
     try
     {
         // Retrieve the socket from the state object.
         FxTcpClientSocket pClientSocket = (FxTcpClientSocket)ar.AsyncState;
         // Complete sending the data to the remote device.
         int bytesSent = m_hSocket.EndSend(ar);
         OnSend((UInt32)bytesSent);
     }
     catch (SocketException e)
     {
         SNetEvent pEvent = new SNetEvent();
         pEvent.eType   = ENetEvtType.NETEVT_ERROR;
         pEvent.dwValue = (UInt32)e.SocketErrorCode;
         FxNetModule.Instance().PushNetEvent(this, pEvent);
         Disconnect();
         return;
     }
     catch (Exception e)
     {
         SNetEvent pEvent = new SNetEvent();
         pEvent.eType = ENetEvtType.NETEVT_ERROR;
         //pEvent.dwValue = (UInt32)e.HResult;
         FxNetModule.Instance().PushNetEvent(this, pEvent);
         Disconnect();
         return;
     }
 }
Exemple #2
0
 protected void getIPType(String serverIp, int serverPorts, out String newServerIp, out AddressFamily mIPType)
 {
     mIPType     = AddressFamily.InterNetwork;
     newServerIp = serverIp;
     try
     {
         string mIPv6 = GetIPv6(serverIp, serverPorts.ToString());
         if (!string.IsNullOrEmpty(mIPv6))
         {
             string[] m_StrTemp = System.Text.RegularExpressions.Regex.Split(mIPv6, "&&");
             if (m_StrTemp != null && m_StrTemp.Length >= 2)
             {
                 string IPType = m_StrTemp[1];
                 if (IPType == "ipv6")
                 {
                     newServerIp = m_StrTemp[0];
                     mIPType     = AddressFamily.InterNetworkV6;
                 }
             }
         }
     }
     catch (Exception e)
     {
         SNetEvent pEvent = new SNetEvent();
         pEvent.eType = ENetEvtType.NETEVT_ERROR;
         //pEvent.dwValue = (UInt32)e.HResult;
         FxNetModule.Instance().PushNetEvent(this, pEvent);
         Disconnect();
         return;
     }
 }
Exemple #3
0
 public override void AsynReceive()
 {
     try
     {
         // Create the state object.
         // Begin receiving the data from the remote device.
         m_hSocket.BeginReceive(m_pDataBuffer, 0, (int)m_pRecvBuffer.GetFreeLength(), 0, new AsyncCallback(ReceiveCallback), this);
     }
     catch (SocketException e)
     {
         SNetEvent pEvent = new SNetEvent();
         pEvent.eType   = ENetEvtType.NETEVT_ERROR;
         pEvent.dwValue = (UInt32)e.SocketErrorCode;
         FxNetModule.Instance().PushNetEvent(this, pEvent);
         Disconnect();
         return;
     }
     catch (Exception e)
     {
         SNetEvent pEvent = new SNetEvent();
         pEvent.eType = ENetEvtType.NETEVT_ERROR;
         //pEvent.dwValue = (UInt32)e.HResult;
         FxNetModule.Instance().PushNetEvent(this, pEvent);
         Disconnect();
         return;
     }
 }
Exemple #4
0
        public override void OnConnect()
        {
            SNetEvent pEvent = new SNetEvent();

            pEvent.eType = ENetEvtType.NETEVT_ESTABLISH;
            FxNetModule.Instance().PushNetEvent(this, pEvent);
        }
Exemple #5
0
 public override void OnRecv(byte[] buffer, UInt32 bytesRead)
 {
     m_pRecvBuffer.PushData(buffer, bytesRead);
     //判断包长 达到一定长度 将其加入处理队列
     while (true)
     {
         Int32 iLength = GetDataHeader().ParsePacket(m_pRecvBuffer.GetData(), m_pRecvBuffer.GetUsedLength());
         if (iLength == 0)
         {
             break;
         }
         if (iLength < 0)
         {
             SNetEvent pEvent = new SNetEvent();
             pEvent.eType = ENetEvtType.NETEVT_ERROR;
             FxNetModule.Instance().PushNetEvent(this, pEvent);
             Disconnect();
             return;
         }
         else if (iLength > 0)
         {
             if (m_pRecvBuffer.GetUsedLength() < iLength)
             {
                 break;
             }
             m_pSessionBuffer.PushData(m_pRecvBuffer.GetData(), (UInt32)iLength);
             m_pRecvBuffer.PopData((UInt32)iLength);
             SNetEvent pEvent = new SNetEvent();
             pEvent.eType   = ENetEvtType.NETEVT_RECV;
             pEvent.dwValue = (UInt32)iLength;
             FxNetModule.Instance().PushNetEvent(this, pEvent);
         }
     }
 }
Exemple #6
0
        public override void Connect(string szIp, int nPort)
        {
            Disconnect();
            String        newServerIp    = "";
            AddressFamily pAddressFamily = AddressFamily.InterNetwork;

            getIPType(szIp, nPort, out newServerIp, out pAddressFamily);
            IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(newServerIp), nPort);

            CreateSocket(pAddressFamily);
            try
            {
                m_hSocket.BeginConnect(ipe, new AsyncCallback(ConnectCallback), this);
            }
            catch (SocketException e)
            {
                SNetEvent pEvent = new SNetEvent();
                pEvent.eType   = ENetEvtType.NETEVT_ERROR;
                pEvent.dwValue = (UInt32)e.SocketErrorCode;
                FxNetModule.Instance().PushNetEvent(this, pEvent);
                Disconnect();
                return;
            }
            catch (Exception e)
            {
                SNetEvent pEvent = new SNetEvent();
                pEvent.eType = ENetEvtType.NETEVT_ERROR;
                //pEvent.dwValue = (UInt32)e.HResult;
                FxNetModule.Instance().PushNetEvent(this, pEvent);
                Disconnect();
            }
        }
Exemple #7
0
        public override void OnRecv(byte[] buffer, UInt32 bytesRead)
        {
            m_pSessionBuffer.PushData(buffer, bytesRead);
            SNetEvent pEvent = new SNetEvent();

            pEvent.eType   = ENetEvtType.NETEVT_RECV;
            pEvent.dwValue = bytesRead;
            FxNetModule.Instance().PushNetEvent(this, pEvent);
        }
Exemple #8
0
        void OnError(object pSender, WebSocketSharp.ErrorEventArgs pEventArgs)
        {
            m_szError = pEventArgs.Message;
            SNetEvent pEvent = new SNetEvent();

            pEvent.eType = ENetEvtType.NETEVT_ERROR;
            FxNetModule.Instance().PushNetEvent(this, pEvent);
            Disconnect();
        }
Exemple #9
0
        public override void Disconnect()
        {
            if (m_hSocket != null && m_hSocket.Connected)
            {
                m_hSocket.Disconnect(false);
                m_hSocket = null;

                SNetEvent pEvent = new SNetEvent();
                pEvent.eType = ENetEvtType.NETEVT_TERMINATE;
                FxNetModule.Instance().PushNetEvent(this, pEvent);
                IoThread.Instance().DelConnectSocket(this);
            }
        }
Exemple #10
0
        public override void Disconnect()
        {
            if (m_hSocket != null && m_bIsConnected)
            {
                WebSocketSharp.WebSocket pSock = m_hSocket;

                m_bIsConnected = false;
                m_hSocket      = null;
                pSock.Close();

                SNetEvent pEvent = new SNetEvent();
                pEvent.eType = ENetEvtType.NETEVT_TERMINATE;
                FxNetModule.Instance().PushNetEvent(this, pEvent);
                IoThread.Instance().DelConnectSocket(this);
            }
        }
Exemple #11
0
 private void ReceiveCallback(IAsyncResult ar)
 {
     try
     {
         // Retrieve the state object and the client socket
         // from the asynchronous state object.
         //FxClientSocket pSocket = (FxClientSocket)ar.AsyncState;
         // Read data from the remote device.
         int bytesRead = m_hSocket.EndReceive(ar);
         if (bytesRead < 0)
         {
             SNetEvent pEvent = new SNetEvent();
             pEvent.eType = ENetEvtType.NETEVT_ERROR;
             FxNetModule.Instance().PushNetEvent(this, pEvent);
             Disconnect();
             return;
         }
         if (bytesRead == 0)
         {
             Disconnect();
             return;
         }
         // There might be more data, so store the data received so far.
         OnRecv(m_pDataBuffer, (UInt32)bytesRead);
         // Get the rest of the data.
         m_hSocket.BeginReceive(m_pDataBuffer, 0, (int)m_pRecvBuffer.GetFreeLength(), 0, new AsyncCallback(ReceiveCallback), this);
     }
     catch (SocketException e)
     {
         SNetEvent pEvent = new SNetEvent();
         pEvent.eType   = ENetEvtType.NETEVT_ERROR;
         pEvent.dwValue = (UInt32)e.SocketErrorCode;
         FxNetModule.Instance().PushNetEvent(this, pEvent);
         Disconnect();
         return;
     }
     catch (Exception e)
     {
         SNetEvent pEvent = new SNetEvent();
         pEvent.eType = ENetEvtType.NETEVT_ERROR;
         //pEvent.dwValue = (UInt32)e.HResult;
         FxNetModule.Instance().PushNetEvent(this, pEvent);
         Disconnect();
         return;
     }
 }
Exemple #12
0
        private void ConnectCallback(IAsyncResult ar)
        {
            try
            {
                FxTcpClientSocket pClientSocket = (FxTcpClientSocket)ar.AsyncState;
                m_hSocket.EndConnect(ar);

                OnConnect();
            }
            catch (SocketException e)
            {
                SNetEvent pEvent = new SNetEvent();
                pEvent.eType   = ENetEvtType.NETEVT_ERROR;
                pEvent.dwValue = (UInt32)e.SocketErrorCode;
                FxNetModule.Instance().PushNetEvent(this, pEvent);
                Disconnect();
            }
        }
Exemple #13
0
        public override void ProcEvent(SNetEvent pEvent)
        {
            switch (pEvent.eType)
            {
            case ENetEvtType.NETEVT_ESTABLISH:
            {
                m_pSession.OnConnect();
                IoThread.Instance().AddConnectSocket(this);
            }
            break;

            case ENetEvtType.NETEVT_RECV:
            {
                m_pSession.OnRecv(m_pSessionBuffer.GetData(), pEvent.dwValue);
                m_pSessionBuffer.PopData(pEvent.dwValue);
            }
            break;

            case ENetEvtType.NETEVT_ERROR:
            {
                m_pSession.OnError(pEvent.dwValue);
            }
            break;

            case ENetEvtType.NETEVT_TERMINATE:
            {
                m_pSession.OnClose();
                SNetEvent oEvent = new SNetEvent();
                oEvent.eType = ENetEvtType.NETEVT_RELEASE;
                FxNetModule.Instance().PushNetEvent(this, oEvent);
            }
            break;

            case ENetEvtType.NETEVT_RELEASE:
                m_pSession.OnDestroy();
                break;

            default:
                break;
            }
        }