public virtual async Task <dynamic> ReplyingAsync(LinderRequest linderRequest) { dynamic response = null; if (this.clients == null || this.clients.Count == 0) { throw new Exception("No agent has been found to reply request."); } else { int initialAgentIndex = this.clientIndex; string ipAddress = null; int port = 0; lock (this.clients) { ipAddress = this.clients[this.clientIndex].IPAddress; port = this.clients[this.clientIndex].Port; if (this.clientIndex == this.clients.Count - 1) { this.clientIndex = 0; } else { this.clientIndex++; } } Exception exception = null; try { using LinderConnection linderConnection = new(ipAddress, port); response = await linderConnection.QueryAsync(linderRequest); } catch (SocketException socketException) { exception = socketException; lock (this.clients) { this.clients.RemoveAt(initialAgentIndex); this.clientIndex--; if (this.clientIndex < 0) { this.clientIndex = 0; } else if (this.clientIndex >= this.clients.Count) { this.clientIndex = this.clients.Count - 1; } } } if (exception != null) { throw exception; } } return(response); }
public dynamic Query(LinderRequest linderRequest) { Task <dynamic> task = this.QueryAsync(linderRequest); task.Wait(); return(task.Result); }
public sealed override async Task <dynamic> Reply(LinderRequest linderRequest) { dynamic response; if (linderRequest.Name == "server") { response = new { IPAddress = this.ServerIPAddress, Port = this.ServerPort } } ; else { response = await this.ReplyingAsync(linderRequest); } return(response); }
public async Task <dynamic> QueryAsync(LinderRequest linderRequest) { dynamic result = null; CancellationTokenSource cancellationTokenSource = new(); cancellationTokenSource.CancelAfter(this.Timeout); Exception exception = null; try { this.tcpClient = new(this.IPAddress, this.Port); string json = JsonConvert.SerializeObject(linderRequest); byte[] bytes = Encoding.UTF8.GetBytes(json); string incomingJson = null; using (NetworkStream networkStream = tcpClient.GetStream()) { await networkStream.WriteAsync(bytes, 0, bytes.Length); bytes = new byte[this.packazeSize]; int i = 0; List <byte> data = new(); do { i = await networkStream.ReadAsync(bytes, 0, bytes.Length, cancellationTokenSource.Token); var array = bytes.Take(i); data.AddRange(array); } while (i != 0); incomingJson = Encoding.UTF8.GetString(data.ToArray(), 0, data.Count); } LinderResponse linderResponse = JsonConvert.DeserializeObject <LinderResponse>(incomingJson); if (linderResponse.Exception == null) { result = linderResponse.Data; } else { exception = linderResponse.Exception; } } catch (JsonReaderException jsonReaderException) { exception = new InvalidCastException($"An error occurred while deserializing response.", jsonReaderException); } catch (Exception ex) { exception = ex; } finally { if (this.tcpClient != null) { this.tcpClient.Close(); } } if (exception != null) { throw exception; } return(result); }
public abstract Task <dynamic> ReplyingAsync(LinderRequest linderRequest);
public sealed override async Task <dynamic> Reply(LinderRequest linderRequest) { dynamic response = null; if (linderRequest.Name == "reset agents") { this.clientIndex = 0; this.clients.Clear(); response = true; } else if (linderRequest.Name == "join") { string name; if (linderRequest.Parameters.ContainsKey("name")) { name = linderRequest.Parameters["name"]; } else { throw new ArgumentException("name parameter is missing."); } string ipAddress; if (linderRequest.Parameters.ContainsKey("ipAddress")) { ipAddress = linderRequest.Parameters["ipAddress"]; } else { throw new ArgumentException("ipAddress parameter is missing."); } int port; if (linderRequest.Parameters.ContainsKey("port")) { port = Convert.ToInt32(linderRequest.Parameters["port"]); } else { throw new ArgumentException("port parameter is missing."); } lock (this.clients) { if (this.clients.Any(p => p.IPAddress == ipAddress && p.Port == port)) { response = false; } else { this.clients.Add(new LinderClientInfo { Name = name, IPAddress = ipAddress, Port = port, JoinedDate = DateTime.UtcNow }); response = true; } } } else if (linderRequest.Name == "list agents") { response = this.clients; } else { response = await this.ReplyingAsync(linderRequest); } return(response); }