static void PacketManipulationDemo() { Console.WriteLine("Packet manipulation test/demo... should print 3 2 1..."); using (ENet.Packet packet = new ENet.Packet()) { packet.Initialize(new byte[0]); packet.Add((byte)1); packet.Insert(0, (byte)3); packet.Insert(1, (byte)2); packet.Insert(packet.IndexOf((byte)3), 4); packet.Remove(1); packet.RemoveAt(0); if (packet.Contains(3)) { packet.Add((byte)1); } if (packet.Contains(4)) { packet.Add((byte)5); } byte[] bytes = packet.GetBytes(); for (int i = 0; i < bytes.Length; i++) { Console.WriteLine(bytes[i]); } } }
/// <summary> /// Encode Data Object to ENet Packet /// </summary> /// <param name="data">Data Object to Encode</param> /// <returns>get ENet Packet</returns> public ENet.Packet Encode(Data data) { ENet.Packet packet = default(ENet.Packet); packet.Create(Serialize(data)); return(packet); }
/// <summary> /// Decode ENet Packet to Data Object /// </summary> /// <param name="packet_data">ENet Packet to Decode</param> /// <returns>get Data Object</returns> public Data Decode(ENet.Packet packet_data) { byte[] buffer = new byte[packet_data.Length]; packet_data.CopyTo(buffer); return((Data)Deserialize(buffer)); }
static void Server() { using (ENet.Host host = new ENet.Host()) { Console.WriteLine("Initializing server..."); host.InitializeServer(5000, 1); ENet.Peer peer = new ENet.Peer(); while (true) { ENet.Event @event; if (host.Service(15000, out @event)) { do { switch (@event.Type) { case ENet.EventType.Connect: peer = @event.Peer; // If you are using ENet 1.3.4 or newer, the following two methods will work: //peer.SetPingInterval(1000); //peer.SetTimeouts(8, 5000, 60000); Console.WriteLine("Connected to client at IP/port {0}.", peer.GetRemoteAddress()); for (int i = 0; i < 200; i++) { ENet.Packet packet = new ENet.Packet(); packet.Initialize(new byte[] { 0, 0 }, 0, 2, ENet.PacketFlags.Reliable); packet.SetUserData(i); packet.SetUserData("Test", i * i); packet.Freed += p => { Console.WriteLine("Initial packet freed (channel {0}, square of channel {1})", p.GetUserData(), p.GetUserData("Test")); }; peer.Send((byte)i, packet); } break; case ENet.EventType.Receive: byte[] data = @event.Packet.GetBytes(); ushort value = BitConverter.ToUInt16(data, 0); if (value % 1000 == 1) { Console.WriteLine(" Server: Ch={0} Recv={1}", @event.ChannelID, value); } value++; peer.Send(@event.ChannelID, BitConverter.GetBytes(value), ENet.PacketFlags.Reliable); @event.Packet.Dispose(); break; } }while (host.CheckEvents(out @event)); } } } }
private static void server(object state) { var host = (ENet.Host)state; ENet.Event e; host.Service(100, out e); switch (e.Type) { case ENet.EventType.Connect: var packet = new ENet.Packet(); packet.Initialize(Encoding.Default.GetBytes("Bonsoir"), ENet.PacketFlags.UnreliableFragment); e.Peer.Send(0, packet); break; case ENet.EventType.Receive: e.Peer.Send(0, e.Packet); break; } Task.Factory.StartNew(server, host); }
static void Server() { using (ENet.Host host = new ENet.Host()) { Console.WriteLine("Initializing server..."); host.InitializeServer(5000, 1); ENet.Peer peer = new ENet.Peer(); while (true) { ENet.Event @event; if (host.Service(15000, out @event)) { do { switch (@event.Type) { case ENet.EventType.Connect: peer = @event.Peer; // If you are using ENet 1.3.4 or newer, the following two methods will work: //peer.SetPingInterval(1000); //peer.SetTimeouts(8, 5000, 60000); Console.WriteLine("Connected to client at IP/port {0}.", peer.GetRemoteAddress()); for (int i = 0; i < 200; i++) { ENet.Packet packet = new ENet.Packet(); packet.Initialize(new byte[] { 0, 0 }, 0, 2, ENet.PacketFlags.Reliable); packet.SetUserData(i); packet.SetUserData("Test", i * i); packet.Freed += p => { Console.WriteLine("Initial packet freed (channel {0}, square of channel {1})", p.GetUserData(), p.GetUserData("Test")); }; peer.Send((byte)i, packet); } break; case ENet.EventType.Receive: byte[] data = @event.Packet.GetBytes(); ushort value = BitConverter.ToUInt16(data, 0); if (value % 1000 == 1) { Console.WriteLine(" Server: Ch={0} Recv={1}", @event.ChannelID, value); } value++; peer.Send(@event.ChannelID, BitConverter.GetBytes(value), ENet.PacketFlags.Reliable); @event.Packet.Dispose(); break; } } while (host.CheckEvents(out @event)); } } } }