void UdpReceiveHandler(SharedArraySelectServer server, Socket sock) { EndPoint from = new IPEndPoint(IPAddress.Any, 0); int received = sock.ReceiveFrom(server.sharedArray, ref from); if (received < 0) { throw new Exception("ReceiveFrom failed"); } String command = Encoding.ASCII.GetString(server.sharedArray, 0, received); if (command.Equals("stop")) { while (server.ListenSockets.Count > 0) { var enumerator = server.ListenSockets.GetEnumerator(); Assert.AreEqual(true, enumerator.MoveNext()); Console.WriteLine("[SelectServer] Closing listen socket..."); server.DisposeAndRemoveListenSocket(enumerator.Current); } Console.WriteLine("[SelectServer] Closing UDP receive socket..."); Console.Out.Flush(); server.DisposeAndRemoveReceiveSocket(sock); } }
Thread StartSelectServer(out UInt16 tcpListenPort, out UInt16 udpReceivePort) { IPEndPoint listenEndPoint = new IPEndPoint(IPAddress.Any, 0); Socket udpReceiveSock = new Socket(listenEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp); udpReceiveSock.Bind(listenEndPoint); udpReceivePort = (UInt16)((IPEndPoint)udpReceiveSock.LocalEndPoint).Port; Socket tcpListenSock = new Socket(listenEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); tcpListenSock.Bind(listenEndPoint); tcpListenPort = (UInt16)((IPEndPoint)tcpListenSock.LocalEndPoint).Port; tcpListenSock.Listen(8); var server = new SharedArraySelectServer(); server.AddReceiveSocket(udpReceiveSock, UdpReceiveHandler); server.AddListenSocket(tcpListenSock, TcpAcceptHandler); Thread serverThread = new Thread(() => { server.Run(); }); serverThread.Name = "SelectServer"; serverThread.Start(); return(serverThread); }
void TcpAcceptHandler(SharedArraySelectServer server, Socket sock) { Socket newSock = sock.Accept(); Console.WriteLine("[SelectServer] Accepted new connection from '{0}'", newSock.RemoteEndPoint); server.AddReceiveSocket(newSock, TcpReceiveHandler); }
void TcpReceiveHandler(SharedArraySelectServer server, Socket sock) { int received = sock.Receive(server.sharedArray); if (received <= 0) { server.DisposeAndRemoveReceiveSocket(sock); } else { Console.WriteLine("[SelectServer] Got {0} bytes", received); } }