public override bool Connect(bool keepalive = true)
        {
            try {
                fSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                fSocket.ReceiveBufferSize = 2 * fBuffer.Length;
                fSocket.Connect(new IPEndPoint(IPAddress.Loopback, 5000));

                //EndPoint remoteAddress = new IPEndPoint(IPAddress.Loopback, 0);
                //fSocket.BeginReceiveFrom(fBuffer, 0, fBuffer.Length, SocketFlags.None, ref remoteAddress, EndClientRecv, null);

                fStream = new IPCStream(new NetworkStream(fSocket, false));
            } catch (SocketException e) {
                System.Diagnostics.Debug.WriteLine(e);
                return(false);
            }

            if (keepalive)
            {
                fPingTimer = new Timer(
                    (object state) => { SendMessage(new IPCMessage(StatusMessage.Ping)); },
                    null, IPCSocketChannel.ReadTimeOut / 2, IPCSocketChannel.ReadTimeOut / 2);
            }

            return(true);
        }
 internal void RaiseDataReceive(IPEndPoint peer, IPCMessage message, IPCStream stream)
 {
     if (DataReceive != null)
     {
         DataReceive(this, new DataReceiveEventArgs(peer, message, stream));
     }
 }
        public override void Disconnect(bool sendCloseMessage = true)
        {
            if (sendCloseMessage)
            {
                fPingTimer.Dispose();
                fStream.WriteMessage(new IPCMessage(StatusMessage.CloseConnection));
            }

            if (fStream != null)
            {
                fStream.Dispose();
                fStream = null;
            }

            base.Disconnect();
        }
Beispiel #4
0
        public TCPServerConnection(IPCServerChannel channel, Socket clientSocket)
        {
            Id = Guid.NewGuid().ToString();

            fChannel = channel;
            fChannel.AddConnection(this);

            fSocket = clientSocket;
            var netStream = new NetworkStream(fSocket, false);

            netStream.ReadTimeout = IPCSocketChannel.ReadTimeOut;
            fStream = new IPCStream(netStream);

            // Start listening for incoming data.  (If you want a multi threaded service,
            // you can start this method up in a separate thread.)

            fSocket.BeginReceive(fBuffer, 0, fBuffer.Length, SocketFlags.None, OnBytesReceived, this);
        }
Beispiel #5
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);
            }
        }
Beispiel #6
0
 private void ReceiveClients(IPCStream stream)
 {
     while (ProcessMessage(stream))
     {
     }
 }
 public DataReceiveEventArgs(IPEndPoint endPoint, IPCMessage message, IPCStream stream)
 {
     fEndPoint = endPoint;
     fMessage  = message;
     fStream   = stream;
 }