Ejemplo n.º 1
0
        private void ClientReceiver()
        {
            Byte[] bytes = new Byte[1024 * 16];
            while (receiverRun)
            {
                if (StoppedTcp)
                {
                    break;
                }
                else if (receiveTcp.Pending())
                {
                    try
                    {
                        LastReceiveTimeFromHost = DateTime.Now;
                        TcpClient     client = receiveTcp.AcceptTcpClient();
                        NetworkStream stream = client.GetStream();
                        int           i;
                        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            if (!connect_to_host)
                            {
                                client.Close();
                                break;
                            }

                            /*BinaryFormatter bin = new BinaryFormatter();
                             * MemoryStream mem = new MemoryStream(bytes);*/
                            Command c = new Command(bytes);
                            if (c.Command_Code == CommandCode.Standby)
                            {
                                //do nothing
                            }
                            //else if (c.Command_Code == CommandCode.Chat_Message)
                            //{
                            //    chatDisplayTextbox.Text += c.Message;
                            //}
                            else if (c.Command_Code == CommandCode.Update_Room)
                            {
                                this.room = (Room)c.Data1;
                                UpdateRoom();
                            }
                            else if (c.Command_Code == CommandCode.Start_Game)
                            {
                                room = (Room)c.Data1;
                                ScreenEvent.Invoke(this, new SivEventArgs(1));
                            }
                            else if (c.Command_Code == CommandCode.Character_Distribute)
                            {
                                characterPlayerList = (List <Character>)c.Data1;
                                UpdateSelectImageList();
                            }
                        }
                    }
                    catch
                    {
                        ScreenEvent.Invoke(this, new SivEventArgs(0));
                        Game1.MessageBox(new IntPtr(0), "Connection Error!", "Connection Error!", 0);
                    }
                }
                else if ((DateTime.Now - LastReceiveTimeFromHost) > new TimeSpan(0, 0, 5))
                {
                    try
                    {
                        Command c = new Command(CommandCode.Check_Connect);
                        c.SendData(room.Player_List[room.owner_index].Address, 51002);
                    }
                    catch
                    {
                        Game1.MessageBox(new IntPtr(0), "Disconected from host", "Disconnected", 0);
                        ScreenEvent.Invoke(this, new SivEventArgs(0));
                    }
                }
            }
        }