internal void Runner()
        {
            peer.AddDeveIncomingMessage(new DeveIncomingMessage(this, new byte[2] { (byte)DeveMessageType.StatusChanged, (byte)NetworkStatus.Connected }));

            Thread tr = new Thread(Sender);
            tr.Start();

            while (!shouldShutdown && tcpClient.Connected)
            {
                try
                {
                    byte[] firstLengthIntByteArray = ReadThisAmmountOfBytes(4);
                    int bytesToRead = BitConverter.ToInt32(firstLengthIntByteArray, 0);

                    if (bytesToRead > peer.MaxMessageSize)
                    {
                        Console.WriteLine("Warning: I'm gonna receive a big message of the size: " + bytesToRead);
                        //throw new InvalidDataException("This message is probably a bit big :), the size is: " + bytesToRead + " max message size is: " + this.maxMessageSize);
                    }

                    byte[] data = ReadThisAmmountOfBytes(bytesToRead);
                    DeveIncomingMessage devIn = new DeveIncomingMessage(this, data);
                    peer.AddDeveIncomingMessage(devIn);

                }
                catch (SocketException e)
                {
                    Console.WriteLine("Socket exception: " + e.ToString());
                    break;
                }
                catch (EndOfStreamException e)
                {
                    Console.WriteLine("Exception that happens when a client disconnects nice and safe: " + e.ToString());
                    break;
                }
                catch (IOException e)
                {
                    Console.WriteLine("IOException: " + e.ToString());
                    break;
                }
            }

            if (peer is DeveServer)
            {
                ((DeveServer)peer).RemoveClient(this);
            }

            peer.AddDeveIncomingMessage(new DeveIncomingMessage(this, new byte[2] { (byte)DeveMessageType.StatusChanged, (byte)NetworkStatus.Disconnected }));
        }
Exemple #2
0
 internal void AddDeveIncomingMessage(DeveIncomingMessage devInc)
 {
     messages.Enqueue(devInc);
 }
Exemple #3
0
        private void HandleDataMessage(DeveIncomingMessage inc)
        {
            ServerSendMessageType messageType = (ServerSendMessageType)inc.ReadInt32();

            //string IDString = ""; //ID of the brokemonzo, only used in case 1, 2 and 3, not 0. NOT 0 ;)

            switch (messageType)
            {
                case ServerSendMessageType.SetScreenSize:
                    {
                        curSize.X = inc.ReadInt32();
                        curSize.Y = inc.ReadInt32();
                        curSize.Width = inc.ReadInt32();
                        curSize.Height = inc.ReadInt32();

                        totSize.X = inc.ReadInt32();
                        totSize.Y = inc.ReadInt32();
                        totSize.Width = inc.ReadInt32();
                        totSize.Height = inc.ReadInt32();
                    }
                    break;
                case ServerSendMessageType.MapString:
                    mapWidth = inc.ReadInt32();
                    mapHeight = inc.ReadInt32();
                    for (int y = 0; y < mapHeight; y++)
                    {
                        String line = inc.ReadString();
                        List<String> xlist = new List<string>();
                        map.Add(xlist);
                        foreach (Char c in line)
                        {
                            xlist.Add(c.ToString());
                        }
                    }
                    break;
                case ServerSendMessageType.SpawnNewPlayer:
                    {
                        String idString = inc.ReadString(); //ID van het brokemonnetje

                        int xPos = inc.ReadInt32();
                        int yPos = inc.ReadInt32();
                        int xDir = inc.ReadInt32();
                        int yDir = inc.ReadInt32();
                        int brokeSpecies = inc.ReadInt32(); //0 = squirtle, 1 = charmander, 2 = bulbasaur, 10 = missingno

                        Texture2D[] brokemonTexture = new Texture2D[3];
                        //int rInt = 0;
                        //Random r = new Random();
                        //rInt = r.Next(3);

                        switch (brokeSpecies)
                        {
                            case 0:
                                brokemonTexture[0] = squirtleTexture;
                                brokemonTexture[1] = squirtleTexture;//replace plx
                                brokemonTexture[2] = squirtleTexture;//replace plx
                                break;
                            case 1:
                                brokemonTexture[0] = charmanderTexture;
                                brokemonTexture[1] = charmanderTexture;//replace plx
                                brokemonTexture[2] = charmanderTexture;//replace plx
                                break;
                            case 2:
                                brokemonTexture[0] = bulbasaurTexture;
                                brokemonTexture[1] = venubronkiTexture;
                                brokemonTexture[2] = venubrateuzadroidTexture;
                                break;
                            default:
                                Console.WriteLine("Invalid selection. Please select 0, 1, or 2.");
                                brokemonTexture[0] = myssignuvskitrabovTexture;
                                brokemonTexture[1] = myssignuvskitrabovTexture;
                                brokemonTexture[2] = myssignuvskitrabovTexture;
                                break;
                        }
                        //if (r.Next(15) == 10)
                        //{
                        //    brokemonTexture[0] = myssignuvskitrabovTexture;
                        //    brokemonTexture[1] = myssignuvskitrabovTexture;
                        //    brokemonTexture[2] = myssignuvskitrabovTexture;
                        //}
                        brokemons2.Add(idString, new Brokemon(new Vector2((float)xDir * 1f / 1000.0f, (float)yDir * 1f / 1000.0f), new Vector2(xPos, yPos), brokemonTexture, spriteBatch, idString));
                    }
                    break;
                case ServerSendMessageType.RemovePlayer:
                    {
                        String idString = inc.ReadString(); //ID van het brokemonnetje

                        brokemons2.Remove(idString);
                    }
                    break;
                case ServerSendMessageType.SetPlayerLocation:
                    {
                        String idString = inc.ReadString(); //ID van het brokemonnetje
                        //SetLocation Broketje
                        int xPos = inc.ReadInt32();
                        int yPos = inc.ReadInt32();
                        int xSpeed = inc.ReadInt32();
                        int ySpeed = inc.ReadInt32();
                        Brokemon brokemon = brokemons2[idString];
                        brokemon.Position = new Vector2(xPos, yPos);
                        brokemon.xSpeed = xSpeed;
                        brokemon.ySpeed = ySpeed;
                    }
                    break;
                default:
                    break;
            }
        }
Exemple #4
0
        public void HandleDataMessage(DeveIncomingMessage inc)
        {
            ServerReceiveMessageType messageType = (ServerReceiveMessageType)inc.ReadInt32();

            switch (messageType)
            {
                case ServerReceiveMessageType.LoginMessageScreenClient:
                    {
                        DebugMSG("Screen client connected");

                        int xscr = random.Next(100, 1000);
                        int yscr = random.Next(100, 1000);
                        int widthscr = inc.ReadInt32();
                        int heightscr = inc.ReadInt32();

                        ScreenClientje screenClientje = new ScreenClientje(new Rectangle(xscr, yscr, widthscr, heightscr), this, inc.Sender);
                        screenClientjes.Add(inc.Sender, screenClientje);
                        allClientjes.Add(inc.Sender, screenClientje);

                        DeveOutgoingMessage outje = new DeveOutgoingMessage();
                        outje.WriteInt32((int)ServerSendMessageType.MapString);
                        outje.WriteInt32(map[0].Count);
                        outje.WriteInt32(map.Count);
                        foreach (var mapline in map)
                        {
                            StringBuilder build = new StringBuilder();

                            foreach (String str in mapline)
                            {
                                build.Append(str);
                            }

                            outje.WriteString(build.ToString());
                        }

                        screenClientje.deveConnection.Send(outje);

                        foreach (var controlClientje in controlClientjes.Values)
                        {
                            outje = new DeveOutgoingMessage();
                            outje.WriteInt32((int)ServerSendMessageType.SpawnNewPlayer); //Add beestje bij alle screens
                            outje.WriteString(controlClientje.guid);

                            screenClientje.deveConnection.Send(outje);

                            outje = new DeveOutgoingMessage();
                            outje.WriteInt32((int)ServerSendMessageType.SetPlayerLocation);
                            outje.WriteString(controlClientje.guid);
                            outje.WriteInt32((int)controlClientje.posx);
                            outje.WriteInt32((int)controlClientje.posy);
                            outje.WriteInt32(0);
                            outje.WriteInt32(0);

                            screenClientje.deveConnection.Send(outje);
                        }
                    }
                    break;
                case ServerReceiveMessageType.LoginMessageControlClient:
                    {
                        DebugMSG("Het is een Android :)");

                        ControlClientje controlClientje = new ControlClientje(inc.Sender, this);
                        controlClientjes.Add(inc.Sender, controlClientje);
                        allClientjes.Add(inc.Sender, controlClientje);

                        DeveOutgoingMessage outje = new DeveOutgoingMessage();
                        outje.WriteInt32((int)ServerSendMessageType.SpawnNewPlayer); //Add beestje bij alle screens
                        outje.WriteString(controlClientje.guid);

                        SendToScreens(outje);
                    }
                    break;
                case ServerReceiveMessageType.NewButtonState:
                    {
                        ControlClientje curControlClient = controlClientjes[inc.Sender];

                        String direction = inc.ReadString();
                        String onOrOffString = inc.ReadString();
                        Boolean onOrOff = onOrOffString == "True";

                        DebugMSG("Got message with: " + direction + ": " + onOrOff);

                        if (direction == "left")
                        {
                            curControlClient.LeftArrow = onOrOff;
                        }
                        else if (direction == "right")
                        {
                            curControlClient.RightArrow = onOrOff;
                        }
                        else if (direction == "up")
                        {
                            curControlClient.UpArrown = onOrOff;
                        }
                        else if (direction == "down")
                        {
                            curControlClient.DownArrow = onOrOff;
                        }

                        //DebugMSG("X: " + curControlClient.posx + " Y: " + curControlClient.posy);

                        //DeveOutgoingMessage outje = new DeveOutgoingMessage();
                        //outje.WriteInt32((int)ServerSendMessageType.SetPlayerLocation);
                        //outje.WriteString(curControlClient.guid);
                        //outje.WriteInt32((int)curControlClient.posx);
                        //outje.WriteInt32((int)curControlClient.posy);

                        //SendToScreens(outje);
                        break;
                    }
                default:
                    DebugMSG("Unknown message type");
                    break;
            }
        }