public void ReceiveRequest() { // Seems like this method is invoked to send a reply // with related to "already created" SOAP fault. // // It is still not understandable that this delegate // is invoked as an infinite loop ... ReplyHandler handler = delegate(Message input) { Console.Error.WriteLine("Processing a reply."); // a:InvalidSecurity // An error occurred when verifying security for the message. Assert.IsTrue(input.IsFault); throw new ApplicationException(); }; Message msg = Message.CreateMessage(MessageVersion.Default, "myAction"); RequestReceiver receiver = delegate() { return(msg); }; IChannelListener <IReplyChannel> listener = CreateListener(handler, receiver); listener.Open(); IReplyChannel reply = listener.AcceptChannel(); reply.Open(); RequestContext ctx = reply.EndReceiveRequest(reply.BeginReceiveRequest(null, null)); }
private async void Run() { Console.WriteLine("Client Start"); var connection = new SignalRConnection(); IClientLogger myLogger = new ClientConsoleLogger(); var connectionOptions = new ConnectionOptions("http://127.0.0.1:15117/signalr", myLogger); await connection.Connect(connectionOptions); var requestReceiver = new RequestReceiver(connection); var requestExecutor = new RequestExecutor(connection); do { var response = await requestReceiver.ReadAsync <TestResponse>(new TestRequest { Test = "Hello Server!" }); requestExecutor.Execute(new MyRequest { Hessage = "Hello Server with responseless request" }); Console.WriteLine(response.Text); } while (true); Console.WriteLine("Client End"); }
public void FullRequest() { EndpointIdentity identity = new X509CertificateEndpointIdentity(new X509Certificate2("Test/Resources/test.pfx", "mono")); EndpointAddress address = new EndpointAddress(new Uri("stream:dummy"), identity); Message mreq = Message.CreateMessage(MessageVersion.Default, "myAction"); Message mreply = null; XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; // listener setup ReplyHandler replyHandler = delegate(Message rinput) { mreply = rinput; }; RequestReceiver receiver = delegate() { return(mreq); }; IChannelListener <IReplyChannel> listener = CreateListener(replyHandler, receiver); listener.Open(); IReplyChannel reply = listener.AcceptChannel(); reply.Open(); RequestSender reqHandler = delegate(Message input) { try { // sync version somehow causes an infinite loop (!?) RequestContext ctx = reply.EndReceiveRequest(reply.BeginReceiveRequest(TimeSpan.FromSeconds(5), null, null)); // RequestContext ctx = reply.ReceiveRequest (TimeSpan.FromSeconds (5)); Console.Error.WriteLine("Acquired RequestContext."); ctx.Reply(input); } catch (Exception ex) { Console.Error.WriteLine("ERROR during processing a request in FullRequest()"); Console.Error.WriteLine(ex); Console.Error.Flush(); throw; } return(mreply); }; CustomBinding b = CreateBinding(reqHandler); IRequestChannel ch = ChannelFactory <IRequestChannel> .CreateChannel(b, address); ch.Open(); Console.Error.WriteLine("**** starting a request ****"); IAsyncResult async = ch.BeginRequest(mreq, null, null); Console.Error.WriteLine("**** request started. ****"); Message res = ch.EndRequest(async); }
CustomBinding CreateBinding(ReplyHandler replier, RequestReceiver receiver) { SymmetricSecurityBindingElement sbe = new SymmetricSecurityBindingElement(); sbe.ProtectionTokenParameters = new X509SecurityTokenParameters(); CustomBinding b = new CustomBinding( sbe, new TextMessageEncodingBindingElement(), new HandlerTransportBindingElement(replier, receiver)); return(b); }
IChannelListener <IReplyChannel> CreateListener(ReplyHandler handler, RequestReceiver receiver) { CustomBinding rb = CreateBinding(handler, receiver); BindingParameterCollection bpl = new BindingParameterCollection(); ServiceCredentials cred = new ServiceCredentials(); cred.ServiceCertificate.Certificate = new X509Certificate2("Test/Resources/test.pfx", "mono"); IServiceBehavior sb = cred; sb.AddBindingParameters(null, null, null, bpl); IChannelListener <IReplyChannel> listener = rb.BuildChannelListener <IReplyChannel> (bpl); return(listener); }
protected override void InitRoutine() { int port = GetConfigInteger("Port"); string uri = GetConfigString("Path"); bool auth = GetConfigBoolean("Authentication"); _rcv = new RequestReceiver(port); _rcv.RequestUri = uri; if (auth) { _rcv.EnableAuthentication(GetConfigString("Credentials", "User"), GetConfigString("Credentials", "Password")); } _rcv.ProcessRequestString = ProcessRequestString; }
public void CustomTransportDoesNotRequireMessageEncoding() { ReplyHandler replier = delegate(Message msg) { resmsg = msg; }; RequestReceiver receiver = delegate() { return(reqmsg); }; RequestSender sender = delegate(Message msg) { reqmsg = msg; CustomBinding br = new CustomBinding( new HandlerTransportBindingElement(replier, receiver)); IChannelListener <IReplyChannel> l = br.BuildChannelListener <IReplyChannel> ( new BindingParameterCollection()); l.Open(); IReplyChannel rch = l.AcceptChannel(); rch.Open(); Message res = Message.CreateMessage(MessageVersion.Default, "urn:succeeded"); rch.ReceiveRequest().Reply(res); rch.Close(); l.Close(); return(resmsg); }; CustomBinding bs = new CustomBinding( new HandlerTransportBindingElement(sender)); IChannelFactory <IRequestChannel> f = bs.BuildChannelFactory <IRequestChannel> ( new BindingParameterCollection()); f.Open(); IRequestChannel ch = f.CreateChannel(new EndpointAddress("urn:dummy")); ch.Open(); Message result = ch.Request(Message.CreateMessage(MessageVersion.Default, "urn:request")); }
void HelloStep( RequestReceiver requestReceiver, IMessageConnection c, ConcurrentStack <IDisposable> disposables, IObservable <IMessage> messagesIn, IObserver <IMessage> messagesOut) { messagesOut.OnNext( Event.Create(new Welcome() { Message = "You have successfully connected to the Fuse Daemon. You can find API docs on our webpage." })); var taskResult = new TaskCompletionSource <Unit>(); using (requestReceiver.SubscribeToRequest <HelloRequest, HelloResponse>( helloRequest => { try { HandleHello(helloRequest, c, disposables, requestReceiver, messagesIn, messagesOut); taskResult.SetResult(Unit.Default); return(new HelloResponse()); } catch (Exception e) { taskResult.SetException(e); throw; } })) { c.StartRead(); taskResult.Task.Wait(); } }
/// <exception cref="SocketException"></exception> public void Run() { using (var ensureSingleInstance = EnsureSingleInstanceFactory.Create("Fuse.Daemon", _report)) { HandleMultipleUsers(); if (ensureSingleInstance.IsAlreadyRunning()) { var alreadyRunning = "Already running at " + RunningAt; _report.Info(alreadyRunning, ReportTo.LogAndUser); return; } try { var serializer = new Serializer(); var clientConnected = _localServer.ClientConnected; var clientDisconnected = _localServer.ClientDisconnected; if (_isDebug) { clientConnected.Subscribe( c => _report.Info("A client connected at: " + DateTime.Now.ToShortTimeString(), ReportTo.LogAndUser)); clientDisconnected.Subscribe( c => _report.Info("A client disconnected at: " + DateTime.Now.ToShortTimeString(), ReportTo.LogAndUser)); } clientConnected.Subscribe( async c => { var disposables = new ConcurrentStack <IDisposable>(); c.Disconnected.Subscribe(t => Dispose(disposables)); // Dispose client stuff on OnCompleted var messagesOut = c.OutgoingMessages.Serialized(serializer); var messagesIn = c.IncomingMessages.TryDeserialize(serializer, _report); var requestReceiver = new RequestReceiver( _report, messagesIn.OfType <IRequestMessage <UnresolvedMessagePayload> >(), messagesOut); // Handle kill request disposables.Push( requestReceiver.SubscribeToRequest <KillRequest, KillResponse>(HandleKillRequest)); try { await Task.Run(() => HelloStep(requestReceiver, c, disposables, messagesIn, messagesOut)); } catch (Exception e) { _report.Error(e); c.Close(); } }); clientDisconnected.Subscribe(_pluginClients.Remove); _localServer.Host(12122); var runningAt = "Running at " + RunningAt; _report.Info(runningAt, ReportTo.LogAndUser); if (_runServices) { Task.Run(() => _serviceRunnerFactory.Start()) .ContinueWith(t => _serviceRunner = t.Result); } //Task.Run(() => Proxy.Initialize()); Console.CancelKeyPress += (sender, args) => { DoPreTermination(); ScheduleTerminate(); args.Cancel = true; }; using (Observable.Interval(TimeSpan.FromMinutes(1)).Subscribe(duration => _report.Info("Daemon is still running"))) using (_replayBroadcastEvents.Connect()) using (_localServer) //using (simulatorProxyServer) { while (_isRunning) { Thread.Sleep(1000); } } } finally { DoPreTermination(); } } }
void HandleHello( HelloRequest helloReq, IMessageConnection c, ConcurrentStack <IDisposable> disposables, RequestReceiver requestReceiver, IObservable <IMessage> messagesIn, IObserver <IMessage> messagesOut) { if (helloReq == null) { throw new FuseRequestErrorException(ErrorCode.InvalidData, "Expected data to not be empty."); } // TODO: Enforce daemonkey to be present in the future if (!string.IsNullOrWhiteSpace(helloReq.DaemonKey) && DaemonKey.Deserialize(helloReq.DaemonKey) != _daemonKey) { throw new FuseRequestErrorException( ErrorCode.WrongDaemonKey, "Daemon key was not right, maybe because daemon possesses wrong local user."); } #pragma warning disable 0618 var pluginClient = new PluginClient( messagesOut, helloReq.Identifier, helloReq.Implements, helloReq.EventFilter); #pragma warning restore 0618 _pluginClients.Add(c, pluginClient); _report.Info("Client connected: " + helloReq.Identifier, ReportTo.LogAndUser); c.Disconnected.Subscribe(d => _report.Info("Client disconnected: " + helloReq.Identifier, ReportTo.LogAndUser)); // Broadcast events to clients that wants the events. disposables.Push(messagesIn.OfType <IEventMessage <IEventData> >().Subscribe(_broadcastEvents.OnNext)); disposables.Push(_broadcastEvents .Subscribe(pluginClient.HandleEvent)); // Handle Subscribe to event request. disposables.Push( messagesIn .OfType <IRequestMessage <UnresolvedMessagePayload> >() .Deserialize <SubscribeRequest>(_report) .Subscribe( r => disposables.Push( pluginClient.SubscribeToEvent( r, hotMessages: _broadcastEvents, replayMessages: _replayBroadcastEvents)) )); disposables.Push( requestReceiver.SubscribeToRequest <PublishServiceRequest, PublishServiceResponse>(pluginClient.AppendSupportedRequests)); // Send requests to clients that implements the request. disposables.Push( messagesIn.OfType <IRequestMessage <IRequestData> >() .Where(r => r.Name != "Subscribe" && r.Name != "Fuse.KillDaemon" && r.Name != "PublishService") .Subscribe(r => Task.Run(() => _pluginClients.PassRequestToAnImplementor(pluginClient, r)))); // Handle the response from the one that implemented the request disposables.Push( messagesIn.OfType <IResponseMessage <IResponseData> >().Subscribe(pluginClient.HandleResponse)); }
IChannelListener<IReplyChannel> CreateListener (ReplyHandler handler, RequestReceiver receiver) { CustomBinding rb = CreateBinding (handler, receiver); BindingParameterCollection bpl = new BindingParameterCollection (); ServiceCredentials cred = new ServiceCredentials (); cred.ServiceCertificate.Certificate = new X509Certificate2 ("Test/Resources/test.pfx", "mono"); IServiceBehavior sb = cred; sb.AddBindingParameters (null, null, null, bpl); IChannelListener<IReplyChannel> listener = rb.BuildChannelListener<IReplyChannel> (bpl); return listener; }
CustomBinding CreateBinding (ReplyHandler replier, RequestReceiver receiver) { SymmetricSecurityBindingElement sbe = new SymmetricSecurityBindingElement (); sbe.ProtectionTokenParameters = new X509SecurityTokenParameters (); CustomBinding b = new CustomBinding ( sbe, new TextMessageEncodingBindingElement (), new HandlerTransportBindingElement (replier, receiver)); return b; }
public HandlerTransportBindingElement(ReplyHandler handler, RequestReceiver receiver) { this.reply_handler = handler; this.receiver = receiver; }
public void registerRequestReceiver(RequestReceiver receiver) { requestReceivers[receiver.Key] = receiver; }