Beispiel #1
0
        public void AyncConnectCallback(IAsyncResult ar)
        {
            Connection so   = (Connection)ar.AsyncState;
            Socket     sock = so.socket;

            so.Connected = sock.Connected;


            //TODO: Optimize tcp/ip.
            if (NoDelay)
            {
                sock.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
            }

            QSocketEvent socketEvent = new QSocketEvent();

            socketEvent.eventType = QSOCKET_EVENTTYPE.EVENT_CONNECT;
            socketEvent.socket    = so;
            socketEvent.sessionID = so.SessionID;

            try
            {
                sock.EndConnect(ar);
            }
            catch (Exception ex)
            {
                socketEvent.errCode = SOCKET_ERRCODE.ERR_CONNECT;
                so.ExceptionCode    = socketEvent.errCode;
                so.ExceptionMessage = ex.Message;
                AddSocketEvent(socketEvent);
                return;
            }

            if (sock.Connected == true)
            {
                socketEvent.errCode = SOCKET_ERRCODE.SUCCESS;
            }
            else
            {
                socketEvent.errCode = SOCKET_ERRCODE.FAILED;
            }
            so.ExceptionCode = socketEvent.errCode;

            AddSocketEvent(socketEvent);

            if (so.Connected == true)
            {
                try
                {// 开始接收数据
                 //System.IAsyncResult iar;
                 /*iar = */ sock.BeginReceive(so.buffer, 0, Connection.BUFFER_SIZE, 0, new AsyncCallback(AsynchReadCallback), so);
                }
                catch (Exception ex)
                {
                    so.ExceptionCode    = SOCKET_ERRCODE.ERR_RECEIVE_BEGIN_EXCEPTION;
                    so.ExceptionMessage = ex.Message;
                    so.Close(SOCKET_ERRCODE.ERR_RECEIVE_BEGIN_EXCEPTION);
                }
            }
        }
Beispiel #2
0
        public virtual bool OnReceiveData(ref Connection so)
        {
            if (!so.Connected || so.socket == null)
            {
                return(false);
            }
            try
            {
                if (so.msgBuffer.Length < Message.MessageHeadSize)
                {
                    return(false);
                }
                //Get  msg  size
                byte[] bytesBodySize = new byte[2];
                so.msgBuffer.ReadData(ref bytesBodySize, 0);
                byte b = bytesBodySize[0];
                bytesBodySize[0] = bytesBodySize[1];
                bytesBodySize[1] = b;
                int bodySize = BitConverter.ToInt16(bytesBodySize, 0) - 4;
                //完整数据包还没有到达
                if (so.msgBuffer.Length < bodySize + Message.MessageHeadSize)
                {
                    return(false);
                }
                so.msgBuffer.SubmitReadData(ref bytesBodySize);
                byte[] bytesHeadConfirm = new byte[4];
                so.msgBuffer.FectchData(ref bytesHeadConfirm);

                QSocketEvent socketEvent = new QSocketEvent();
                socketEvent.eventType = QSOCKET_EVENTTYPE.EVENT_RECEIVE;
                socketEvent.socket    = so;
                socketEvent.sessionID = so.SessionID;
                socketEvent.errCode   = SOCKET_ERRCODE.SUCCESS;
                //get msg type
                byte[] msgCmd = new byte[4];
                so.msgBuffer.FectchData(ref msgCmd);
                string msgCmdStr = Encoding.ASCII.GetString(msgCmd);
                int.TryParse(msgCmdStr, out socketEvent.msgType);
                //get body
                socketEvent.msgData = new byte[bodySize - 4];
                so.msgBuffer.FectchData(ref socketEvent.msgData);
                AddSocketEvent(socketEvent);
                return(true);
            }
            catch (Exception ex)
            {
                so.ExceptionCode    = SOCKET_ERRCODE.ERR_RECEIVE_DEALDATA_EXCEPTION;
                so.ExceptionMessage = ex.Message;
                //so.Close(SOCKET_ERRCODE.ERR_RECEIVE_DEALDATA_EXCEPTION);
            }

            return(false);
        }
Beispiel #3
0
        public void Close(SOCKET_ERRCODE error)
        {
            if (Connected)
            {
                mIsConnected = false;

                // 关闭Socket
                if (mSocket != null)
                {
                    mSocket.Close();
                    mSocket = null;
                }
            }

            //mSocketManager.OnClose(this);
            this.ExceptionCode = error;
            QSocketEvent socketEvent = new QSocketEvent();

            socketEvent.eventType = QSOCKET_EVENTTYPE.EVENT_CLOSE;
            socketEvent.errCode   = error;
            socketEvent.sessionID = SessionID;
            socketEvent.socket    = this;
            mSocketManager.AddSocketEvent(socketEvent);
        }
Beispiel #4
0
        //进程循环检查函数
        public void Update()
        {
            bool run = true;

            while (run)
            {
                if (CurrentConnection != null)
                {
                    if (CurrentConnection.mSendTime != 0)
                    {
                        if (Time.realtimeSinceStartup >= CurrentConnection.mSendTime)
                        {
                            CurrentConnection.mSendTime = 0;
                        }
                    }
                    else if (CurrentConnection.mSendList.Count > 0)
                    {
                        int maxCount = Mathf.Min(15, CurrentConnection.mSendList.Count);
                        for (int i = 0; i < maxCount; i++)
                        {
                            CurrentConnection.SendMessage(CurrentConnection.mSendList[i], CurrentConnection.mSendList[i].Length);
                        }
                        CurrentConnection.mSendList.RemoveRange(0, maxCount);

                        if (CurrentConnection.mSendList.Count > 0)
                        {
                            CurrentConnection.mSendTime = Time.realtimeSinceStartup + 1.0f;
                        }
                    }
                }

                QSocketEvent socketEvent = null;
                if (mMessageQueue.Count > 0)
                {
                    socketEvent = (QSocketEvent)mMessageQueue.DelItem();
                }
                else
                {
                    run = false;
                }

                if (null != socketEvent)
                {
                    switch (socketEvent.eventType)
                    {
                    case QSOCKET_EVENTTYPE.EVENT_CONNECT:
                        this.OnConnect(socketEvent.socket, socketEvent.errCode);
                        break;

                    case QSOCKET_EVENTTYPE.EVENT_RECEIVE:
                        this.OnReceive(socketEvent.socket, socketEvent.msgType, socketEvent.msgData);
                        break;

                    case QSOCKET_EVENTTYPE.EVENT_CLOSE:
                        this.OnClose(socketEvent.socket, socketEvent.errCode);
                        break;

                    default:
                        SQDebug.Log("Invalidate message; message type:" + socketEvent.eventType);
                        break;
                    }
                }
            }
        }
Beispiel #5
0
 public void AddSocketEvent(QSocketEvent socketEvent)
 {
     mMessageQueue.AddItem(socketEvent);
 }