コード例 #1
0
        private void readCallback(IAsyncResult ar)
        {
            //The callback messages
            object[] messages = (object[])ar.AsyncState;

            //The channel
            ConnectionChannel channel = (ConnectionChannel)messages[0];

            //The packet
            Packet packet = (Packet)messages[1];

            try {
                //If we are unable to poll the channel's socket, it must be terminated
                if (channel.getSocket().Poll(1000, SelectMode.SelectRead))
                {
                    channel.getPipeline().getHandler().connectionTerminated(channel);
                    connectedChannels.Remove(channel);
                    return;
                }

                //Send the packet to the channel's decoder
                channel.getPipeline().getHandler().messageReceived(channel, channel.getPipeline().getDecoder().decode(channel, packet));

                //Await another packet
                channel.getSocket().BeginReceive(packet.getPayload(), 0, packet.getPayload().Length, 0, new AsyncCallback(readCallback), new object[] { channel, packet });
            } catch (Exception e) {
                //Send the exception to the channel's handler
                channel.getPipeline().getHandler().exceptionCaught(channel, e);
            }
        }
コード例 #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);
        }