コード例 #1
0
    public IRltProto CreateLuaProto(ushort id, ByteBuffer buffer, ref int protocolLen)
    {
        if (propsJsonDict.ContainsKey(id))
        {
            var props = propsJsonDict[id];

            /**
             * 暂时不用吧
             */
            // if (props.NeedCompress)
            // {
            //     int decompressLen = buffer.Decompress(protocolLen - 2);
            //     protocolLen = decompressLen + 2;
            // }
            var ret = new RltLuaMessage();
            ret.ID = id;
            SerializeJsonTools.FillObject(ret.Data, props, buffer, 1);
            return(ret);
        }
        else
        {
            Debug.LogError("协议 id " + id + "未定义");
            return(null);
        }
    }
コード例 #2
0
    public void ReceiveMessage()
    {
        try
        {
            while (stopThread == false)
            {
                if (ThreadSleepTime > 0)
                {
                    Thread.Sleep(ThreadSleepTime);
                }

                if (tcpClient == null || tcpClient.Connected == false)
                {
                    continue;
                }
                if (stream.CanRead && stream.DataAvailable)
                {
                    int bytes = stream.Read(buffer, 0, buffer.Length);
                    if (bytes > 0)
                    {
                        byteBuffer.WriteBytes(buffer, bytes);
                        var protos = byteBuffer.ReadProtocol(bytes);
                        if (protos != null && protos.Length > 0)
                        {
                            GameMainLoopManager.Instance.AddActionDoInMainThread(() =>
                            {
                                for (int i = 0; i < protos.Length; i++)
                                {
                                    bool isHeartbeat = protos[i].ID == RltHeartBeatProtoID;
                                    if (isHeartbeat)
                                    {
                                        RltLuaMessage msg = protos[i] as RltLuaMessage;
                                        if (msg != null)
                                        {
                                            OnHeartBeat((long)msg.Data["current_time"]);
                                        }
                                    }

                                    if (isHeartbeat == false || NeedLuaHearbeatCallback)
                                    {
                                        protos[i].Deal(DealMessageFunc);
                                    }
                                }
                            });
                        }
                    }
                }
            }
        }
        catch (ThreadAbortException)
        {
        }
        catch (Exception e)
        {
            if (OnError != null)
            {
                OnError(e);
            }
        }
    }