public void ReadAsync(PipeStream stream, int timeout, Action <byte[]> onComplete) { var memoryStream = new MemoryStream(); var buffer = new byte[8 * 1024]; var read = 0; if (stream.CanTimeout) { stream.ReadTimeout = timeout; } Future.Of( x => stream.BeginRead(buffer, 0, buffer.Length, x, null), x => { read = stream.EndRead(x); if (read > 0) { memoryStream.Write(buffer, 0, read); } if (stream.IsMessageComplete && memoryStream.Length > 0) { onComplete(memoryStream.ToArray()); memoryStream.Close(); memoryStream.Dispose(); memoryStream = new MemoryStream(); } return(read); }) .LoopWhile(() => Running) .Start(); }
public void Connect(Action onConnect, Action onFailure) { Future.Of( x => Server.BeginWaitForConnection(x, null), x => { Server.EndWaitForConnection(x); return(true); }) .OnValue(x => { if (x) { Connected = true; Running = true; onConnect(); } else { Connect(onConnect, onFailure); } }) .OnFailure(() => { onFailure(); return(true); }) .Start() .LoopWhile(() => Running); }
public Future <TReply> ExpectReply <TReply, TMessage>(TMessage message, Action <IEnvelope> modifyEnvelope) { var envelope = new NamedPipeEnvelope( ) { CorrelationId = Definition.GetCorrelationId(message), RoutingKey = Definition.GetRoutingKey(message), Message = message, MessageType = typeof(TMessage) }; modifyEnvelope(envelope); var future = Future.Of <TReply>(() => Pipe.Send(envelope)); Dispatcher.ExpectResponse <TReply>(envelope.MessageId.ToString(), future); return(future); }
public Future <TReply> ExpectReply <TReply, TMessage>(TMessage message, Action <IEnvelope> modifyEnvelope) { var envelope = new RabbitEnvelope <TMessage>(message) { CorrelationId = Definition.GetCorrelationId(message), RoutingKey = Definition.GetRoutingKey(message), ReplyToExchange = RabbitBroker.ResponseId, ReplyToKey = ConfigureResponseChannel <TReply>() }; modifyEnvelope(envelope); envelope.ByteStream = Serializer.Serialize(envelope.Message); var future = Future.Of <TReply>(() => Proxy.Send(envelope)); MessageDispatcher.ExpectResponse <TReply>(envelope.MessageId.ToString(), future); return(future); }
public void Send(NamedPipeEnvelope envelope) { var envelopeBuffer = Serializer.Serialize(envelope); var header = Encoding.UTF8.GetBytes(envelope.MessageType.AssemblyQualifiedName); var buffer = BitConverter .GetBytes(header.Length) .Concat(header) .Concat(envelopeBuffer) .ToArray(); Future.Of( x => Pipe.Stream.BeginWrite(buffer, 0, buffer.Length, x, null), x => { Pipe.Stream.EndWrite(x); Pipe.Stream.Flush(); Pipe.Stream.WaitForPipeDrain(); return(true); } ).Start(); }
public Future <TReply> ExpectReply <TReply, TMessage>(TMessage message, Action <IEnvelope> modifyEnvelope) { Func <object, string> correlate; Func <object, string> route; Definition.CorrelationMethods.TryGetValue(typeof(TMessage), out correlate); Definition.RoutingMethods.TryGetValue(typeof(TMessage), out route); correlate = correlate ?? Empty; route = route ?? Empty; var envelope = new Envelope <TMessage>(message) { CorrelationId = correlate(message), RoutingKey = route(message), }; modifyEnvelope(envelope); var future = Future.Of <TReply>(() => MessageDispatcher.Send(envelope)); MessageDispatcher.ExpectResponse <TReply>(envelope.MessageId.ToString(), future); return(future); }