public NetClient() { CanSend = true; host = new ENet.Host(); host.Initialize(null, 1); OnStatusChanged += Client_OnStatusChanged; OnData += Client_OnData; }
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)); } } } }
void ClientThread(HostInfo hostinfo) { _hostInfo = hostinfo; using (ENet.Host host = new ENet.Host()) { Console.WriteLine("Initializing client..."); host.Initialize(hostinfo.address, hostinfo.peerLimit, hostinfo.channelLimit, hostinfo.incomingBandwidth, hostinfo.outgoingBandwidth); peer = host.Connect(hostinfo.hostName, hostinfo.port, hostinfo.data, hostinfo.channelLimit); hostinfo.isConnected = true; while (hostinfo.isConnected) { ENet.Event @event; if (host.Service(hostinfo.timeout, out @event)) { do { switch (@event.Type) { case ENet.EventType.Connect: Console.WriteLine("Connected to server at IP/port {0}.", peer.GetRemoteAddress()); break; case ENet.EventType.Receive: byte[] data = @event.Packet.GetBytes(); HandleMessage(data); ushort value = BitConverter.ToUInt16(data, 0); if (value % 1000 == 0) { Console.WriteLine(" Client: Ch={0} Recv={1}", @event.ChannelID, value); } value++; // peer.Send(@event.ChannelID, BitConverter.GetBytes(value), ENet.PacketFlags.Reliable); @event.Packet.Dispose(); break; case ENet.EventType.Disconnect: Console.WriteLine("Disconnected from server at IP/port {0}.", peer.GetRemoteAddress()); hostinfo.isConnected = false; hostinfo.isLoggedIn = false; break; default: Console.WriteLine(@event.Type); break; } }while (host.CheckEvents(out @event)); } } } }
public NetServer(string upnpIdent, int cport, int maxConnections = 64, bool enableUPnP = true) { m_enableUPnP = enableUPnP; m_maxConnections = maxConnections; UPnPOpenPort(cport); host = new ENet.Host(); host.InitializeServer(cport, maxConnections); listenPort = cport; OnData += Server_OnData; OnStatusChanged += Server_OnStatusChanged; }
static void Main(string[] args) { var host = new ENet.Host(); host.InitializeServer(111, 500); Task.Factory.StartNew(server, host); var peers = new ENet.Host[500]; for (int i = 0; i < peers.Length; i++) { var peer = (peers[i] = new ENet.Host()); peer.InitializeClient(1); peer.Connect("localhost", 111, 0); Task.Factory.StartNew(client, peer); } Console.Read(); }
static void Client() { using (ENet.Host host = new ENet.Host()) { Console.WriteLine("Initializing client..."); host.Initialize(null, 1); peer = host.Connect("127.0.0.1", 1234, 1234, 200); while (true) { ENet.Event @event; if (host.Service(15000, out @event)) { do { switch (@event.Type) { case ENet.EventType.Connect: Console.WriteLine("Connected to server at IP/port {0}.", peer.GetRemoteAddress()); break; case ENet.EventType.Receive: byte[] data = @event.Packet.GetBytes(); ushort value = BitConverter.ToUInt16(data, 0); if (value % 1000 == 0) { Console.WriteLine(" Client: Ch={0} Recv={1}", @event.ChannelID, value); } value++; peer.Send(@event.ChannelID, BitConverter.GetBytes(value), ENet.PacketFlags.Reliable); @event.Packet.Dispose(); break; default: Console.WriteLine(@event.Type); break; } }while (host.CheckEvents(out @event)); } } } }
static void Client() { using (ENet.Host host = new ENet.Host()) { Console.WriteLine("Initializing client..."); host.Initialize(null, 1); ENet.Peer peer = host.Connect("127.0.0.1", 5000, 1234, 200); while (true) { ENet.Event @event; if (host.Service(15000, out @event)) { do { switch (@event.Type) { case ENet.EventType.Connect: Console.WriteLine("Connected to server at IP/port {0}.", peer.GetRemoteAddress()); break; case ENet.EventType.Receive: byte[] data = @event.Packet.GetBytes(); ushort value = BitConverter.ToUInt16(data, 0); if (value % 1000 == 0) { Console.WriteLine(" Client: Ch={0} Recv={1}", @event.ChannelID, value); } value++; peer.Send(@event.ChannelID, BitConverter.GetBytes(value), ENet.PacketFlags.Reliable); @event.Packet.Dispose(); break; default: Console.WriteLine(@event.Type); break; } } while (host.CheckEvents(out @event)); } } } }
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)); } } } }