示例#1
0
        /// <summary>
        /// Shutdowns this instance.
        /// </summary>
        public override void Shutdown()
        {
            base.Shutdown();

            ConsoleLogger.WriteToLog("Shutting Server down...");

            IsRunning = false;

            TcpClientConnected.Set();

            if (ListenThread != null)
            {
                ListenThread.Join();
            }

            while (NumClientThreads > 0)
            {
                Clients.Last().Value.ThisThread.Join();
            }

            Clients.Clear();

            if (Server != null)
            {
                Server.Stop();
            }

            ConsoleLogger.WriteToLog("Server successfully Shutdown.");
        }
示例#2
0
        /// <summary>
        /// Events the TCP client accept.
        /// </summary>
        /// <param name="ar">The async result.</param>
        private void EventTcpClientAccept(IAsyncResult ar)
        {
            try
            {
                TcpListener listener  = (TcpListener)ar.AsyncState;
                TcpClient   newClient = listener.EndAcceptTcpClient(ar);
                newClient.ReceiveTimeout = 0;
                Thread t = new Thread(new ParameterizedThreadStart(HandleClient));
                t.IsBackground = true;
                int uniqueID = CreateUniqueClientID();

                ClientConnection sC = new ClientConnection();
                sC.ThisID     = uniqueID;
                sC.ThisClient = newClient;
                sC.ThisThread = t;

                switch (ConnectionType)
                {
                case EConnectionType.SERVER:
                    sC.ConnectionType = EConnectionType.CLIENT;
                    break;
                }

                Clients.Add(uniqueID, sC);
                Clients[uniqueID].ThisThread.Start(Clients[uniqueID]);

                ConsoleLogger.WriteToLog("Client ID: " + uniqueID + " Connected.", true);

                PacketDesc_SetClientID sidpkg = new PacketDesc_SetClientID();
                sidpkg.PacketTarget         = sC.ConnectionType;
                sidpkg.Id                   = uniqueID;
                sidpkg.SenderID             = UniqueID;
                sidpkg.PacketOriginClientID = UniqueID;
                SendPacketToClient(sidpkg, uniqueID);

                PacketDesc_ClientConnected copkg = new PacketDesc_ClientConnected();
                copkg.PacketTarget         = sC.ConnectionType;
                copkg.OtherClientID        = uniqueID;
                copkg.PacketOriginClientID = UniqueID;
                SendPacketToAllClients(copkg, uniqueID);

                UpdateTitle();

                TcpClientConnected.Set();

                if (OnClientAccepted != null)
                {
                    OnClientAccepted(uniqueID);
                }
            }
            catch (ObjectDisposedException)
            {
                return;
            }
        }