internal void RaiseDataReceive(IPEndPoint peer, IPCMessage message, IPCStream stream) { if (DataReceive != null) { DataReceive(this, new DataReceiveEventArgs(peer, message, stream)); } }
protected object SendMessage(IPCMessage message) { bool closeStream = false; if (fStream == null) { if (!Connect(false)) { throw new TimeoutException("Unable to connect"); } closeStream = true; } else if (message.StatusMsg == StatusMessage.None) { message.StatusMsg = StatusMessage.KeepAlive; } IPCMessage rv = null; lock (fStream) { fStream.WriteMessage(message); if (message.StatusMsg == StatusMessage.Ping) { return(null); } rv = fStream.ReadMessage(); } if (closeStream) { Disconnect(false); } if (rv != null) { if (rv.Error != null) { //throw new InvalidOperationException(rv.Error); rv.Return = rv.Error; } return(rv.Return); } else { return(null); } }
public void WriteMessage(IPCMessage message) { using (var memStream = new MemoryStream()) { BinFormatter.Serialize(memStream, message); byte[] msgBuffer = memStream.GetBuffer(); //byte[] msgBuffer = message.WriteObject(); int length = msgBuffer.Length; if (length > UInt16.MaxValue) { throw new InvalidOperationException("Message is too long"); } byte[] buffer = new byte[length + 2]; buffer[0] = (byte)(length / 256); buffer[1] = (byte)(length % 256); Buffer.BlockCopy(msgBuffer, 0, buffer, 2, length); fBaseStream.Write(buffer, 0, length + 2); fBaseStream.Flush(); } }
private bool ProcessMessage(IPCStream stream) { IPCMessage msg = stream.ReadMessage(); // this was a close-connection notification if (msg.StatusMsg == StatusMessage.CloseConnection) { return(false); } else if (msg.StatusMsg == StatusMessage.Ping) { return(true); } bool processedOk = false; string error = ""; object rv = null; // find the service object instance; if (fServices.TryGetValue(msg.Service, out instance) && instance != null) { // get the method System.Reflection.MethodInfo method = instance.GetType().GetMethod(msg.Method); // double check method existence against type-list for security // typelist will contain interfaces instead of instances if (fTypes[msg.Service].GetMethod(msg.Method) != null && method != null) { try { // invoke method rv = method.Invoke(instance, msg.Parameters); processedOk = true; } catch (Exception e) { error = e.ToString(); } } else { error = "Could not find method"; } } else { error = "Could not find service"; } // return either return value or error message IPCMessage returnMsg; if (processedOk) { returnMsg = new IPCMessage() { Return = rv } } ; else { returnMsg = new IPCMessage() { Error = error } }; stream.WriteMessage(returnMsg); // if there's more to come, keep reading a next message if (msg.StatusMsg == StatusMessage.KeepAlive) { return(true); } else // otherwise close the connection { return(false); } }
public DataReceiveEventArgs(IPEndPoint endPoint, IPCMessage message, IPCStream stream) { fEndPoint = endPoint; fMessage = message; fStream = stream; }