Esempio n. 1
0
        public virtual async Task<Response> RequestAsync(ServerAddress address, byte[] data, params char[] messageHeader)
        {            
            try
            {
                var cts = new CancellationTokenSource(1000);
                var result = await ReceiveDataAsync(address, data, cts.Token, messageHeader);

                return new Response
                {
                    Data = result.Data,
                    IsTimeout = false,
                    Ping = result.Ping,
                    MessageHeaderChar = result.MessageHeaderChar,
                };
            }
            catch (OperationCanceledException)
            {
                return new Response
                {
                    Data = null,
                    IsTimeout = true,
                    Ping = -1,
                };
            }
        }
Esempio n. 2
0
        public async Task<Server> InitServerAsync(string ip, int port)
        {
            var serverAddress = new ServerAddress(ip, port);
            var engineFactory = await detector.DetectAsync(serverAddress);

            return new Server(serverAddress, engineFactory, logListener);
        }
Esempio n. 3
0
 private void LogListener_OnLogMessageReceived(ServerAddress address, DateTime date, string message)
 {
     if (address == Address)
     {
         OnLogMessageReceived(date, message);
     }
 }
Esempio n. 4
0
 public void RemoveServer(ServerAddress address)
 {
     var serverToRemove = servers.FirstOrDefault(s => s.Ip == address.Ip && s.Port == address.Port);
     if (serverToRemove != null)
     {
         servers.Remove(serverToRemove);
     }
 }
Esempio n. 5
0
        public void TestEquality()
        {
            var address1 = new ServerAddress("0.1.2.3", 1234);
            var address2 = new ServerAddress("0.1.2.3", 1234);

            Assert.That(address1, Is.EqualTo(address2));
            Assert.That(address1 == address2, Is.True);
        }
Esempio n. 6
0
 public Server(ServerAddress address, EngineFactory engineFactory, LogListener logListener)
 {
     this.requester = new UDPRequester(engineFactory);
     this.dataParser = engineFactory.CreateDataParser();
     this.logListener = logListener;
     Address = address;
     Rcon = new Rcon(Address);
     logListener.OnLogMessageReceived += LogListener_OnLogMessageReceived;
 }
Esempio n. 7
0
 public Rcon(ServerAddress serverAddress)
 {
     this.serverAddress = serverAddress;
     client = new TcpClient();       
 }
Esempio n. 8
-1
        private async Task<ReceiveDataInfo> ReceiveDataAsync(ServerAddress address, byte[] data, CancellationToken ct, params char[] header)
        {
            var udpClient = new UdpClient();
            var ipAddr = IPAddress.Parse(address.Ip);
            try
            {
                udpClient.Connect(ipAddr, address.Port);

                var sendTime = DateTime.Now;
                await udpClient.SendAsync(data, data.Length);

                Packet packet = null;            
                return await Task.Run(async () =>
                {
                    while (true)
                    {
                        var clientResult = await udpClient.ReceiveAsync().WithCancellation(ct);
                        var remoteIp = clientResult.RemoteEndPoint.Address.ToString();
                        var remotePort = clientResult.RemoteEndPoint.Port;
                        var receiveDate = DateTime.Now;
                        var packetReader = new PacketReader(clientResult.Buffer);
                        var packetheader = packetReader.ReadLong();
                        if (packet == null)
                        {
                            packet = engineFactory.CreatePacket(packetheader);
                        }
                        packet.AddData(clientResult.Buffer);
                        if (packet.IsCompleted)
                        {
                            var messageReader = new PacketReader(packet.Payload);
                            var messageheader = messageReader.ReadByte();
                            var messageHeaderChar = Convert.ToChar(messageheader);
                            if (header.Contains(messageHeaderChar))
                            {
                                var result = new ReceiveDataInfo
                                {
                                    Ping = (int)(DateTime.Now - sendTime).TotalMilliseconds,
                                    Data = packet.Payload,
                                    MessageHeaderChar = messageHeaderChar,
                                };
                                return result;
                            }
                        }
                    }
                });
            }
            catch 
            {
                throw new OperationCanceledException();
            }
            finally
            {
                udpClient.Close();
            }
        }