static void EventLoop(UdpSocket socket) { UdpConnection c = null; while (true) { UdpEvent ev = default(UdpEvent); if (socket.Poll(ref ev)) { UdpLog.User(ev.EventType.ToString()); switch (ev.EventType) { case UdpEventType.ConnectRequest: socket.Accept(ev.EndPoint); break; case UdpEventType.Connected: c = ev.Connection; break; } } if (c != null) { c.Send(10u); } Thread.Sleep(100); } }
static void Server() { #if DISABLE_AUTO_ACCEPT UdpConfig config = new UdpConfig(); config.AutoAcceptIncommingConnections = false; #else UdpConfig config = new UdpConfig(); #endif UdpSocket server = UdpSocket.Create <UdpPlatformManaged, DummySerializer>(config); server.Start(new UdpEndPoint(UdpIPv4Address.Localhost, 14000)); while (true) { UdpEvent ev = default(UdpEvent); while (server.Poll(ref ev)) { UdpLog.User("Event raised {0}", ev.EventType); switch (ev.EventType) { case UdpEventType.Connected: UdpLog.User("Client connected from {0}, total clients connected: {1}", ev.Connection.RemoteEndPoint, server.ConnectionCount); break; #if ENABLE_MANUAL_ACCEPT case UdpEventType.ConnectRequest: UdpLog.User("Connection requested from {0}", ev.EndPoint); server.Accept(ev.EndPoint); break; #endif } } // Simulate ~60fps game loop Thread.Sleep(16); } }