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); } }
public static PtMessagePackage Build(int messageId) { PtMessagePackage package = new PtMessagePackage(); package.MessageId = messageId; return(package); }
public static PtMessagePackage Build(int messageId, byte[] content, bool compress = false) { PtMessagePackage package = Build(messageId); package.IsCompress = compress; package.Content = content; return(package); }
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); } }
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()); } }
public void Broadcast(PtMessagePackage package) { Broadcast(PtMessagePackage.Write(package)); }
public void Write(PtMessagePackage package) { Write(PtMessagePackage.Write(package)); }
/// <summary> /// Server 端快速回复 /// </summary> /// <param name="package"></param> public void Reply(PtMessagePackage package) { Reply(PtMessagePackage.Write(package)); }
private void BcAll(byte[] msg) { Broadcast(PtMessagePackage.Build(messageid, msg)); messageid++; }
private void BcAll(String msg) { Broadcast(PtMessagePackage.Build(messageid, System.Text.Encoding.Default.GetBytes(msg))); messageid++; }
private void Reply(TcpClient client, byte[] msg) { NetworkStreamUtil.Write(client.GetStream(), PtMessagePackage.Write(PtMessagePackage.Build(messageid, msg))); messageid++; }