Example #1
0
        private void OnTcpRec(SocketToken token, BufferSegment seg)
        {
            tcpTokenPool.RefreshConnectionToken(token);
            PacketReader pr;

            if (!_tcpPkgs.TryGetValue(token, out pr))
            {
                pr = new PacketReader(Configs.net.TCPBuffersize * 4096);
                _tcpPkgs.Add(token, pr);
            }

            pr.Set(seg.buffer, seg.offset, seg.count);


            var pkgs = pr.Get();

            if (pkgs != null)
            {
                for (int i = 0; i < pkgs.Count; i++)
                {
                    var         p      = pkgs[i];
                    Type        type   = NetMessageTool.GetTypeByID(p.pkgID);
                    string      msgstr = encoding.GetString(p.message);
                    INetMessage msg    = Json.ToObject(type, msgstr) as INetMessage;
                    APP.env.modules.Loom.RunOnMainThread(() =>
                    {
                        if (onTcpMessage != null)
                        {
                            onTcpMessage(token, msg);
                        }
                    });
                }
            }
        }
Example #2
0
        private BufferSegment NetMessageToBufferSegment(INetMessage message)
        {
            var    bytes  = encoding.GetBytes(Json.ToJsonString(message));
            Packet pac    = new Packet(1, NetMessageTool.GetIDByType(message.GetType()), 1, bytes);
            var    buffer = pac.Pack();

            return(new BufferSegment(buffer, buffer.Length));
        }