Пример #1
0
        private void AcceptHandler(IAsyncResult result)
        {
            Debug.Assert(this.state == ServerState.Started);
            Socket peerSocket;
            NaiadProtocolOpcode opcode = this.GetOpcode(result, out peerSocket);
            Action <Socket>     acceptAction;

            if (this.serverActions.TryGetValue(opcode, out acceptAction))
            {
                try
                {
                    acceptAction(peerSocket);
                }
                catch (Exception e)
                {
                    Logging.Progress("Error handling a connection with opcode: {0}", opcode);
                    Logging.Progress(e.ToString());
                    peerSocket.Close();
                }
            }
            else
            {
                Logging.Progress("Invalid/unhandled opcode received: {0}", opcode);
                peerSocket.Close();
            }
        }
Пример #2
0
 private void AcceptInternal(Action <Socket> action, Socket peerSocket, NaiadProtocolOpcode opcode)
 {
     try
     {
         action(peerSocket);
     }
     catch (Exception e)
     {
         Logging.Progress("Error handling a connection with opcode: {0}", opcode);
         Logging.Progress(e.ToString());
         peerSocket.Close();
     }
 }
Пример #3
0
        private void AcceptHandler(IAsyncResult result)
        {
            Debug.Assert(this.state == ServerState.Started);
            Socket peerSocket;
            NaiadProtocolOpcode opcode = this.GetOpcode(result, out peerSocket);
            GuardedAction       acceptAction;

            if (this.serverActions.TryGetValue(opcode, out acceptAction))
            {
                acceptAction.guard.ContinueWith((task) => AcceptInternal(acceptAction.action, peerSocket, opcode));
            }
            else
            {
                Logging.Progress("Invalid/unhandled opcode received: {0}", opcode);
                peerSocket.Close();
            }
        }
Пример #4
0
        /// <summary>
        /// Accept loop thread. Implements protocol operation demuxing, based on a 4-byte opcode, in the first 4 bytes received
        /// from the accept()'ed socket.
        /// </summary>
        private void ThreadStart()
        {
            this.state = ServerState.Started;

            this.listeningSocket.Listen(100);
            while (true)
            {
                Socket acceptedSocket;
                try
                {
                    acceptedSocket = this.listeningSocket.Accept();
                }
                catch (ObjectDisposedException)
                {
                    break;
                }
                catch (SocketException se)
                {
                    if (se.SocketErrorCode == SocketError.Interrupted)
                    {
                        break;
                    }
                    else
                    {
                        throw;
                    }
                }
                NaiadProtocolOpcode opcode = (NaiadProtocolOpcode)ReceiveInt(acceptedSocket);
                GuardedAction       acceptAction;
                if (this.serverActions.TryGetValue(opcode, out acceptAction))
                {
                    // Ensures that the Action runs after the Guard task has run.
                    acceptAction.Guard.ContinueWith(
                        (task) => AcceptInternal(acceptAction.Action, acceptedSocket, opcode));
                }
                else
                {
                    Logging.Progress("Invalid/unhandled opcode received: {0}", opcode);
                    acceptedSocket.Close();
                }
            }
        }
Пример #5
0
 /// <summary>
 /// Registers an Action that will be called when a connection is made with the given opcode.
 ///
 /// The Action receives the socket for the accepted connection, and is responsible for managing that
 /// resource by e.g. closing it.
 ///
 /// This method must be called before a call to Start();
 /// </summary>
 /// <param name="opcode">The opcode to handle.</param>
 /// <param name="serverAction">The Action to execute when this opcode is received.</param>
 /// <param name="mustGuard">If true, the execution will be guarded.</param>
 public void RegisterServerAction(NaiadProtocolOpcode opcode, Action <Socket> serverAction, bool mustGuard)
 {
     this.serverActions[opcode] = new GuardedAction(serverAction, mustGuard);
 }
Пример #6
0
 /// <summary>
 /// Registers an action that will be called when a connection is made with the given opcode.
 ///
 /// The action receives the socket for the accepted connection, and is responsible for managing that
 /// resource by e.g. closing it.
 ///
 /// This method must be called before a call to Start();
 /// </summary>
 /// <param name="opcode">The opcode to handle.</param>
 /// <param name="serverAction"></param>
 public void RegisterServerAction(NaiadProtocolOpcode opcode, Action <Socket> serverAction)
 {
     this.serverActions[opcode] = serverAction;
 }