Beispiel #1
0
        public void Receive(object client)
        {
            TcpClient     tcpClient    = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            byte[] message = new byte[8192];
            int    bytesRead;

            while (true)
            {
                bytesRead = 0;
                //System.Threading.Thread.Sleep(Program.Random.Next(50, 200));
                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 8192);
                }
                catch
                {
                    //a socket error has occured
                    break;
                }

                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }

                //message has successfully been received
                string info = "";
                try
                {
                    decoder.PushMsg(Encoding.Unicode.GetString(message, 0, bytesRead));                    //encoder.GetString(message, 0, bytesRead));
                    info = decoder.GetMsg();
                    while (info != null)
                    {
                        //System.Console.WriteLine(info);
                        Message msg = Message.Decode(info);
                        if (Connected || msg is MSGRequired)
                        {
                            msg.HandleMsg();
                        }
                        info = decoder.GetMsg();
                    }
                }
                catch (MultiPlayerError)
                {
                    break;
                }
                catch (SameNameError)                 //I have conflict with some one in the game, will close, and abort.
                {
                    if (MPManager.Simulator.Confirmer != null)
                    {
                        MPManager.Simulator.Confirmer.Error(MPManager.Catalog.GetString("Connection to the server is lost, will play as single mode"));
                    }
                    MPManager.Client = null;
                    tcpClient.Close();
                    listenThread.Abort();
                }
                catch (Exception e)
                {
                    System.Console.WriteLine(e.Message + e.StackTrace);
                    Trace.TraceWarning(e.Message + e.StackTrace);
                }
            }
            if (MPManager.Simulator.Confirmer != null)
            {
                MPManager.Simulator.Confirmer.Error(MPManager.Catalog.GetString("Connection to the server is lost, will play as single mode"));
            }
            try
            {
                foreach (var p in MPManager.OnlineTrains.Players)
                {
                    MPManager.Instance().AddRemovedPlayer(p.Value);
                }
            }
            catch (Exception) { }

            //no matter what, let player gain back the control of the player train
            if (MPManager.Simulator.PlayerLocomotive != null && MPManager.Simulator.PlayerLocomotive.Train != null)
            {
                MPManager.Simulator.PlayerLocomotive.Train.TrainType      = Train.TRAINTYPE.PLAYER;
                MPManager.Simulator.PlayerLocomotive.Train.LeadLocomotive = MPManager.Simulator.PlayerLocomotive;
            }
            if (MPManager.Simulator.Confirmer != null)
            {
                MPManager.Simulator.Confirmer.Information(MPManager.Catalog.GetString("Alt-E to gain control of your train"));
            }

            MPManager.Client = null;
            tcpClient.Close();
            listenThread.Abort();
        }
Beispiel #2
0
        private async Task Connection(IPAddress address, int port)
        {
            byte[] buffer = new byte[8192];
            int    bytesRead;
            string content;

            try
            {
                await client.ConnectAsync(address, port).ConfigureAwait(false);

                NetworkStream networkStream = client.GetStream();

                while (client.Connected && !cts.Token.IsCancellationRequested)
                {
                    bytesRead = await networkStream.ReadAsync(buffer.AsMemory(0, buffer.Length), cts.Token).ConfigureAwait(false);

                    if (bytesRead == 0)
                    {
                        break;
                    }

                    decoder.PushMsg(Encoding.Unicode.GetString(buffer, 0, bytesRead));

                    while ((content = decoder.GetMsg()) != null)
                    {
                        try
                        {
                            Message message = Message.Decode(content);
                            if (Connected || message is MSGRequired)
                            {
                                message.HandleMsg();
                            }
                        }
                        catch (Exception ex) when(ex is InvalidDataException)
                        {
                        }
                    }
                }
            }
            catch (Exception ex) when(ex is MultiPlayerException)
            {
            }
            catch (Exception ex) when(ex is SocketException || ex is IOException ||
                                      ex is OperationCanceledException || ex is TaskCanceledException)
            {
            }

            Simulator.Instance.Confirmer?.Error(CatalogManager.Catalog.GetString("Connection to the server is lost, will play as single mode"));
            foreach (System.Collections.Generic.KeyValuePair <string, OnlinePlayer> p in MultiPlayerManager.OnlineTrains.Players)
            {
                MultiPlayerManager.Instance().AddRemovedPlayer(p.Value);
            }

            //no matter what, let player gain back the control of the player train
            if (Simulator.Instance.PlayerLocomotive?.Train != null)
            {
                Simulator.Instance.PlayerLocomotive.Train.TrainType      = TrainType.Player;
                Simulator.Instance.PlayerLocomotive.Train.LeadLocomotive = Simulator.Instance.PlayerLocomotive;
            }
            Simulator.Instance.Confirmer?.Information(CatalogManager.Catalog.GetString("Alt-E to gain control of your train"));

            MultiPlayerManager.Client = null;
            client.Close();
        }
Beispiel #3
0
        public void Receive(object client)
        {
            NetworkStream clientStream = Client.GetStream();

            byte[] message = new byte[8192];
            int    bytesRead;
            int    errorCount     = 0;
            double firstErrorTick = 0;
            double nowTicks       = 0;

            while (true)
            {
                //System.Threading.Thread.Sleep(Program.Random.Next(50, 200));

                bytesRead = 0;

                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 8192);
                }
                catch
                {
                    //a socket error has occured
                    break;
                }

                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }

                //message has successfully been received
                string info = "";
                try
                {
                    decoder.PushMsg(Encoding.Unicode.GetString(message, 0, bytesRead));                    //encoder.GetString(message, 0, bytesRead));
                    info = decoder.GetMsg();
                    while (info != null)
                    {
                        //System.Console.WriteLine(info);
                        Message msg = Message.Decode(info);
                        if (msg is MSGPlayer)
                        {
                            ((MSGPlayer)msg).HandleMsg(this);
                        }
                        else
                        {
                            msg.HandleMsg();
                        }

                        info = decoder.GetMsg();
                    }
                }
                catch (MultiPlayerError)
                {
                    break;
                }
                catch (SameNameError)
                {
                    Client.Close();
                    thread.Abort();
                }
                catch (Exception)
                {
                    nowTicks = MPManager.Simulator.GameTime;
                    if (firstErrorTick == 0)
                    {
                        firstErrorTick = nowTicks;
                        errorCount     = 1;
                    }
                    if (errorCount >= 5 && nowTicks - firstErrorTick < 10)                     //5 errors last 10 seconds
                    {
                        MSGMessage emsg = new MSGMessage(this.Username, "Error", "Too many errors received from you in a short period of time.");
                        MPManager.BroadCast(emsg.ToString());
                        break;
                    }
                    else if (errorCount < 5)
                    {
                        errorCount++;
                    }
                    else
                    {
                        firstErrorTick = nowTicks; errorCount = 0;
                    }
                    //System.Console.WriteLine(e.Message + info);
                }
            }

            System.Console.WriteLine("{0} quit", this.Username);
            if (MPManager.Simulator.Confirmer != null)
            {
                MPManager.Simulator.Confirmer.Information(MPManager.Catalog.GetStringFmt("{0} quit.", this.Username));
            }
            Client.Close();
            if (this.Train != null && this.status != Status.Removed)             //remember the location of the train in case the player comes back later, if he is not removed by the dispatcher
            {
                if (!MPManager.Instance().lostPlayer.ContainsKey(this.Username))
                {
                    MPManager.Instance().lostPlayer.Add(this.Username, this);
                }
                this.quitTime       = MPManager.Simulator.GameTime;
                this.Train.SpeedMpS = 0.0f;
                this.status         = Status.Quit;
            }
            MPManager.Instance().AddRemovedPlayer(this);            //add this player to be removed
            MPManager.BroadCast((new MSGQuit(this.Username)).ToString());
            thread.Abort();
        }