public static void Main(string[] args) { var host = new ENet.Host(); host.Create(new Address(Address.IPv4HostAny, 5119), 32, 8, 0, 0); while (true) { while (host.Service(0, out Event eevent) != 0) { switch (eevent.Type) { case EventType.None: Console.WriteLine("Event: none!"); break; case EventType.Connect: Console.WriteLine("Event: connect!"); break; case EventType.Disconnect: Console.WriteLine("Event: disconnect!"); break; case EventType.Receive: Console.WriteLine("Event: recieve!"); break; } } } }
public int Bind(Address address) { m_Address = address; m_DidBind = m_Host.Create(address, (int)MaxConnections, 32); if (m_DidBind) { return(0); } return(-1); }
private static void Server() { using (var host = new Host()) { host.Create(5000, 1); var peer = new Peer(); while (host.Service(1) >= 0) { Event @event; while (host.CheckEvents(out @event) > 0) { //Console.WriteLine("Server: " + @event.Type.ToString()); switch (@event.Type) { case EventType.Connect: peer = @event.Peer; for (var i = 0; i < 200; i++) { peer.Send((byte) i, new byte[] {0, 0}); } break; case EventType.Receive: var data = @event.Packet.GetBytes(); var 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)); @event.Packet.Dispose(); break; } } } } }
private static void Client() { using (var host = new Host()) { host.Create(null, 1); var address = new Address(); address.SetHost("127.0.0.1"); address.Port = 5000; var peer = host.Connect(address, 200, 1234); while (host.Service(1) >= 0) { Event @event; while (host.CheckEvents(out @event) > 0) { //Console.WriteLine("Client: " + @event.Type.ToString()); switch (@event.Type) { case EventType.Receive: var data = @event.Packet.GetBytes(); var 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)); @event.Packet.Dispose(); break; } } } } }
public Task StartAsync(CancellationToken cancellationToken) { cancellationToken.Register(() => { _shouldRun = false; _logger.LogInformation("Enet server is shutting down..."); }); _serverTask = Task.Factory.StartNew(() => { Library.Initialize(); _shouldRun = true; using (Host server = new Host()) { Address address = new Address { Port = _configuration.Port }; int pollingTimeout = _configuration.PollingTimeout; server.Create(address, (int)_configuration.PeerLimit); _logger.LogInformation($"Enet server listening at 0.0.0.0:{address.Port}"); Event netEvent; while (_shouldRun) { bool polled = false; while (!polled) { if (server.CheckEvents(out netEvent) <= 0) { if (server.Service(pollingTimeout, out netEvent) <= 0) { break; } polled = true; } switch (netEvent.Type) { case EventType.None: break; case EventType.Connect: _connectionHandlers.OnConnectedPeer(netEvent.Peer); break; case EventType.Disconnect: _disconnectionHandlers.OnDisconnectedPeer(netEvent.Peer); break; case EventType.Timeout: _timeoutHandlers.OnTimeoutPeer(netEvent.Peer); break; case EventType.Receive: _packetHandlers.OnPacketReceived(netEvent.Peer, netEvent.ChannelID, netEvent.Packet); break; } } } server.Flush(); } _logger.LogInformation("Enet server closed."); Library.Deinitialize(); }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current); return(_serverTask); }