/// <summary> /// Will be called whenever a message has been received. No matter if request, response, event, or event reply ... /// /// Starts a new thread to handle the message /// </summary> /// <param name="msg"></param> public static void MessageReceived(ChannelMessage msg) { SingleMessageHandler mh = new SingleMessageHandler(msg); Thread t = new Thread(new ThreadStart(mh.HandleMessage)); t.IsBackground=true; t.Start(); }
public void HandleIncomingMessage(ChannelMessage msg) { ITransportHeaders responseHeaders; Stream responseStream; IMessage responseMsg; ServerChannelSinkStack stack = new ServerChannelSinkStack(); stack.Push(this,msg); ServerProcessing proc = _nextSink.ProcessMessage(stack,null,msg.Headers,msg.Body,out responseMsg, out responseHeaders,out responseStream); // check the return value. switch (proc) { // this message has been handled synchronously case ServerProcessing.Complete: // send a response message ChannelMessage reply = new ChannelMessage(msg.From,msg.ID,responseHeaders,responseStream); ConnectionManager.SendMessage(reply); break; // this message has been handled asynchronously case ServerProcessing.Async: // nothing needs to be done yet break; // it's been a one way message case ServerProcessing.OneWay: // nothing needs to be done yet break; } }
/* public void StartListening() { ConnectionManager.StartListening(_port); } */ public void AsyncProcessResponse( IServerResponseChannelSinkStack sinkStack, object state, IMessage msg, ITransportHeaders headers, System.IO.Stream stream) { ChannelMessage req = (ChannelMessage) state; ChannelMessage reply = new ChannelMessage(req.From,req.ID,headers,stream); ConnectionManager.SendMessage(reply); }
/// <summary> /// read the message incoming from a socket /// </summary> /// <param name="sock"></param> public static ChannelMessage ReadFromSocket(Socket sock, int port) { BinaryFormatter fmt = new BinaryFormatter(); ChannelMessage msg = new ChannelMessage(); // read the lead-in (16 bytes) byte[] buf = new Byte[16]; sock.Receive(buf,16,SocketFlags.None); MemoryStream lead = new MemoryStream(buf); BinaryReader leadrdr = new BinaryReader(lead); // read the number of header bytes int headerlen = (int) leadrdr.ReadInt64(); // read the number of body bytes int bodylen = (int) leadrdr.ReadInt64(); // get header & body byte[] headerbytes = new Byte[headerlen]; sock.Receive(headerbytes,0,headerlen,SocketFlags.None); byte[] bodybytes = new Byte[bodylen]; sock.Receive(bodybytes,0,bodylen,SocketFlags.None); // layer first memorystream MemoryStream head = new MemoryStream(headerbytes,0,headerlen,false); // layer body stream MemoryStream body = new MemoryStream(bodybytes,0,bodylen,false); msg.Headers = (ITransportHeaders) fmt.Deserialize(head); msg.ID = (Guid) msg.Headers["Id"]; msg.InReplyTo = (Guid) msg.Headers["InReplyTo"]; msg.From = (String) msg.Headers["From"]; // IPEndPoint ipe = (IPEndPoint) sock.LocalEndPoint; msg.To = (String) msg.Headers["To"]; msg.SecondaryGuessTo = Helper.GetIPAddress() + ":" + port; body.Seek(0,SeekOrigin.Begin); msg.Body = body; return msg; }
public void AsyncProcessRequest(IClientChannelSinkStack sinkStack, IMessage msg, ITransportHeaders headers, Stream stream) { String objectURI; // check the URL String URL = (String) msg.Properties["__Uri"]; String baseurl = Helper.ParseURL(URL,out objectURI); if (baseurl == null) { objectURI = URL; } headers["__RequestUri"] = objectURI; // transfer it ChannelMessage req; if (URL.ToLower().StartsWith(Helper.TCP_PREFIX)) { req = new ChannelMessage(_hostname.ToLower() + ":" + _port,headers,stream); } else { req = new ChannelMessage(_GUID,headers,stream); } AsyncResponseHandler hdl = new AsyncResponseHandler(sinkStack); MessageHandler.RegisterAsyncResponseHandler(req.ID,hdl); ConnectionManager.SendMessage(req); }
public void ProcessMessage(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, out ITransportHeaders responseHeaders, out Stream responseStream) { String objectURI; // check the URL String URL = (String) msg.Properties["__Uri"]; String baseurl = Helper.ParseURL(URL,out objectURI); if (baseurl == null) { objectURI = URL; if (_hostname == null) { URL = Helper.TCPGUID_PREFIX + _GUID + objectURI; } } requestHeaders["__RequestUri"] = objectURI; // transfer it ChannelMessage req; if (URL.ToLower().StartsWith(Helper.TCP_PREFIX)) { req = new ChannelMessage(_hostname.ToLower() + ":" + _port,requestHeaders,requestStream); } else { req = new ChannelMessage(_GUID,requestHeaders,requestStream); } AutoResetEvent evt = new AutoResetEvent(false); MessageHandler.RegisterAutoResetEvent(req.ID,evt); ConnectionManager.SendMessage(req); ChannelMessage resp = MessageHandler.WaitAndGetResponseMessage(req.ID, evt); responseHeaders = resp.Headers; responseStream = resp.Body; }
internal SingleMessageHandler(ChannelMessage incomingMessage) { msg = incomingMessage; }
internal void HandleAsyncResponseMsg(ChannelMessage msg) { _sinkStack.AsyncProcessResponse(msg.Headers,msg.Body); }
/// <summary> /// Sends a message. Looks for the necessary connection. If none exists, a new one will be built /// </summary> /// <param name="msg"></param> public static void SendMessage(ChannelMessage msg) { Connection conn = (Connection) ConnByReceiver[msg.To]; if (conn == null) { String host; int port; msg.SplitReceiver(out host, out port); if (host == null) { // message is directed at a GUID but no connection found // TODO: wait some seconds and retry. Maybe the client reconnects. Console.WriteLine("Message is directed at a GUID but no connection found"); throw new Exception("Message is directed at a GUID but no connection found"); } conn = new Connection(host,port); conn.Connect(); ConnByReceiver[msg.To] = conn; } conn.SendMessage(msg); }