Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
0
        protected void OnBytesReceived(IAsyncResult result)
        {
            try {
                int count = fSocket.EndReceive(result);
                if (count > 0)
                {
                    //byte[] data = new byte[count];
                    //Array.Copy(fBuffer, 0, data, 0, count);
                    //var rv = fStream.ReadMessage();

                    byte[] data = new byte[count - 2];
                    Array.Copy(fBuffer, 2, data, 0, count - 2);
                    var rv = fStream.ReadMessage(data);

                    fChannel.RaiseDataReceive((IPEndPoint)fSocket.RemoteEndPoint, rv, fStream);
                }

                fSocket.BeginReceive(fBuffer, 0, fBuffer.Length, SocketFlags.None, OnBytesReceived, this);
            } catch (ObjectDisposedException ex) {
                //fDuplexClient.fLogger.WriteError("TCPConnection.OnBytesReceived(): ", ex);
            } catch (SocketException ex) {
                //fDuplexClient.fLogger.WriteError("TCPConnection.OnBytesReceived(): ", ex);
            }
        }
Ejemplo n.º 3
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);
            }
        }