Exemplo n.º 1
0
        /// <summary>
        /// Creates the bootstrap instance
        /// </summary>
        ///
        /// <param name="maxConnections">
        /// The maximum amount of concurrent connections to handle
        /// </param>
        public ServerBootstrap(ChannelPipelineFactory pipelineFactory, int maxConnections = 1024)
        {
            //Ensure the pipeline is not null
            if (pipelineFactory == null)
            {
                Logger.Severe("Invalid pipeline factory specified!");
                Environment.Exit(0);
            }

            //Ensure a channel handler is set
            if (pipelineFactory.getPipeline().getHandler() == null)
            {
                Logger.Severe("Invalid channel handler specified!");
                Environment.Exit(0);
            }

            //Ensure an encoder is set
            if (pipelineFactory.getPipeline().getEncoder() == null)
            {
                Logger.Severe("Invalid encoder specified!");
                Environment.Exit(0);
            }

            //Ensure a decoder is set
            if (pipelineFactory.getPipeline().getDecoder() == null)
            {
                Logger.Severe("Invalid decoder specified!");
                Environment.Exit(0);
            }

            this.pipelineFactory = pipelineFactory;
            this.maxConnections  = maxConnections;
        }
Exemplo n.º 2
0
        private void connectCallback(IAsyncResult ar)
        {
            //The listener socket
            Socket listener = (Socket)ar.AsyncState;

            //The connected socket
            Socket socket = listener.EndAccept(ar);

            //Set the socket options
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);

            //The channel instance
            ConnectionChannel channel = new ConnectionChannel(socket, pipelineFactory.getPipeline());

            //Add the channel
            connectedChannels.Add(channel);

            //An empty packet instance
            Packet packet = new Packet();

            //Begin receiving to the packet payload
            channel.getSocket().BeginReceive(packet.getPayload(), 0, packet.getPayload().Length, 0, new AsyncCallback(readCallback), new object[] { channel, packet });

            // Start accepting another connection
            listener.BeginAccept(new AsyncCallback(connectCallback), listener);
        }