Exemplo n.º 1
0
 private void Process(object s, SocketAsyncEventArgs e)
 {
     try
     {
         if (e.SocketError == SocketError.Success)
         {
             ServerClient T = new ServerClient(e.AcceptSocket, CBufferSize, CMaxPacketSize);
             lock (CClients)
             {
                 if (CClients.Count < CMaxConnections)
                 {
                     CClients.Add(T);
                     T.StateChanged         += HandleStateChanged;
                     T.ExceptionThrown      += OnClientExceptionThrown;
                     T.ReadPacket           += OnClientReadPacket;
                     T.ReadProgressChanged  += OnClientReadProgressChanged;
                     T.WritePacket          += OnClientWritePacket;
                     T.WriteProgressChanged += OnClientWriteProgressChanged;
                     OnClientStateChanged(T, true);
                 }
                 else
                 {
                     T.Disconnect();
                 }
             }
             e.AcceptSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
             {
                 NoDelay = true
             };
             if (CKeepAlive)
             {
                 e.AcceptSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
             }
             if (!Handle.AcceptAsync(e))
             {
                 Process(null, e);
             }
         }
         else
         {
             OnExceptionThrown(new SocketException((int)e.SocketError));
             Disconnect();
         }
     }
     catch (Exception ex)
     {
         OnExceptionThrown(ex);
         Disconnect();
     }
 }
Exemplo n.º 2
0
        private void ListenForClients()
        {
            //simulrate listening tread crash
            int          numconnected = 0;
            TcpClient    tcpClient    = null;
            ServerClient serverClient = null;

            while (_isActive)
            {
                try
                {
                    if (_listener == null)
                    {
                        _listener = new TcpListener(IPAddress.Any, _port);
                        _listener.Start();
                        TalkyLog.Debug("Server|Listener|Started|");
                    }

                    TalkyLog.Debug("Server|Listener|Accepting|");
                    tcpClient    = _listener.AcceptTcpClient();
                    serverClient = new ServerClient(tcpClient);

                    if (_channelRepository.GetLobby() == null)
                    {
                        serverClient.Disconnect("§2Server Error: No Lobby!");
                        continue;
                    }

                    numconnected++;
                    if (UserCount > 1)
                    {
                        throw new ApplicationException("Simulate crash");
                    }

                    var clientThread = new Thread(new ServerConnection(serverClient).HandleMessages);
                    clientThread.Start();
                }
                catch (System.Exception ex)
                {
                    TalkyLog.Debug("Server|Listener|Crashed");
                    serverClient?.Disconnect();
                    tcpClient?.Close();
                    TalkyLog.Debug(ex.ToString());
                }
            }
        }
        private static void ClientAntwort(ServerClient Client, TCP_Protocoll Antwort)
        {
            Console.WriteLine("##--##--##--##--##--##--##--##--##--##--##");
            string EnumName = Enum.GetName(typeof(TCP_Protocoll.HeaderData), Antwort.ProtocolType);

            switch (Antwort.ProtocolType)
            {
            case TCP_Protocoll.HeaderData.Error:
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("ERROR");
                Console.WriteLine("Client ID: " + Client.ClientID.ToString());
                Console.WriteLine("Type: " + EnumName);
                Console.WriteLine("Int-Type: " + ((int)Antwort.ProtocolType).ToString());
                Console.WriteLine("Message: " + Antwort.Data);
                Console.ForegroundColor = ConsoleColor.White;
                Client.Disconnect();
                break;

            case TCP_Protocoll.HeaderData.Broadcast:
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("BROADCAST");
                ServerClient.SendToAllClients(Antwort, Client.ClientID);
                Console.WriteLine("Client ID: " + Client.ClientID.ToString());
                Console.WriteLine("Type: " + EnumName);
                Console.WriteLine("Message: " + Antwort.Data);
                Console.ForegroundColor = ConsoleColor.White;
                break;

            default:
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("Client ID: " + Client.ClientID.ToString());
                Console.WriteLine("Type: " + EnumName);
                Console.WriteLine("Message: " + Antwort.Data);
                Console.ForegroundColor = ConsoleColor.White;
                break;
            }
        }