コード例 #1
0
        /// <summary>
        /// Serves for connections acceptance.
        /// </summary>
        /// <param name="socket">Accepted <see cref="Socket"/>.</param>
        internal static void AcceptConnection(Socket socket)
        {
            if (socket == null || !socket.Connected)
            {
                return;
            }

            NetworkHelper.RemoteServiceInfo info = NetworkHelper.GetServiceInfo(socket);

            if (info.ServiceType == ServiceType.Undefined)
            {
                Console.WriteLine("Connection rejected for remote connection from {0}, service was not recognized.", socket.RemoteEndPoint);
                NetworkHelper.CloseSocket(ref socket);
                return;
            }

            if (m_ActiveConnections.ContainsKey(info.ServiceId))
            {
                Console.WriteLine("{0} with id 0x{1} already connected, skipping connection request.", info.ServiceType, info.ServiceId);
                NetworkHelper.CloseSocket(ref socket);
                return;
            }

            InnerNetworkClient client = null;

            switch (info.ServiceType)
            {
            case ServiceType.LoginService:
            {
                client = new InnerNetworkClient(info.ServiceId, info.ServiceType, socket);
                client.OnDisconnected += new OnDisconnectedEventHandler(OnRemoteConnectionError);
                client.HandleDelegate  = new LoginServiceRequestsHandlers(ref client).Handle;

                break;
            }

            case ServiceType.GameService:
            {
                client = new InnerNetworkClient(info.ServiceId, info.ServiceType, socket);
                client.OnDisconnected += new OnDisconnectedEventHandler(OnRemoteConnectionError);
                client.HandleDelegate  = new GameServiceRequestsHandlers(ref client).Handle;
                break;
            }

            case ServiceType.NpcService:
            {
                client = new InnerNetworkClient(info.ServiceId, info.ServiceType, socket, NpcServiceRequestsHandlers.Handle);
                break;
            }

            default:
            {
                throw new InvalidOperationException();
            }
            }

            Logger.WriteLine(Source.InnerNetwork, "Connection accepted for {0} (0x{1})", client.ServiceType, client.ServiceID.ToString("x2"));
            client.Send(m_ResponseAccepted);
            client.BeginReceive();
            m_ActiveConnections.Add(info.ServiceId, client);
        }