public void SendMessage(TcpDestinationModel dst, byte[] msg) { Connection con = GetConnection(dst); int tries = RETRIES + 1; lock (con) { while (tries-- > 0) { CheckConnection(con); bool sent; try { TcpClient client = con.Client; client.GetStream().Write(msg, 0, msg.Length); sent = SeemsHaveWorked(client); } catch (IOException) { sent = false; } if (sent) { return; } } } throw new Exception("Message couldn't be sent!"); }
/// <remarks> /// When using the stream directly, one should access it asynchronously, cause no locking happens. /// For multi-threaded access MSDN suggests the following: /// "If you want to process your I/O using separate threads, consider using the BeginWrite and EndWrite methods..." /// [http://msdn.microsoft.com/en-us/library/System.Net.Sockets.NetworkStream%28v=vs.110%29.aspx] /// </remarks> public NetworkStream GetStream(TcpDestinationModel dst) { Connection con = GetConnection(dst); CheckConnection(con); return(con.Client.GetStream()); }
private Connection GetConnection(TcpDestinationModel dst) { Connection con; lock (syncLock) { if (dst.Id != null) { if (connections.ContainsKey(dst.Id)) { con = connections[dst.Id]; } else { throw new ArgumentException(String.Format("There is no connection with id '{0}' available!", dst.Id)); } } else { TcpDestination dest = new TcpDestination(dst); if (connections.ContainsKey(dest.Id)) { con = connections[dest.Id]; } else { con = AddEntry(dest); } } } return(con); }
public TcpDestination(TcpDestinationModel model) { if (model.Id != null) { Init(model.Id, model.Host, model.Port); } else { string id = GenerateId(model.Host, model.Port); Init(id, model.Host, model.Port); } }
public override string ToString() { TcpDestinationModel dst = Destination; if (dst.Id == null) { return(String.Format("{0} ( message = \"{1}\", host = {2}, port = {3} )", GetType().Name, Message, dst.Host, dst.Port)); } else { return(String.Format("{0} ( message = \"{1}\", destId = '{2}' )", GetType().Name, Message, dst.Id)); } }
/// <summary> /// New destinations override existing ones, if they have the same id. /// </summary> public void Add(TcpDestinationModel model) { TcpDestination dst = new TcpDestination(model); lock (syncLock) { if (connections.ContainsKey(dst.Id)) { TcpDestination old = connections[dst.Id].Destination; if (dst.Equals(old)) { logger.Warn("Ignored add operation, because identical {0} is already there.", typeof(TcpDestination)); } else { ReplaceConnection(dst); } } else { AddEntry(dst); } } }
public TcpActionParameter(TcpDestinationModel dst, string message) { Destination = dst; Message = message; }