Beispiel #1
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();
            }
        }