Beispiel #1
0
        private void tcpClientConnectCallback(IAsyncResult ar)
        {
            try
            {
                if (tcpClient.Connected)
                {
                    var config = (AppConfiguration)ar.AsyncState;

                    localIPAddress = (tcpClient.Client.LocalEndPoint as IPEndPoint).Address;

                    sendMessage(new ConnectMessage(new BroadcastClient(config.ClientName, null)));
                    messageReader.ReadAsync(true);

                    IsConnecting = false;
                    IsConnected  = true;
                }
                else
                {
                    IsConnecting = false;
                    IsConnected  = false;
                }
            }
            catch (Exception ex)
            {
                tcpClient.Close();

                IsConnecting = false;
                IsConnected  = false;

                Logger.log.Error(ex);
            }
        }
Beispiel #2
0
 public void StartTcpListener()
 {
     try
     {
         tcpListener.Start();
         while (true)
         {
             NetworkMessageReader netMessageReader = new NetworkMessageReader(tcpListener.AcceptTcpClient());
             netMessageReader.ReadCompleted += NetMessageReader_CatanClientAuth_Request_ReadCompleted;
             netMessageReader.ReadError     += (obj, e) => { e.TcpClient.Close(); };
             netMessageReader.ReadAsync();
         }
     }
     catch (SocketException socketEx)
     {
         if (socketEx.SocketErrorCode == SocketError.Interrupted)
         {
             // Tcplistener wurde geschlossen. Durch Stop()
             while (true)
             {
                 Console.ReadLine();           // Server läuft weiter ...
             }
         }
         else
         {
             iLogicLayer.ThrowException(socketEx);
         }
     }
     catch (Exception ex)
     {
         iLogicLayer.ThrowException(ex);
     }
 }
Beispiel #3
0
        private void NetMessageWriter_CatanClientAuth_Response_WriteCompleted(object obj, NetworkMessageWriterWriteCompletedEventArgs e)
        {
            //getDisconnectedClientConnections(silently:true);

            // new catan client
            CatanClient catanClient = new CatanClient(e.TcpClient, e.TcpClient.Client.RemoteEndPoint.ToString(), (e.NetMessage as CatanClientAuthenticationResponseMessage).AuthRequestMessage.Playername);

            catanClients.Add(catanClient);

            Console.WriteLine($"Catan player joined: {catanClient.Name}");
            if (catanClients.Count == maxClients)
            {
                // Start game
                tcpListener.Stop();
                Thread.Sleep(1000);

                catanClients.ForEach(client =>
                {
                    var netReader            = new NetworkMessageReader(client.TcpClient);
                    netReader.ReadCompleted += NetMessageReader_CatanClientMessageReceived;
                    netReader.ReadAsync(readLoop: true);
                }); // error handling wird ignoriert !!!

                iLogicLayer.ServerFinishedListening(catanClients);
            }
        }
Beispiel #4
0
        public void AcceptClientsForEver()
        {
            lock (tcpListener)
            {
                tcpListener.Start();
            }
            if (nicNotifier == null)
            {
                nicNotifier = new NetworkInterfaceStateNotifier(5, localEndPoint.Address);
                nicNotifier.NetworkInterfaceIsNotUpEvent += NicNotifier_NetworkInterfaceIsNotUpEvent;
                nicNotifier.Start();
            }

            while (true)
            {
                try
                {
                    Logger.log.Info("Waiting for clients ...");

                    NetworkMessageReader messageReader = new NetworkMessageReader(tcpListener.AcceptTcpClient());
                    messageReader.ReadCompleted += MessageReader_ReadCompleted;
                    messageReader.ReadError     += MessageReader_ReadError;

                    messageReader.OnErrorStopReadingAndCloseClient = true;
                    messageReader.ReadAsync(true);
                }
                catch (Exception ex)
                {
                    Logger.log.Error(ex);
                    bool needTcpListenerRestart = true;
                    while (needTcpListenerRestart)
                    {
                        while (restartTcpListener())
                        {
                            needTcpListenerRestart = false;
                            break;
                        }
                        Thread.Sleep(5000); // wait 5 sec
                    }
                }
            }
        }