Пример #1
0
        //port is 11000
        static void Main(string[] args)
        {
            Task.Run(async() =>
            {
                var trainer = new Trainer();
                trainer.Initialize();

                Console.WriteLine("Are you the 'server' or 'client'? (type server or client)");
                if (Console.ReadLine().Equals("server"))
                {
                    var server = new Server();
                    server.StartServer(trainer);
                }
                else
                {
                    var client = new Client();
                    await client.StartClient(trainer);
                }
            }).GetAwaiter().GetResult();
        }
Пример #2
0
        public async Task StartClient(Trainer trainer)
        {
            //Request server IP and port number
            Console.WriteLine("Please enter the server IP in the format 192.168.0.1 and press return:");
            string ip = Console.ReadLine();

            Console.WriteLine("Please enter the server port and press return:");
            string port = Console.ReadLine();

            ISocket server = AweSock.TcpConnect(ip, int.Parse(port));

            var inBuf  = AwesomeSockets.Buffers.Buffer.New(99999);
            var outBuf = AwesomeSockets.Buffers.Buffer.New(99999);

            this.SetupClientTrainer(trainer);

            while (true)
            {
                try
                {
                    //get data
                    var outputData = this.GetOutputData(trainer);

                    //write data to buffer
                    AwesomeSockets.Buffers.Buffer.ClearBuffer(outBuf);
                    AwesomeSockets.Buffers.Buffer.Add(outBuf, Utils.ObjectToByteArray(outputData));
                    AwesomeSockets.Buffers.Buffer.FinalizeBuffer(outBuf);

                    //send data
                    int bytesSent = AweSock.SendMessage(server, outBuf);
                    Console.WriteLine("sent data");

                    //get response
                    Stopwatch stopWatch = new Stopwatch();
                    stopWatch.Start();

                    Tuple <int, EndPoint> received = AweSock.ReceiveMessage(server, inBuf);
                    stopWatch.Stop();

                    TimeSpan ts = stopWatch.Elapsed;
                    trainer.timeSinceUpdate = ts.Milliseconds;

                    AwesomeSockets.Buffers.Buffer.FinalizeBuffer(inBuf);

                    //parse response
                    var res = Utils.Deserialize <Dictionary <string, byte[]> >(AwesomeSockets.Buffers.Buffer.GetBuffer(inBuf));
                    AwesomeSockets.Buffers.Buffer.ClearBuffer(inBuf);

                    //act on response
                    //Console.WriteLine(string.Join(", ", res["pos_ally"]));
                    this.HandleInputData(res, trainer);

                    //refresh rate
                    Thread.Sleep(50);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);

                    AwesomeSockets.Buffers.Buffer.ClearBuffer(outBuf);
                    AwesomeSockets.Buffers.Buffer.ClearBuffer(inBuf);

                    trainer.Initialize();
                    Thread.Sleep(50);
                }
            }
        }
Пример #3
0
        public void StartServer(Trainer trainer)
        {
            Console.WriteLine("Please enter the server port and press return:");
            string port = Console.ReadLine();


            ISocket listenSocket = AweSock.TcpListen(int.Parse(port));

            Console.WriteLine("Server started");
            ISocket client = AweSock.TcpAccept(listenSocket);

            var inBuf  = AwesomeSockets.Buffers.Buffer.New(99999);
            var outBuf = AwesomeSockets.Buffers.Buffer.New(99999);

            while (true)
            {
                try
                {
                    //get message
                    Stopwatch stopWatch = new Stopwatch();
                    stopWatch.Start();

                    Tuple <int, EndPoint> received = AweSock.ReceiveMessage(client, inBuf);

                    stopWatch.Stop();

                    TimeSpan ts = stopWatch.Elapsed;
                    trainer.timeSinceUpdate = ts.Milliseconds;

                    AwesomeSockets.Buffers.Buffer.FinalizeBuffer(inBuf);

                    //parse message
                    var res = Utils.Deserialize <Dictionary <string, byte[]> >(AwesomeSockets.Buffers.Buffer.GetBuffer(inBuf));
                    AwesomeSockets.Buffers.Buffer.ClearBuffer(inBuf);

                    //act on message
                    this.HandleInputData(res, trainer);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);

                    AwesomeSockets.Buffers.Buffer.ClearBuffer(outBuf);
                    AwesomeSockets.Buffers.Buffer.ClearBuffer(inBuf);
                    trainer.Initialize();
                }

                try {
                    //get response data
                    var outputData = this.GetOutputData(trainer);

                    //write to buffer
                    AwesomeSockets.Buffers.Buffer.ClearBuffer(outBuf);
                    AwesomeSockets.Buffers.Buffer.Add(outBuf, Utils.ObjectToByteArray(outputData));
                    AwesomeSockets.Buffers.Buffer.FinalizeBuffer(outBuf);

                    //respond
                    int bytesSent = AweSock.SendMessage(client, outBuf);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);

                    AwesomeSockets.Buffers.Buffer.ClearBuffer(outBuf);
                    AwesomeSockets.Buffers.Buffer.ClearBuffer(inBuf);
                    trainer.Initialize();
                }
            }

            Console.ReadLine();
        }