public static void doServerService(int delay = 0) { doClientService(0); var Event = m_Host.Service(TimeSpan.FromMilliseconds(delay)); switch (Event.Type) { case ENetEventType.None: break; case ENetEventType.Connect: Host_OnConnect(Event.Peer); break; case ENetEventType.Disconnect: break; case ENetEventType.Receive: Peer_OnReceive(Event.Peer, Event.Packet); Event.Packet.Destroy(); break; default: throw new NotImplementedException(); } doClientService(0); }
private void Run() { ENetEvent eNetEvent = new ENetEvent(); while (_running) { int result = _eNetHost.Service(eNetEvent, 1000); _logger.Info($"Result: {result}"); switch (eNetEvent.Type) { case ENetEventType.ENET_EVENT_TYPE_CONNECT: _logger.Info( $"A new client connected from: {eNetEvent.Peer.Address.Address}:{eNetEvent.Peer.Address.Port}"); break; case ENetEventType.ENET_EVENT_TYPE_RECEIVE: _logger.Info( $"A packet of length {eNetEvent.Packet.DataLength} was received from {eNetEvent.Peer.Address.Address}:{eNetEvent.Peer.Address.Port} on channel {eNetEvent.ChannelId}"); ENetPacket packet = new ENetPacket(); packet.Data = Encoding.UTF8.GetBytes("Test Response\0"); packet.DataLength = (uint)packet.Data.Length; packet.Flags = ENetPacketFlags.ENET_PACKET_FLAG_RELIABLE; eNetEvent.Peer.Send(eNetEvent.ChannelId, packet); break; case ENetEventType.ENET_EVENT_TYPE_DISCONNECT: _logger.Info( $"Client {eNetEvent.Peer.Address.Address}:{eNetEvent.Peer.Address.Port} disconnected"); break; } } }
static void Main(string[] args) { // You should call this at start of your application or before any usage of ENet Console.WriteLine("Starting ENet..."); ManagedENet.Startup(); // The IP endpoint which we are going to listen on var listenEndPoint = new IPEndPoint(IPAddress.Loopback, 27015); // By creating ENetHost we bind the endpoint Console.WriteLine("Creating host..."); var host = new ENetHost(listenEndPoint, MaximumPeers, MaximumChannels); Console.WriteLine($"Servicing on {listenEndPoint}"); while (true) { var Event = host.Service(TimeSpan.FromSeconds(60)); switch (Event.Type) { case ENetEventType.None: continue; case ENetEventType.Connect: Console.WriteLine($"Peer connected from {Event.Peer.GetRemoteEndPoint()}"); continue; case ENetEventType.Disconnect: Console.WriteLine($"Peer disconnected from {Event.Peer.GetRemoteEndPoint()}"); continue; case ENetEventType.Receive: // Decode packet data bytes to ASCII string var dataString = Encoding.ASCII.GetString(Event.Packet.Data); // Here we prefix the dataString with peer's remote endpoint dataString = $"{Event.Peer.GetRemoteEndPoint()}: {dataString}"; Console.WriteLine($"Peer {Event.Peer.GetRemoteEndPoint()}: {dataString}"); // this will broadcast the packet to all connected peers // including the peer that sent this packet host.Broadcast(Event.ChannelId, Event.Packet.Data, ENetPacketFlags.Reliable); // We are done with the packet that the peer sent so we destroy it // if you miss this you will end up with memory leaks Event.Packet.Destroy(); continue; default: throw new NotImplementedException(); } } }
public static void doClientService(int delay = 0) { if (client == null) { return; } if (client.Disposed) { return; } var Event = client.Service(TimeSpan.FromMilliseconds(delay)); unsafe { switch (Event.Type) { case ENetEventType.None: break; case ENetEventType.Connect: Client_OnConnect(Event.Peer); break; case ENetEventType.Disconnect: Peer_OnDisconnect(Event.Peer, 0); Event.Peer.UnsetUserData(); break; case ENetEventType.Receive: Peer_OnReceive_Client(Event.Peer, Event.Packet); Event.Packet.Destroy(); break; default: throw new NotImplementedException(); } } }
static void Main(string[] args) { // You should call this at start of your application or before any usage of ENet library Console.WriteLine("Starting ENet..."); ManagedENet.Startup(); // By passing null as endpoint the system will pick up a random open endpoint to listen on // since we are the client we choose a random endpoint IPEndPoint listenEndPoint = null; var host = new ENetHost(listenEndPoint, MaximumPeers, MaximumChannels); // This is the endpoint that the server is listening on // IPAddress.Loopback equals to 127.0.0.1 on most systems IPEndPoint connectEndPoint = new IPEndPoint(IPAddress.Loopback, 27015); // Here we connect to the server by creating a peer and sending the connect packet // Connect Data is a number which we can supply with our packet // this number can be ignored by server uint connectData = 0; // Send connect request Console.WriteLine("Requesting connection..."); var peer = host.Connect(connectEndPoint, MaximumChannels, connectData); while (true) { var Event = host.Service(TimeSpan.FromMilliseconds(250)); switch (Event.Type) { case ENetEventType.None: // Check if user is about to write something to input if (Console.KeyAvailable) { // Read user input var line = Console.ReadLine(); // If user wanted to disconnect if (line == "/disconnect") { // Request disconnect peer.Disconnect(data: 0); // Go for next event continue; } // Encode the input into ASCII bytes var data = Encoding.ASCII.GetBytes(line); // Send packet through channel 0 with the reliable packet flag set peer.Send(channelId: 0, data, ENetPacketFlags.Reliable); } continue; case ENetEventType.Connect: Console.WriteLine("Connected, write /disconnect to disconnect from server"); continue; case ENetEventType.Disconnect: Console.WriteLine("Disconnected"); goto shutdown; case ENetEventType.Receive: // Decode packet data into ASCII string var dataString = Encoding.ASCII.GetString(Event.Packet.Data); // We are done with this packet so we destroy it Event.Packet.Destroy(); Console.WriteLine(dataString); continue; default: throw new NotImplementedException(); } } shutdown: host.Dispose(); Console.WriteLine("Shutdown ENet..."); ManagedENet.Shutdown(); Console.WriteLine("Client stopped, press any key to close the app"); Console.ReadKey(); }