示例#1
0
 public void Run(ICdpServer server, Byte[] maxDatagramBuffer, ICdpTimeout timeout)
 {
     Cdp.UdpServerLoop(server, udpSocket, endPoint, maxDatagramBuffer, timeout);
 }
示例#2
0
        public static void UdpServerLoop(ICdpServer server, Socket udpSocket, EndPoint listenEndPoint, Byte[] maxDatagramBuffer, ICdpTimeout timeout)
        {
            Dictionary <EndPoint, CdpServerDatagramHandler> endPointToHandler =
                new Dictionary <EndPoint, CdpServerDatagramHandler>();

            //Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            udpSocket.Bind(listenEndPoint);

            try
            {
                while (true)
                {
                    EndPoint from = listenEndPoint;
                    int      bytesRead;
                    try
                    {
                        bytesRead = udpSocket.ReceiveFrom(maxDatagramBuffer, ref from);
                    }
                    catch (SocketException e)
                    {
                        Boolean stopServerGracefully = server.SocketException(e);
                        if (stopServerGracefully)
                        {
                            throw new NotImplementedException("Stop server gracefully not implemented");
                        }
                        continue;
                    }

                    if (bytesRead <= 0)
                    {
                        // it's just a heartbeat
                        Console.WriteLine("[CdpDebug] Got a heartbeat from '{0}'", from);
                        continue;
                    }

                    CdpServerDatagramHandler handler;

                    //
                    // Handle new connection
                    //
                    if (!endPointToHandler.TryGetValue(from, out handler))
                    {
                        CdpTransmitter transmitter = new CdpTransmitter(new UdpConnectedServerTransmitter(udpSocket, from));

                        ICdpServerHandler serverHandler;
                        Int32             maxSendBeforeAck;
                        Boolean           refuseConnection = server.NewConnection(transmitter, out serverHandler, out maxSendBeforeAck);

                        if (refuseConnection)
                        {
                            handler.Closed();
                            server.ConnectionClosed(from);
                            throw new NotImplementedException("Refusing connection is not yet implemented");
                        }

                        if (serverHandler == null)
                        {
                            handler.Closed();
                            server.ConnectionClosed(from);
                            throw new InvalidOperationException("You provided a null payload handler");
                        }

                        handler = new CdpServerDatagramHandler(transmitter, serverHandler, timeout);
                        endPointToHandler.Add(from, handler);
                    }

                    Boolean closeClient = handler.Datagram(maxDatagramBuffer, 0, bytesRead);
                    if (closeClient)
                    {
                        handler.Closed();
                        server.ConnectionClosed(from);
                        endPointToHandler.Remove(from);
                    }
                }
            }
            finally
            {
                if (udpSocket != null)
                {
                    udpSocket.Close();
                }
            }
        }