예제 #1
0
 internal void RaiseDataReceive(IPEndPoint peer, IPCMessage message, IPCStream stream)
 {
     if (DataReceive != null)
     {
         DataReceive(this, new DataReceiveEventArgs(peer, message, stream));
     }
 }
예제 #2
0
        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);
            }
        }
예제 #3
0
        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();
            }
        }
예제 #4
0
        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);
            }
        }
예제 #5
0
 public DataReceiveEventArgs(IPEndPoint endPoint, IPCMessage message, IPCStream stream)
 {
     fEndPoint = endPoint;
     fMessage  = message;
     fStream   = stream;
 }