/// <summary> /// Initializes a new instance of the <see cref="CoCoL.Network.LatencyHidingReader<T>"/> class. /// </summary> /// <param name="parent">The channel where requests are made.</param> /// <param name="buffersize">The size of the buffer.</param> public LatencyHidingReader(IReadChannelEnd <T> parent, int buffersize) { if (buffersize <= 0) { throw new ArgumentOutOfRangeException("buffersize"); } m_parent = parent; m_buffersize = buffersize; }
/// <summary> /// Initializes a new instance of the <see cref="CoCoL.Network.NetworkClientConnector"/> class. /// </summary> /// <param name="hostname">The hostname for the nameserver.</param> /// <param name="port">The port for the nameserver.</param> public NetworkClientConnector(string hostname, int port) { using (new IsolatedChannelScope()) { m_nameserverclient = new NameServerClient(hostname, port); var chan = ChannelManager.CreateChannel <PendingNetworkRequest>(); m_requests = chan.AsReadOnly(); Requests = chan.AsWriteOnly(); } }
private static Task WriteChannel(StreamWriter stream, IReadChannelEnd <string> ch) { stream.AutoFlush = true; return(AutomationExtensions.RunTask( new { Input = ch }, async self => { using (stream) { while (true) { var line = await self.Input.ReadAsync(); await stream.WriteLineAsync(line).ConfigureAwait(false); //Console.WriteLine("Wrote {0}", line); } } } )); }
private static Task WriteChannel(StreamWriter stream, IReadChannelEnd<string> ch) { stream.AutoFlush = true; return AutomationExtensions.RunTask( new { Input = ch }, async self => { using(stream) { while(true) { var line = await self.Input.ReadAsync(); await stream.WriteLineAsync(line); //Console.WriteLine("Wrote {0}", line); } } } ); }
/// <summary> /// The process that serializes data from a <see cref="CoCoL.Network.PendingNetworkRequest" /> and sends it into the channel. /// </summary> /// <returns>The awaitable task.</returns> /// <param name="client">The <see cref="System.Net.Sockets.TcpClient"/> to read data from.</param> /// <param name="stream">The stream to write data to.</param> /// <param name="channel">The channel to read requests from.</param> /// <param name="selfid">A string used to identify this process in logs</param> private static async Task WriterProcess(TcpClient client, Stream stream, IReadChannelEnd <PendingNetworkRequest> channel, string selfid) { try { var headbuffer = new byte[SMALL_MESSAGE_SIZE]; var json = new Newtonsoft.Json.JsonSerializer(); using (client) using (stream) using (channel) { while (true) { var prnq = await channel.ReadAsync(); var header = new RequestHeader() { ChannelID = prnq.ChannelID, ChannelDataType = prnq.ChannelDataType.AssemblyQualifiedName, RequestID = prnq.RequestID, SourceID = prnq.SourceID, RequestType = prnq.RequestType, Timeout = prnq.Timeout, PayloadClassName = prnq.Value == null ? null : prnq.Value.GetType().AssemblyQualifiedName, NoOffer = prnq.Offer == null }; ushort headlen; using (var ms = new MemoryStream(headbuffer, true)) { // Make space for the size fields ms.Write(headbuffer, 0, 8 + 2); using (var tw = new StreamWriter(ms)) using (var jw = new Newtonsoft.Json.JsonTextWriter(tw)) { json.Serialize(jw, header); jw.Flush(); await tw.FlushAsync(); headlen = (ushort)(ms.Position - 8 - 2); } } // We write it all into the array before writing to the stream if (headlen > SMALL_MESSAGE_SIZE - 8 - 2 - 8) { throw new Exception("Too larger header"); } // Make a memory stream for the payload using (var ms = new MemoryStream()) using (var tw = new StreamWriter(ms)) using (var jw = new Newtonsoft.Json.JsonTextWriter(tw)) { ulong payloadlen = 0; if (prnq.Value != null) { json.Serialize(jw, prnq.Value); jw.Flush(); await tw.FlushAsync(); payloadlen = (ulong)ms.Length; ms.Position = 0; } if (payloadlen > MAX_MESSAGE_SIZE) { throw new Exception("Too large message payload"); } ulong packlen = 8uL + 2uL + headlen + 8uL + payloadlen; Array.Copy(BitConverter.GetBytes(packlen), headbuffer, 8); Array.Copy(BitConverter.GetBytes(headlen), 0, headbuffer, 8, 2); Array.Copy(BitConverter.GetBytes(payloadlen), 0, headbuffer, 8 + 2 + headlen, 8); LOG.DebugFormat("{2}: Sending {0} - {1} request", prnq.RequestID, prnq.RequestType, selfid); await stream.WriteAsync(headbuffer, 0, headlen + 8 + 2 + 8); if (payloadlen != 0) { await ms.CopyToAsync(stream); } LOG.DebugFormat("{4}: Sent {0} - {1} request with {2} bytes to {3}", prnq.RequestID, prnq.RequestType, packlen, client.Client.RemoteEndPoint, selfid); await stream.FlushAsync(); } } } } catch (Exception ex) { if (!ex.IsRetiredException()) { LOG.Error("Crashed network client writer side", ex); throw; } else { LOG.Info("Stopped network client writer"); } } }