Exemplo n.º 1
0
        internal void Handle(NetRawData rdata)
        {
            //if (heartbeatCode == rdata.proto)
            //{
            //    return; // lua no need to handle heartbeat message
            //}

            if (NetConst.IsPrintLog)
            {
                Debug.Log("network log receive : S->C msgid = " + rdata.msgName + ", socketId=" + rdata.socketId + ", len: " + rdata.total);
            }

            GEngine.Net.IProtocolHandler handler;
            if (protocolHandlers.TryGetValue(rdata.msgName, out handler))
            {
                try
                {
                    handler.Handle(rdata.stream);
                }
                catch (Exception e)
                {
                    Debug.LogErrorFormat("Error S->C msgid={0}, socketId={1}, error={2}", rdata.msgName, rdata.socketId, e);
                }
            }
        }
Exemplo n.º 2
0
        private void SocketConnectAsyncCallBack(IAsyncResult asyncResult)
        {
            Debug.Log(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff") + " SocketConnectAsyncCallBack start");
            try
            {
                Socket socket = asyncResult.AsyncState as Socket;
                if (socket != null)
                {
                    socket.EndConnect(asyncResult);
                    //Debug.Log(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff") + " SocketConnectAsyncCallBack socket ok");
                    if (NetConst.IsPrintLog)
                    {
                        Debug.Log(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff") + " ConnectSocket ok ep=" + endp + "  socketId" + sid);
                    }
                    tempSocketConnectOKFlag = true;
                    needReconnectFlag       = false;

                    status = SocketStatus.Ok;
                    rdata  = DataObjectPool.Factory <NetRawData>();
                    UpdateLatestHeartBeatActiveTime();
                    UpdateLastSendHeartBeatMessageTime();
                    //sConnect.Dispatch(sid);

                    if (!hasConnectedBefore)
                    {
                        FireNetEvent(NetClientEventType.Connected);
                        hasConnectedBefore = true;
                    }
                    else
                    {
                        FireNetEvent(NetClientEventType.ReConnected);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.Log(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff") + " SocketConnectAsyncCallBack error: " + e);
            }
        }
Exemplo n.º 3
0
        private bool ReadData()
        {
            bool gotData = false;

            try
            {
                lock (this)
                {
                    int avail = socket.Available;
                    while (avail > 0)
                    {
                        UpdateLatestHeartBeatActiveTime();
                        gotData = true;
                        //lastSendTime = -1;
                        //Debug.Log("ReadData: " + avail);
                        //if (NetConst.IsPrintLog) Debug.Log(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff") + " ReadData available: " + avail);

                        if (rdata.total > NetConst.ReceiveBufferSize && Application.isEditor)
                        {
                            Debug.LogError("Extend net receive buffer size: " + NetConst.ReceiveBufferSize + ", total: " + rdata.total);
                        }

                        int _bufferLength = rdata.buffer.Length;
                        if (rdata.total >= _bufferLength)
                        {
                            Array.Resize <byte>(ref rdata.buffer, rdata.total + 100);
                        }

                        int byt = socket.Receive(rdata.buffer, rdata.read, rdata.total - rdata.read, SocketFlags.None);

                        avail      -= byt;
                        rdata.read += byt;
                        if (rdata.read == rdata.total)
                        {
                            if (rdata.state == ReadState.Header)
                            {
                                int len              = BitConverter.ToInt32(rdata.buffer, 0);
                                int msgNameLength    = BitConverter.ToInt32(rdata.buffer, 4);
                                int msgContentLength = len - 8 - msgNameLength;
                                Debug.Log("len=" + len + ", msgNameLength=" + msgNameLength + ", msgContentLength=" + msgContentLength);

                                rdata.msgNameLen = msgNameLength;
                                rdata.read       = 0;
                                rdata.total      = len - 8; // minus 8 bytes head length
                                rdata.state      = ReadState.Content;
                                rdata.socketId   = sid;
                            }
                            else
                            {
                                rdata.stream.Seek(0, SeekOrigin.Begin);
                                rdata.stream.SetLength(0);
                                string msgName = Encoding.UTF8.GetString(rdata.buffer, 0, rdata.msgNameLen); // BitConverter.ToString(rdata.buffer, 0, rdata.msgNameLen);
                                Debug.Log("msgName=" + msgName);

                                rdata.msgName = msgName;
                                if (rdata.total - rdata.msgNameLen > 0)
                                {
                                    rdata.stream.Write(rdata.buffer, rdata.msgNameLen, rdata.total - rdata.msgNameLen);
                                    rdata.stream.SetLength(rdata.total - rdata.msgNameLen);
                                }

                                Debug.Log("======================== " + (rdata.total - rdata.msgNameLen));

                                dataAsyncPool.Enqueue(rdata);

                                rdata          = DataObjectPool.Factory <NetRawData>();
                                rdata.socketId = sid;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(ex);
                MarkReconnect();
            }
            return(gotData);
        }