예제 #1
0
        private bool HandleSendReq(tReqHead pReq)
        {
            if (pReq == null)
            {
                return(false);
            }

            tReqSend pSend = (tReqSend)pReq;

            if (pSend.pSendData == null)
            {
                return(true);
            }
            if (pSend.pSendData.Length <= 0)
            {
                return(true);
            }

            int uFreeSize = mSendBuffer.Length - mWaitSendSize;
            int uSendSize = pSend.pSendData.Length;

            if (uFreeSize > uSendSize)
            {
                Buffer.BlockCopy(pSend.pSendData, 0, mSendBuffer, mWaitSendSize, uSendSize);
                mWaitSendSize += uSendSize;
                return(true);
            }

            return(false);
        }
예제 #2
0
        private void Execute()
        {
            // 没有在连接状态
            if ((mState != eClientState.eClient_STATE_CONNECTED) && (mState != eClientState.eClient_STATE_CONNECTING))
            {
                Thread.Sleep(1);
                return;
            }

            tReqHead pHead = GetFirstReq();

            if (pHead != null)
            {
                if (pHead.uReqType == eReqType.eREQ_TYPE_CONNECT)
                {
                    HandleConnect(pHead);
                    RemoveFirstReq();
                }
                else if (pHead.uReqType == eReqType.eREQ_TYPE_SEND)
                {
                    if (HandleSendReq(pHead))
                    {
                        RemoveFirstReq();
                    }
                }
            }

            DoSend();
            DoRead();

            Thread.Sleep(1);
        }
예제 #3
0
        private void Execute()
        {
            // 没有在连接状态
            try {
                // 可以考虑用ManualResetEvent而不Update
                eClientState state = GetState();
                if ((state != eClientState.eClient_STATE_CONNECTED) && (state != eClientState.eClient_STATE_CONNECTING))
                {
                    Thread.Sleep(1);
                    return;
                }

                /*
                 * 后面可以考虑发送线程和主线程两个队列,直接交换指针
                 * 性能可以更优化
                 */
                tReqHead pHead = GetFirstReq();
                if (pHead != null)
                {
                    if (pHead.uReqType == eReqType.eREQ_TYPE_CONNECT)
                    {
                        HandleConnect(pHead);
                        RemoteFirstReq();
                        pHead.Dispose();
                    }
                    else
                    {
                        if (pHead.uReqType == eReqType.eREQ_TYPE_SEND)
                        {
                            if (HandleSendReq(pHead))
                            {
                                RemoteFirstReq();
                                pHead.Dispose();
                            }
                        }
                    }
                }

                DoSend(state);
                DoRead(state);

                Thread.Sleep(1);
            }
            catch (ThreadAbortException ex) {
#if DEBUG
                // 不做处理
                UnityEngine.Debug.LogError(ex.ToString());
#endif
            }
        }
예제 #4
0
        private tReqHead GetFirstReq()
        {
            tReqHead pReq = null;

            lock (mMutex) {
                if (mQueueReq.Count > 0)
                {
                    LinkedListNode <tReqHead> node = mQueueReq.First;
                    if (node != null)
                    {
                        pReq = node.Value;
                    }
                }
            }

            return(pReq);
        }
예제 #5
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);
                }
            }
        }
예제 #6
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);
                }
            }
        }
예제 #7
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);
                }
            }
        }