예제 #1
0
        public Frame ReadData()
        {
            Frame frame = new Frame(new byte[] { });

            IAsyncResult result = Connection.Stream.BeginRead(Connection.InputData, 0, Connection.InputData.Length, null, Connection);

            if (result != null)
            {
                int bytesRead = Connection.Stream.EndRead(result);
                if (bytesRead > 0)
                {
                    frame = new Frame(WebSocketClientUtility.SubArray(Connection.InputData, 0, bytesRead));

                    ProcessReceivedData(frame);

                    TestUtility.LogInformation("Client {0:D3}: Read Type {1} : {2} ", Connection.Id, frame.FrameType, frame.Content.Length);
                }
            }

            return(frame);
        }
예제 #2
0
        public void ReadDataCallback(IAsyncResult result)
        {
            WebSocketConnect client = (WebSocketConnect)result.AsyncState;

            if (client.IsDisposed)
            {
                return;
            }

            int       bytesRead        = client.Stream.EndRead(result); // wait until the buffer is filled
            int       bytesReadIntotal = bytesRead;
            ArrayList InputDataArray   = new ArrayList();

            byte[] tempBuffer = null;

            if (bytesRead > 0)
            {
                tempBuffer = WebSocketClientUtility.SubArray(Connection.InputData, 0, bytesRead);

                Frame temp = new Frame(tempBuffer);

                // start looping if there is still remaining data
                if (tempBuffer.Length < temp.DataLength)
                {
                    if (client.TcpClient.GetStream().DataAvailable)
                    {
                        // add the first buffer to the arrayList
                        InputDataArray.Add(tempBuffer);

                        // start looping appending to the arrayList
                        while (client.TcpClient.GetStream().DataAvailable)
                        {
                            bytesRead  = client.TcpClient.GetStream().Read(Connection.InputData, 0, Connection.InputData.Length);
                            tempBuffer = WebSocketClientUtility.SubArray(Connection.InputData, 0, bytesRead);
                            InputDataArray.Add(tempBuffer);
                            bytesReadIntotal += bytesRead;
                            TestUtility.LogInformation("ReadDataCallback: Looping: Client {0:D3}: bytesReadHere {1} ", Connection.Id, bytesRead);
                        }

                        // create a single byte array with the arrayList
                        tempBuffer = new byte[bytesReadIntotal];
                        int arrayIndex = 0;
                        foreach (byte[] item in InputDataArray.ToArray())
                        {
                            for (int i = 0; i < item.Length; i++)
                            {
                                tempBuffer[arrayIndex] = item[i];
                                arrayIndex++;
                            }
                        }
                    }
                }

                // Create frame with the tempBuffer
                Frame frame          = new Frame(tempBuffer);
                int   nextFrameIndex = 0;
                while (nextFrameIndex != -1)
                {
                    TestUtility.LogInformation("ReadDataCallback: Client {0:D3}: Read Type {1} : {2} ", Connection.Id, frame.FrameType, bytesReadIntotal);
                    ProcessReceivedData(frame);

                    // Send Pong if the frame was Ping
                    if (frame.FrameType == FrameType.Ping)
                    {
                        SendPong(frame);
                    }

                    nextFrameIndex = frame.IndexOfNextFrame;
                    if (nextFrameIndex != -1)
                    {
                        tempBuffer = tempBuffer.SubArray(frame.IndexOfNextFrame, tempBuffer.Length - frame.IndexOfNextFrame);
                        frame      = new Frame(tempBuffer);
                    }
                }

                // Send Pong if the frame was Ping
                if (frame.FrameType == FrameType.Ping)
                {
                    SendPong(frame);
                }

                if (client.IsDisposed)
                {
                    return;
                }

                // Start the Async Read to handle the next frame comming from server
                client.Stream.BeginRead(client.InputData, 0, client.InputData.Length, ReadDataCallback, client);
            }
            else
            {
                client.Dispose();
            }
        }
예제 #3
0
        public void ReadDataCallback(IAsyncResult result)
        {
            WebSocketConnect client = (WebSocketConnect)result.AsyncState;

            try
            {
                if (!client.TcpClient.Connected)
                {
                    TestUtility.LogInformation("Failed to ReadDataCallback() because connection is gone");
                    return;
                }

                if (client.IsDisposed)
                {
                    return;
                }

                // wait until the buffer is filled
                int bytesRead = client.Stream.EndRead(result);

                int       bytesReadIntotal = bytesRead;
                ArrayList InputDataArray   = new ArrayList();
                byte[]    tempBuffer       = null;

                if (bytesRead > 0)
                {
                    tempBuffer = WebSocketClientUtility.SubArray(Connection.InputData, 0, bytesRead);

                    Frame temp = new Frame(tempBuffer);

                    // start looping if there is still remaining data
                    if (tempBuffer.Length < temp.DataLength)
                    {
                        if (client.TcpClient.GetStream().DataAvailable)
                        {
                            // add the first buffer to the arrayList
                            InputDataArray.Add(tempBuffer);

                            // start looping appending to the arrayList
                            while (client.TcpClient.GetStream().DataAvailable)
                            {
                                bytesRead  = client.TcpClient.GetStream().Read(Connection.InputData, 0, Connection.InputData.Length);
                                tempBuffer = WebSocketClientUtility.SubArray(Connection.InputData, 0, bytesRead);
                                InputDataArray.Add(tempBuffer);
                                bytesReadIntotal += bytesRead;
                                TestUtility.LogInformation("ReadDataCallback: Looping: Client {0:D3}: bytesReadHere {1} ", Connection.Id, bytesRead);
                            }

                            // create a single byte array with the arrayList
                            tempBuffer = new byte[bytesReadIntotal];
                            int arrayIndex = 0;
                            foreach (byte[] item in InputDataArray.ToArray())
                            {
                                for (int i = 0; i < item.Length; i++)
                                {
                                    tempBuffer[arrayIndex] = item[i];
                                    arrayIndex++;
                                }
                            }
                        }
                    }

                    // Create frame with the tempBuffer
                    Frame frame = new Frame(tempBuffer);
                    ProcessReceivedData(frame);
                    int nextFrameIndex = frame.IndexOfNextFrame;

                    while (nextFrameIndex != -1)
                    {
                        tempBuffer = tempBuffer.SubArray(frame.IndexOfNextFrame, tempBuffer.Length - frame.IndexOfNextFrame);
                        frame      = new Frame(tempBuffer);
                        ProcessReceivedData(frame);
                        nextFrameIndex = frame.IndexOfNextFrame;
                    }

                    if (this.WebSocketState == WebSocketState.ConnectionClosed)
                    {
                        client.Dispose();
                    }

                    if (client.IsDisposed)
                    {
                        return;
                    }

                    if (client.TcpClient.IsDead || client.TcpClient.Connected == false)
                    {
                        throw new Exception("Connection closed unexpectedly");
                    }

                    // Start the Async Read to handle the next frame comming from server
                    client.Stream.BeginRead(client.InputData, 0, client.InputData.Length, ReadDataCallback, client);
                }
                else
                {
                    client.Dispose();
                }
            }
            catch (Exception ex)
            {
                if (this.WebSocketState != WebSocketState.ConnectionClosed)
                {
                    TestUtility.LogInformation("ReadDataCallback: Error on EndRead()" + ex.Message);
                    this.WebSocketState = WebSocketState.ConnectionClosed;
                }
                else
                {
                    TestUtility.LogInformation("ReadDataCallback() failed: WebSocketState is in closed state.");
                }
                client.Dispose();
            }
        }