示例#1
0
        private void PrintMsg(object sender, Message message)
        {
            PtMessagePackage msg     = PtMessagePackage.Read(message.Data);
            ByteBuffer       buffer  = new ByteBuffer(msg.Content);
            String           address = message.TcpClient.Client.RemoteEndPoint.ToString();

            Console.WriteLine("client:" + address);
            long tick = buffer.ReadLong();

            Console.WriteLine("tick:" + tick);
            String act = buffer.ReadString();

            Console.WriteLine("act:" + act);
            if (act == "3")
            {
                foreach (ClientMsg cmsg in msg_list)
                {
                    Reply(message.TcpClient, cmsg.Msg);
                }
                ByteBuffer r_buffer = new ByteBuffer(msg.Content);
                r_buffer.WriteString("3");
                Reply(message.TcpClient, r_buffer.Getbuffer());
            }
            else
            {
                msg_list.Add(new ClientMsg(address, msg.Content));
                BcAll(msg.Content);
            }
        }
示例#2
0
        public static PtMessagePackage Build(int messageId)
        {
            PtMessagePackage package = new PtMessagePackage();

            package.MessageId = messageId;
            return(package);
        }
示例#3
0
        public static PtMessagePackage Build(int messageId, byte[] content, bool compress = false)
        {
            PtMessagePackage package = Build(messageId);

            package.IsCompress = compress;
            package.Content    = content;
            return(package);
        }
示例#4
0
 public static PtMessagePackage Read(byte[] bytes)
 {
     using (ByteBuffer buffer = new ByteBuffer(bytes))
     {
         PtMessagePackage info = new PtMessagePackage();
         info.MessageId  = buffer.ReadInt32();
         info.HasContent = buffer.ReadBool();
         if (info.HasContent)
         {
             info.IsCompress = buffer.ReadBool();
             if (info.IsCompress)
             {
                 info.Content = ByteBuffer.Decompress(buffer.ReadBytes());
             }
             else
             {
                 info.Content = buffer.ReadBytes();
             }
         }
         return(info);
     }
 }
示例#5
0
        public static byte[] Write(PtMessagePackage info)
        {
            using (ByteBuffer buffer = new ByteBuffer())
            {
                buffer.WriteInt32(info.MessageId);
                buffer.WriteBool(info.HasContent);
                if (info.HasContent)
                {
                    buffer.WriteBool(info.IsCompress);
                    if (info.IsCompress)
                    {
                        buffer.WriteBytes(ByteBuffer.CompressBytes(info.Content));
                    }
                    else
                    {
                        buffer.WriteBytes(info.Content);
                    }
                }

                return(buffer.Getbuffer());
            }
        }
示例#6
0
 public void Broadcast(PtMessagePackage package)
 {
     Broadcast(PtMessagePackage.Write(package));
 }
示例#7
0
 public void Write(PtMessagePackage package)
 {
     Write(PtMessagePackage.Write(package));
 }
示例#8
0
 /// <summary>
 /// Server 端快速回复
 /// </summary>
 /// <param name="package"></param>
 public void Reply(PtMessagePackage package)
 {
     Reply(PtMessagePackage.Write(package));
 }
示例#9
0
 private void BcAll(byte[] msg)
 {
     Broadcast(PtMessagePackage.Build(messageid, msg));
     messageid++;
 }
示例#10
0
 private void BcAll(String msg)
 {
     Broadcast(PtMessagePackage.Build(messageid, System.Text.Encoding.Default.GetBytes(msg)));
     messageid++;
 }
示例#11
0
 private void Reply(TcpClient client, byte[] msg)
 {
     NetworkStreamUtil.Write(client.GetStream(), PtMessagePackage.Write(PtMessagePackage.Build(messageid, msg)));
     messageid++;
 }