예제 #1
0
        /// <summary>
        /// Starts an UDP server
        /// </summary>
        /// <param name="endPoint">The IP Endpoint to bind the server</param>
        /// <param name="maxConnections">Max connections allowed</param>
        public override void Start(IPEndPoint bindTo, int MaxConnections)
        {
            // Create our Socket
            Listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
            {
                SendTimeout       = 5000, // We have a limited pool, so we dont want to be locked often
                SendBufferSize    = BufferSizePerEvent,
                ReceiveBufferSize = BufferSizePerEvent
            };

            // Bind to our port
            Listener.Bind(bindTo);

            // Set the rest of our internals
            MaxNumConnections      = MaxConnections;
            MaxConnectionsEnforcer = new SemaphoreSlim(MaxNumConnections, MaxNumConnections);
            SocketReadWritePool    = new SocketAsyncEventArgsPool(MaxNumConnections);

            // Create our Buffer Manager for IO operations.
            BufferManager = new BufferManager(
                MaxNumConnections,
                BufferSizePerEvent
                );

            // Assign our Connection IO SocketAsyncEventArgs object instances
            for (int i = 0; i < MaxNumConnections; i++)
            {
                SocketAsyncEventArgs SockArg = new SocketAsyncEventArgs();
                SockArg.Completed += IOComplete;
                BufferManager.AssignBuffer(SockArg);
                SocketReadWritePool.Push(SockArg);
            }

            // Set public internals
            Port       = bindTo.Port;
            Address    = bindTo.Address.ToString();
            IsRunning  = true;
            IsDisposed = false;

            // Start accepting sockets
            StartAcceptAsync();
        }