Ejemplo n.º 1
0
        private void AddConnectReq(string pRemoteIp, int uRemotePort, int mTimeOut)
        {
            tReqConnect pReq = new tReqConnect(pRemoteIp, uRemotePort, mTimeOut);

            lock (mMutex) {
                mState = eClientState.eClient_STATE_CONNECTING;
                LinkedListNode <tReqHead> node = new LinkedListNode <tReqHead>(pReq);
                mQueueReq.AddLast(node);
            }
        }
Ejemplo n.º 2
0
        private void HandleConnect(tReqHead pReq)
        {
            if (pReq == null)
            {
                return;
            }
            tReqConnect pConnect = (tReqConnect)pReq;
            string      ipv4     = pConnect.szRemoteIp;
            int         port     = pConnect.uRemotePort;

            //---------------use ipv6 to connect to gameserver-------------
            String        newServerIp      = "";
            AddressFamily newAddressFamily = AddressFamily.InterNetwork;

            IPv6SupportMidleware.getIPType(ipv4, port.ToString(), out newServerIp, out newAddressFamily);
            if (!string.IsNullOrEmpty(newServerIp))
            {
                ipv4 = newServerIp;
            }
            //-------------------------------------------------------------
            mSocket = new Socket(newAddressFamily, SocketType.Stream, ProtocolType.Tcp);
            if (mSocket == null)
            {
                // not used
                SetClientState(eClientState.eClient_STATE_CONNECT_FAIL);
                return;
            }

            //	mSocketList[0] = mSocket;

            mSocket.SendTimeout    = 0;
            mSocket.ReceiveTimeout = 0;
            //mSocket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 0);
            //mSocket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);

            bool non = pConnect.timeOut >= 0;

            /*
             * if (non)
             *  mSocket.Blocking = false;
             * else
             *  mSocket.Blocking = true;
             */
            if (non)
            {
                // set waiting = true
                mWaiting.Reset();

                AsyncCallback callBack = new AsyncCallback(OnConnectCallBack);
                mSocket.BeginConnect(ipv4, port, callBack, mSocket);

                //阻塞当前线程,直到当前的 WaitHandle 收到信号
                //返回值:如果当前实例收到信号,则为 true;否则为 false。
                if (mWaiting.WaitOne(pConnect.timeOut))
                {
                    if (mSocket.Connected && mSocket.Poll(0, SelectMode.SelectWrite))
                    {
                        SetClientState(eClientState.eClient_STATE_CONNECTED);
                    }
                    else
                    {
                        CloseSocket();
                        SetClientState(eClientState.eClient_STATE_CONNECT_FAIL);
                    }
                }
                else
                {
                    if (mSocket.Connected && mSocket.Poll(0, SelectMode.SelectWrite))
                    {
                        SetClientState(eClientState.eClient_STATE_CONNECTED);
                    }
                    else
                    {
                        CloseSocket();
                        SetClientState(eClientState.eClient_STATE_CONNECT_FAIL);
                    }
                }
            }
            else
            {
                //阻塞式
                // yi zhi waiting...
                try
                {
                    mSocket.Connect(ipv4, port);
                    if (mSocket.Connected && mSocket.Poll(0, SelectMode.SelectWrite))
                    {
                        SetClientState(eClientState.eClient_STATE_CONNECTED);
                    }
                    else
                    {
                        CloseSocket();
                        SetClientState(eClientState.eClient_STATE_CONNECT_FAIL);
                    }
                }
                catch (Exception e)
                {
                    CloseSocket();
                    SetClientState(eClientState.eClient_STATE_CONNECT_FAIL);
                }
            }
        }
Ejemplo n.º 3
0
        private void HandleConnect(tReqHead pReq)
        {
            if (pReq == null)
            {
                return;
            }
            tReqConnect pConnect = (tReqConnect)pReq;

            mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            if (mSocket == null)
            {
                // not used
                SetClientState(eClientState.eClient_STATE_CONNECT_FAIL);
                return;
            }

            //	mSocketList[0] = mSocket;

            mSocket.SendTimeout    = 0;
            mSocket.ReceiveTimeout = 0;
            //mSocket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 0);
            //mSocket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);

            bool non = pConnect.timeOut >= 0;

            /*
             * if (non)
             *      mSocket.Blocking = false;
             * else
             *      mSocket.Blocking = true;
             */
            if (non)
            {
                // set waiting = true
                mWaiting.Reset();

                AsyncCallback callBack = new AsyncCallback(OnConnectCallBack);
                mSocket.BeginConnect(pConnect.szRemoteIp, pConnect.uRemotePort, callBack, mSocket);


                if (mWaiting.WaitOne(pConnect.timeOut))
                {
                    if (mSocket.Connected && mSocket.Poll(0, SelectMode.SelectWrite))
                    {
                        SetClientState(eClientState.eClient_STATE_CONNECTED);
                    }
                    else
                    {
                        CloseSocket();
                        SetClientState(eClientState.eClient_STATE_CONNECT_FAIL);
                    }
                }
                else
                {
                    if (mSocket.Connected && mSocket.Poll(0, SelectMode.SelectWrite))
                    {
                        SetClientState(eClientState.eClient_STATE_CONNECTED);
                    }
                    else
                    {
                        CloseSocket();
                        SetClientState(eClientState.eClient_STATE_CONNECT_FAIL);
                    }
                }
            }
            else
            {
                // yi zhi waiting...
                try {
                    mSocket.Connect(pConnect.szRemoteIp, pConnect.uRemotePort);
                    if (mSocket.Connected && mSocket.Poll(0, SelectMode.SelectWrite))
                    {
                        SetClientState(eClientState.eClient_STATE_CONNECTED);
                    }
                    else
                    {
                        CloseSocket();
                        SetClientState(eClientState.eClient_STATE_CONNECT_FAIL);
                    }
                } catch (Exception e)
                {
                    CloseSocket();
                    SetClientState(eClientState.eClient_STATE_CONNECT_FAIL);

                    Console.WriteLine(e.Message);
                }
            }
        }
Ejemplo n.º 4
0
        private void HandleConnect(tReqHead pReq)
        {
            if (pReq == null)
            {
                return;
            }
            tReqConnect pConnect = (tReqConnect)pReq;

            m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            if (m_Socket == null)
            {
                // not used
                SetClientState(eClientState.eClient_STATE_CONNECT_FAIL);
                return;
            }

            // 使用非延迟发送
            m_Socket.NoDelay = true;

            m_Socket.SendTimeout    = 0;
            m_Socket.ReceiveTimeout = 0;

            bool non = pConnect.timeOut >= 0;

            if (non)
            {
                // set waiting = true
                m_Waiting.Reset();

                AsyncCallback callBack = new AsyncCallback(OnConnectCallBack);
                try {
                    m_Socket.BeginConnect(pConnect.szRemoteIp, pConnect.uRemotePort, callBack, m_Socket);
                }
                catch (Exception e) {
                    ProcessException(e, eClientState.eClient_STATE_CONNECT_FAIL);
                    return;
                }

                if (m_Waiting.WaitOne(pConnect.timeOut))
                {
                    if (m_Socket.Connected && m_Socket.Poll(0, SelectMode.SelectWrite))
                    {
                        SetClientState(eClientState.eClient_STATE_CONNECTED);
                    }
                    else
                    {
                        CloseSocket();
                        SetClientState(eClientState.eClient_STATE_CONNECT_FAIL);
                    }
                }
                else
                {
                    if (m_Socket.Connected && m_Socket.Poll(0, SelectMode.SelectWrite))
                    {
                        SetClientState(eClientState.eClient_STATE_CONNECTED);
                    }
                    else
                    {
                        CloseSocket();
                        SetClientState(eClientState.eClient_STATE_CONNECT_FAIL);
                    }
                }
            }
            else
            {
                // yi zhi waiting...
                try {
                    m_Socket.Connect(pConnect.szRemoteIp, pConnect.uRemotePort);
                    if (m_Socket.Connected && m_Socket.Poll(0, SelectMode.SelectWrite))
                    {
                        SetClientState(eClientState.eClient_STATE_CONNECTED);
                    }
                    else
                    {
                        CloseSocket();
                        SetClientState(eClientState.eClient_STATE_CONNECT_FAIL);
                    }
                }
                catch (Exception e) {
                    ProcessException(e, eClientState.eClient_STATE_CONNECT_FAIL);
                }
            }
        }