Esempio n. 1
0
        /// <summary>
        /// Checks if server at the given location is alive
        /// </summary>
        /// <param name="serverUrl">Server URL</param>
        /// <returns>True if the server is alive; false otherwise.</returns>
        public bool TryConnection(string serverUrl, bool tryRemove)
        {
            //Console.WriteLine("Trying connection " + serverUrl + ": " + tryRemove);
            TSMan.CheckFreeze();

            TSMan.CheckDelay();


            if (serverUrl.Equals(TSMan.URL))
            {
                //Console.WriteLine("Return false #2");

                return(true);
            }

            // Get the reference for the tuple space server
            ITSpaceServer server = (ITSpaceServer)Activator.GetObject(typeof(ITSpaceServer), serverUrl);


            // Ping server
            if (server == null)
            {
                return(false);
            }

            PingDelegate del         = new PingDelegate(server.Ping);
            IAsyncResult asyncResult = del.BeginInvoke(TSMan.URL, null, null);

            asyncResult.AsyncWaitHandle.WaitOne(5000, false);

            if (asyncResult.IsCompleted)
            {
                try
                {
                    del.EndInvoke(asyncResult);
                    //Console.WriteLine("Return true");

                    return(true);
                }
                catch (Exception)
                {
                    if (tryRemove)
                    {
                        TryRemoveFromView(serverUrl);
                    }
                    //Console.WriteLine("Return false #2");

                    return(false);
                }
            }
            if (tryRemove)
            {
                TryRemoveFromView(serverUrl);
            }

            //Console.WriteLine("Return false #3");


            return(false);
        }
Esempio n. 2
0
        private void DoPing(string remotingEndpointUrl)
        {
            var remotingEndpoint = GetRemoteEndpoint(remotingEndpointUrl);

            PingDelegate pingDelegate = remotingEndpoint.Ping;
            var          asyncResult  = pingDelegate.BeginInvoke(null, null);

            pingDelegate.EndInvoke(asyncResult);
        }
Esempio n. 3
0
 private void pingCallback(IAsyncResult ar)
 {
     try
     {
         PingDelegate aPingDelegate = (PingDelegate)ar.AsyncState;
         aPingDelegate.EndInvoke(ar);
     }
     catch
     { }
 }
Esempio n. 4
0
        public string Ping(string message)
        {
            string retVal = message;
            // TODO : TesterState

            PingDelegate aPingDelegate = new PingDelegate(ping);

            aPingDelegate.BeginInvoke(message, pingCallback, aPingDelegate);
            // TODO
            return(retVal);
        }
        /// <summary>
        /// Processes a websocket frame and triggers consumer events
        /// </summary>
        /// <param name="psocketState">We need to modify the websocket state here depending on the frame</param>
        private void ProcessFrame(WebSocketState psocketState)
        {
            if (psocketState.Header.IsMasked)
            {
                byte[] unmask = psocketState.ReceivedBytes.ToArray();
                WebSocketReader.Mask(psocketState.Header.Mask, unmask);
                psocketState.ReceivedBytes = new List <byte>(unmask);
            }

            switch (psocketState.Header.Opcode)
            {
            case WebSocketReader.OpCode.Ping:
                PingDelegate pingD = OnPing;
                if (pingD != null)
                {
                    pingD(this, new PingEventArgs());
                }

                WebSocketFrame pongFrame = new WebSocketFrame()
                {
                    Header = WebsocketFrameHeader.HeaderDefault(), WebSocketPayload = new byte[0]
                };
                pongFrame.Header.Opcode = WebSocketReader.OpCode.Pong;
                pongFrame.Header.IsEnd  = true;
                SendSocket(pongFrame.ToBytes());
                break;

            case WebSocketReader.OpCode.Pong:

                PongDelegate pongD = OnPong;
                if (pongD != null)
                {
                    pongD(this, new PongEventArgs()
                    {
                        PingResponseMS = Util.EnvironmentTickCountSubtract(Util.EnvironmentTickCount(), _pingtime)
                    });
                }
                break;

            case WebSocketReader.OpCode.Binary:
                if (!psocketState.Header.IsEnd)     // Not done, so we need to store this and wait for the end frame.
                {
                    psocketState.ContinuationFrame = new WebSocketFrame
                    {
                        Header           = psocketState.Header,
                        WebSocketPayload =
                            psocketState.ReceivedBytes.ToArray()
                    };
                }
                else
                {
                    // Send Done Event!
                    DataDelegate dataD = OnData;
                    if (dataD != null)
                    {
                        dataD(this, new WebsocketDataEventArgs()
                        {
                            Data = psocketState.ReceivedBytes.ToArray()
                        });
                    }
                }
                break;

            case WebSocketReader.OpCode.Text:
                if (!psocketState.Header.IsEnd)     // Not done, so we need to store this and wait for the end frame.
                {
                    psocketState.ContinuationFrame = new WebSocketFrame
                    {
                        Header           = psocketState.Header,
                        WebSocketPayload =
                            psocketState.ReceivedBytes.ToArray()
                    };
                }
                else
                {
                    TextDelegate textD = OnText;
                    if (textD != null)
                    {
                        textD(this, new WebsocketTextEventArgs()
                        {
                            Data = Encoding.UTF8.GetString(psocketState.ReceivedBytes.ToArray())
                        });
                    }

                    // Send Done Event!
                }
                break;

            case WebSocketReader.OpCode.Continue:      // Continuation.  Multiple frames worth of data for one message.   Only valid when not using Control Opcodes
                //Console.WriteLine("currhead " + psocketState.Header.IsEnd);
                //Console.WriteLine("Continuation! " + psocketState.ContinuationFrame.Header.IsEnd);
                byte[] combineddata = new byte[psocketState.ReceivedBytes.Count + psocketState.ContinuationFrame.WebSocketPayload.Length];
                byte[] newdata      = psocketState.ReceivedBytes.ToArray();
                Buffer.BlockCopy(psocketState.ContinuationFrame.WebSocketPayload, 0, combineddata, 0, psocketState.ContinuationFrame.WebSocketPayload.Length);
                Buffer.BlockCopy(newdata, 0, combineddata,
                                 psocketState.ContinuationFrame.WebSocketPayload.Length, newdata.Length);
                psocketState.ContinuationFrame.WebSocketPayload = combineddata;
                psocketState.Header.PayloadLen = (ulong)combineddata.Length;
                if (psocketState.Header.IsEnd)
                {
                    if (psocketState.ContinuationFrame.Header.Opcode == WebSocketReader.OpCode.Text)
                    {
                        // Send Done event
                        TextDelegate textD = OnText;
                        if (textD != null)
                        {
                            textD(this, new WebsocketTextEventArgs()
                            {
                                Data = Encoding.UTF8.GetString(combineddata)
                            });
                        }
                    }
                    else if (psocketState.ContinuationFrame.Header.Opcode == WebSocketReader.OpCode.Binary)
                    {
                        // Send Done event
                        DataDelegate dataD = OnData;
                        if (dataD != null)
                        {
                            dataD(this, new WebsocketDataEventArgs()
                            {
                                Data = combineddata
                            });
                        }
                    }
                    else
                    {
                        // protocol violation
                    }
                    psocketState.ContinuationFrame = null;
                }
                break;

            case WebSocketReader.OpCode.Close:
                Close(string.Empty);

                break;
            }
            psocketState.Header.SetDefault();
            psocketState.ReceivedBytes.Clear();
            psocketState.ExpectedBytes = 0;
        }