Exemple #1
0
        private void SendThread()
        {
            while (!IsClose && !callCloseYet)
            {
                //var tn = Thread.CurrentThread.ManagedThreadId;
                //Debug.LogError("ThreadN: "+tn);
                //var st = Util.GetTimeNow();

                signal.WaitOne();
                if (IsClose)
                {
                    break;
                }
                MsgBuffer mb = null;
                lock (msgBuffer)
                {
                    if (msgBuffer.Count > 0)
                    {
                        mb = msgBuffer.Dequeue();
                    }
                }
                if (mb != null)
                {
                    try
                    {
                        client.GetStream().Write(mb.buffer, mb.position, mb.Size);

                        /*
                         * ml.queueInLoop(() =>
                         * {
                         *  Bundle.ReturnBundle(mb.bundle);
                         * });
                         */
                    }
                    catch (Exception exception)
                    {
                        callCloseYet = true;
                        ml.queueInLoop(() => {
                            Debug.LogError(exception.ToString());
                            Close();
                        });
                    }
                }

                lock (msgBuffer)
                {
                    if (msgBuffer.Count <= 0)
                    {
                        signal.Reset();
                    }
                }
                //var et = Util.GetTimeNow();
                //Debug.LogError("DiffTime: "+(et-st));
            }
        }
Exemple #2
0
        //线程安全方法
        void Close()
        {
            if (IsClose)
            {
                return;
            }
            Debug.LogError("CloseRemoteClient");
            if (client != null)
            {
                try
                {
                    if (client != null && client.Connected)
                    {
                        client.Close();
                    }
                } catch (Exception exception)
                {
                    Debug.LogError(exception.ToString());
                }
            }
            client  = null;
            IsClose = true;
            signal.Set();

            if (evtHandler != null)
            {
                var eh = evtHandler;
                ml.queueInLoop(() =>
                {
                    SendEvt(eh, RemoteClientEvent.Close);
                });
            }
            evtHandler = null;
            msgHandler = null;
        }
Exemple #3
0
    private void StartReceive()
    {
        var udpPort = new IPEndPoint(IPAddress.Any, 0);

        try
        {
            var bytes = client.Receive(ref udpPort);
            ReceiveData(bytes);
        }
        catch (Exception exp)
        {
            callCloseYet = true;
            ml.queueInLoop(() =>
            {
                Debug.LogError(exp.ToString());
                Close();
            });
        }
    }
Exemple #4
0
    //线程安全方法
    void Close()   //断开连接
    {
        if (IsClose)
        {
            return;
        }
        Debug.LogError("CloseRemoteClient");
        if (client != null)
        {
            try
            {
                if (client != null && client.Connected)
                {
                    client.Close();   //断开TCP连接
                }
            }
            catch (Exception exception)
            {
                Debug.LogError(exception.ToString());
            }
        }
        client  = null;
        IsClose = true;
        signal.Set();

        if (evtHandler != null)
        {
            ml.queueInLoop(() =>
            {
                //这里把该匿名函数加入到MainThreadLoop的queueInLoop中进行处理,主要是需要在MonoBehaviour的主线程中对消息处理
                evtHandler(RemoteClientEvent.Close);
            });
        }
        evtHandler = null;
        msgHandler = null;
    }
Exemple #5
0
    //该类的核心就是process函数,一种基于if else的状态机机制,而且if else具有优先级的特性,可以继续下去
    //datas是整个缓冲区的大小,length是其中数据的长度,flowHandler是可能的处理函数
    public void process(byte[] datas, uint length, System.Collections.Generic.Dictionary <uint, MessageHandler> flowHandler)
    {
        //Debug.LogError("process receive Data " + length + " state " + state+" expect "+expectSize);
        uint totallen = 0;  //这里把totallen设置为局部变量就表明

        while (length > 0 && expectSize > 0)
        {
            if (state == READ_STATE.READ_STATE_MSGLEN)                                   //读取msg长度的模式
            {
                if (length >= expectSize)                                                //如果缓冲区数据长度大于可读取长度
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize); //够长就直接拷贝数据Copy(Array sourceArray, long sourceIndex, Array destinationArray, long destinationIndex, long length);
                    totallen    += expectSize;                                           //totallen是记录从网络上读取缓冲区的长度
                    stream.wpos += (int)expectSize;                                      //stream.wpos是写入目标缓冲区的偏移量
                    length      -= expectSize;                                           //length是网络数据缓冲区的剩余长度

                    msglen = stream.readUint16();                                        //读取msglen
                    stream.clear();                                                      //清空本地缓冲区

                    state      = READ_STATE.READ_STATE_FLOWID;                           //改变接下来要读取的状态
                    expectSize = 1;                                                      //接下来要读取的数据长度大小
                }
                else                                                                     //如果不够读取的话,先读取length长度的数据,更新wpos和expectSize的大小,等待下一次读取
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                    stream.wpos += (int)length;
                    expectSize  -= length;
                    break;
                }
            }
            else if (state == READ_STATE.READ_STATE_FLOWID)
            {
                if (length >= expectSize)
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                    totallen    += expectSize;
                    stream.wpos += (int)expectSize;
                    length      -= expectSize;

                    flowId = stream.readUint8();
                    stream.clear();

                    state      = READ_STATE.READ_STATE_MODULEID;
                    expectSize = 1;
                }
                else
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                    stream.wpos += (int)length;
                    expectSize  -= length;
                    break;
                }
            }
            else if (state == READ_STATE.READ_STATE_MODULEID)
            {
                if (length >= expectSize)
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                    totallen    += expectSize;
                    stream.wpos += (int)expectSize;
                    length      -= expectSize;

                    moduleId = stream.readUint8();
                    stream.clear();

                    state      = READ_STATE.READ_STATE_MSGID;
                    expectSize = 1;
                }
                else
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                    stream.wpos += (int)length;
                    expectSize  -= length;
                    break;
                }
            }
            else if (state == READ_STATE.READ_STATE_MSGID)
            {
                if (length >= expectSize)
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                    totallen    += expectSize;
                    stream.wpos += (int)expectSize;
                    length      -= expectSize;

                    msgid = stream.readUint8();
                    stream.clear();

                    state      = READ_STATE.READ_STATE_RESPONSE_FLAG;
                    expectSize = 1;
                }
                else
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                    stream.wpos += (int)length;
                    expectSize  -= length;
                    break;
                }
            }
            else if (state == READ_STATE.READ_STATE_RESPONSE_FLAG)
            {
                if (length >= expectSize)
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                    totallen    += expectSize;
                    stream.wpos += (int)expectSize;
                    length      -= expectSize;

                    responseFlag = stream.readUint8();
                    stream.clear();

                    state = READ_STATE.READ_STATE_BODY;
                    //expectSize = msglen - 4 - 1 - 2 - 4 - 2;
                    expectSize = (uint)(msglen - 1 - 1 - 1 - 1);//flowId moduleId msgId
                }
                else
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                    stream.wpos += (int)length;
                    expectSize  -= length;
                    break;
                }
            }

            /*
             * body Can be empty
             */
            if (state == READ_STATE.READ_STATE_BODY)   //真正的Body消息体
            {
                //Debug.LogError("body expect BodySize:"+length+" expSize "+expectSize);
                if (length >= expectSize)
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                    totallen    += expectSize;
                    stream.wpos += (int)expectSize;
                    length      -= expectSize;

                    /*
                     * No Handler Or PushMessage  forward To IPacketHandler
                     * Call Who's RPC Method Or Register Many RPC Method to Handle It ?
                     * [PushHandler]
                     * void GCPushSpriteInfo(Packet packet) {
                     * }
                     *
                     * PacketHandler namespace
                     * IPacketHandler---->GCPushSpriteInfo
                     */
                    IMessageLite pbmsg = MyLib.Util.GetMsg(moduleId, msgid, stream.getBytString());  //通过moduleId,msgid找到对于的proto结构,然后把stream中的内容调用proto的库转换为proto结构
                    //Debug.LogError("expect msgType: "+pbmsg.GetType());
                    Packet p        = new Packet(msglen, flowId, moduleId, msgid, responseFlag, pbmsg);
                    var    fullName = pbmsg.GetType().FullName;
                    mainLoop.queueInLoop(() =>     //这里的mainloop就是用MonoBehaviour开出来的模拟线程
                    {
                        Debug.Log("ReadPacket: " + p.protoBody.ToString());
                    });

                    mainLoop.queueInLoop(() =>   //放到Mainloop中进行消息处理
                    {
                        MessageHandler handler = null;
                        if (flowHandler == null)
                        {
                            handler = msgHandle;                  //msgHandle由RemoteClient提供
                        }
                        else if (flowHandler.ContainsKey(flowId)) //如果需要对该flowId特殊处理,在这里完成,对于由客户端发起带有Flowid的消息,服务器会回一个同样Flowid的消息
                        {
                            handler = flowHandler[flowId];
                            flowHandler.Remove(flowId);
                            if (handler == null)
                            {
                                Debug.LogError("FlowHandlerIsNull: " + flowId);
                            }
                        }
                        else
                        {
                            handler = msgHandle;   //,默认也是由RemoteClient提供msgHandle处理
                        }
                        if (handler != null)
                        {
                            handler(p);
                        }
                        else
                        {
                            Debug.LogError("MessageReader::process No handler for flow Message " + msgid + " " + flowId + " " + pbmsg.GetType() + " " + pbmsg);
                        }
                    });


                    //fullName是检测handlePB()中返回的继承该IMessageLite接口的类是否带有Push字段,带有该字段的协议是由服务器主动发起,

                    //Debug.LogError("HandlerIs: "+flowId+" h "+handler);
                    if (fullName.Contains("Push"))
                    {
                        //Log.Net("MessageReader Handler PushMessage");
                        if (mainLoop != null)
                        {
                            mainLoop.queueInLoop(delegate
                            {
                                var handlerName = fullName.Replace("MyLib", "PacketHandler");
                                var tp          = Type.GetType(handlerName); //通过type的名字找到该Type
                                if (tp == null)
                                {
                                    Debug.LogError("PushMessage noHandler " + handlerName);
                                }
                                else
                                {
                                    //Debug.Log("Handler Push Message here "+handlerName);
                                    var ph = (IPacketHandler)Activator.CreateInstance(tp); //Activator .CreateInstance()用于创建类的实例
                                    ph.HandlePacket(p);                                    //IPacketHandler提供了HandlePacket的方法,该IPacketHandler接口主要是对协议中的Push字段(服务器单方消息)进行处理的接口
                                }
                            });
                        }
                    }

                    stream.clear();
                    state      = READ_STATE.READ_STATE_MSGLEN;
                    expectSize = 2;
                }
                else
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                    stream.wpos += (int)length;
                    expectSize  -= length;
                    break;
                }
            }
        }

        if (responseFlag != 0)
        {
            Debug.LogError("MessageReader:: read Error Packet " + responseFlag);
        }

        //Log.Net("current state after " + state + " msglen " + msglen + " " + length);
        //Log.Net("MessageReader::  prop  flag" + flag + "  msglen " + msglen + " flowId " + flowId + " moduleId " + moduleId + " msgid " + msgid + " responseTime " + responseTime + " responseFlag " + responseFlag + " expectSize " + expectSize);
    }
Exemple #6
0
        public void process(byte[] datas, MessageLength length, Dictionary <uint, MessageHandler> flowHandler)
        {
            //Log.Net("process receive Data " + length + " state " + state);
            MessageLength totallen = 0;

            while (length > 0 && expectSize > 0)
            {
                if (state == READ_STATE.READ_STATE_FLAG)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;

                        flag = stream.readUint8();
                        stream.clear();

                        state      = READ_STATE.READ_STATE_MSGLEN;
                        expectSize = 4;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_MSGLEN)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;

                        msglen = stream.readUint32();
                        stream.clear();

                        state      = READ_STATE.READ_STATE_FLOWID;
                        expectSize = 4;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_FLOWID)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;

                        flowId = stream.readUint32();
                        stream.clear();

                        state      = READ_STATE.READ_STATE_MODULEID;
                        expectSize = 1;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_MODULEID)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;

                        moduleId = stream.readUint8();
                        stream.clear();

                        state      = READ_STATE.READ_STATE_MSGID;
                        expectSize = 2;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_MSGID)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;

                        msgid = stream.readUint16();
                        stream.clear();

                        state      = READ_STATE.READ_STATE_RESPONSE_TIME;
                        expectSize = 4;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_RESPONSE_TIME)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;

                        responseTime = stream.readUint32();
                        stream.clear();

                        state      = READ_STATE.READ_STATE_RESPONSE_FLAG;
                        expectSize = 2;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_RESPONSE_FLAG)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;

                        responseFlag = stream.readInt16();
                        stream.clear();

                        state      = READ_STATE.READ_STATE_BODY;
                        expectSize = msglen - 4 - 1 - 2 - 4 - 2;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }

                /*
                 * body Can be empty
                 */
                if (state == READ_STATE.READ_STATE_BODY)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;

                        /*
                         * No Handler Or PushMessage  forward To IPacketHandler
                         * Call Who's RPC Method Or Register Many RPC Method to Handle It ?
                         * [PushHandler]
                         * void GCPushSpriteInfo(Packet packet) {
                         * }
                         *
                         * PacketHandler namespace
                         * IPacketHandler---->GCPushSpriteInfo
                         */
                        MessageHandler handler = null;
                        if (flowHandler == null)
                        {
                            handler = msgHandle;
                        }
                        else if (flowHandler.ContainsKey(flowId))
                        {
                            handler = flowHandler [flowId];
                            flowHandler.Remove(flowId);
                        }



                        //Message msg = new Message();
                        IMessageLite pbmsg    = KBEngine.Message.handlePB(moduleId, msgid, stream);
                        Packet       p        = new Packet(flag, msglen, flowId, moduleId, msgid, responseTime, responseFlag, pbmsg);
                        var          fullName = pbmsg.GetType().FullName;
                        //Bundle.recvMsg.Add("recvMsg " + fullName + " : " + flowId);
                        //Log.Net("RecvMsg: "+fullName+" f "+flowId);

                        if (fullName.Contains("Push"))
                        {
                            //Log.Net("MessageReader Handler PushMessage");
                            if (mainLoop != null)
                            {
                                mainLoop.queueInLoop(delegate
                                {
                                    var handlerName = fullName.Replace("ChuMeng", "PacketHandler");
                                    var tp          = Type.GetType(handlerName);
                                    if (tp == null)
                                    {
                                        Debug.LogError("PushMessage noHandler " + handlerName);
                                    }
                                    else
                                    {
                                        //Debug.Log("Handler Push Message here "+handlerName);
                                        var ph = (PacketHandler.IPacketHandler)Activator.CreateInstance(tp);
                                        ph.HandlePacket(p);
                                    }
                                });
                            }
                        }
                        else if (handler != null)
                        {
                            mainLoop.queueInLoop(() => {
                                handler(p);
                            });
                        }
                        else
                        {
                            //flowHandler.Remove(flowId);
                            Debug.LogError("MessageReader::process No handler for flow Message " + msgid + " " + flowId + " " + pbmsg.GetType() + " " + pbmsg);
                        }



                        stream.clear();

                        state      = READ_STATE.READ_STATE_FLAG;
                        expectSize = 1;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
            }

            if (responseFlag != 0)
            {
                Debug.LogError("MessageReader:: read Error Packet " + responseFlag);
            }

            //Log.Net("current state after " + state + " msglen " + msglen + " " + length);
            //Log.Net("MessageReader::  prop  flag" + flag + "  msglen " + msglen + " flowId " + flowId + " moduleId " + moduleId + " msgid " + msgid + " responseTime " + responseTime + " responseFlag " + responseFlag + " expectSize " + expectSize);
        }
Exemple #7
0
 //UDP-->KCP
 public void ReceiveData(byte[] data)
 {
     ml.queueInLoop(() => {
         kcp.Input(data, data.Length);
     });
 }