Пример #1
0
    // Some sort of TCP connection to the website to handle user pref like which robot, position of the robot, dimensions of the robot, color of the robot, team number of the robot, start/stop game, and select which game mode to run (freeplay, autonomous, teleop, and full match)
    #region TCP server for sending and receiving data
    void startTCPServer()
    {
        int recv;

        byte[]     data = new byte[1024];
        IPEndPoint ipep = new IPEndPoint(IPAddress.Any,
                                         port);

        newsock = new
                  Socket(AddressFamily.InterNetwork,
                         SocketType.Stream, ProtocolType.Tcp);

        newsock.Bind(ipep);
        newsock.Listen(10);
        print("Waiting for a TCP client... On port " + port);
        client = newsock.Accept();
        IPEndPoint clientep =
            (IPEndPoint)client.RemoteEndPoint;

        print("Connected with {0} at port {1}" +
              clientep.Address + clientep.Port);

        //Spawn in robot and update the robot visual
        print("Spawning in robot");
        connected           = true;
        settingDefaultSpawn = true;


        string welcome = "Welcome to my test server";

        data = Encoding.ASCII.GetBytes(welcome);
        client.Send(data, data.Length,
                    SocketFlags.None);
        while (true)
        {
            try
            {
                data = new byte[1024];
                recv = client.Receive(data);
                if (recv == 0)
                {
                    break;
                }

                string message = Encoding.ASCII.GetString(data, 0, recv);
                //print(message);
                if (message != "ping")
                {
                    websiteCommands = WebsiteCommands.CreateFromJSON(message);
                }
            }
            catch (SocketException)
            {
                print("Client lost connection");
                break;
            }
        }
        print("Disconnected from {0}" +
              clientep.Address);

        connected = false;

        client.Close();
        newsock.Close();
        startTCPServer();
    }
Пример #2
0
    // Some sort of TCP connection to the website to handle user pref like which robot, position of the robot, dimensions of the robot, color of the robot, team number of the robot, start/stop game, and select which game mode to run (freeplay, autonomous, teleop, and full match)
    #region TCP server for sending and receiving data
    void startTCPServer()
    {
        int recv;

        byte[]     data = new byte[1024];
        IPEndPoint ipep = new IPEndPoint(IPAddress.Any,
                                         9052);

        newsock = new
                  Socket(AddressFamily.InterNetwork,
                         SocketType.Stream, ProtocolType.Tcp);

        newsock.Bind(ipep);
        newsock.Listen(10);
        print("Waiting for a TCP client...");
        client = newsock.Accept();
        IPEndPoint clientep =
            (IPEndPoint)client.RemoteEndPoint;

        print("Connected with {0} at port {1}" +
              clientep.Address + clientep.Port);


        string welcome = "Welcome to my test server";

        data = Encoding.ASCII.GetBytes(welcome);
        client.Send(data, data.Length,
                    SocketFlags.None);
        while (true)
        {
            try
            {
                data = new byte[1024];
                recv = client.Receive(data);
                if (recv == 0)
                {
                    break;
                }

                string message = Encoding.ASCII.GetString(data, 0, recv);
                //print(message);
                if (message != "ping")
                {
                    websiteCommands = WebsiteCommands.CreateFromJSON(message);
                }

                if (sendingScore)
                {
                    var sendData = new byte[1024];
                    sendingScore = false;
                    var score = new WebsiteScore();
                    score.gameType = websiteCommands.gameType;
                    if (websiteCommands.position <= 1)
                    {
                        score.score = scoreKeeper.getScoreRed();
                    }
                    else
                    {
                        score.score = scoreKeeper.getScoreBlue();
                    }

                    string scoreJSON = JsonUtility.ToJson(score);

                    sendData = Encoding.ASCII.GetBytes(scoreJSON);
                    //client.Send(data, recv, SocketFlags.None);
                    client.Send(sendData, SocketFlags.None);
                }
            }
            catch (SocketException)
            {
                print("Client lost connection");
                break;
            }
        }
        print("Disconnected from {0}" +
              clientep.Address);
        client.Close();
        newsock.Close();
        startTCPServer();
    }