public Frame Connect(Uri address, bool storeData, bool isAlwaysReading) { Address = address; StoreData = storeData; Connection = new WebSocketConnect(); if (isAlwaysReading) { InitiateWithAlwaysReading(); } SendWebSocketRequest(WebSocketClientUtility.WebSocketVersion); Thread.Sleep(3000); Frame openingFrame = null; if (!IsAlwaysReading) { openingFrame = ReadData(); } else { openingFrame = Connection.DataReceived[0]; } Thread.Sleep(1000); IsOpened = true; return(openingFrame); }
public Frame Connect(Uri address, bool storeData, bool isAlwaysReading, bool waitForConnectionOpen = true) { Address = address; StoreData = storeData; Connection = new WebSocketConnect(); if (isAlwaysReading) { InitiateWithAlwaysReading(); } SendWebSocketRequest(WebSocketClientUtility.WebSocketVersion); if (waitForConnectionOpen) { if (!WaitForWebSocketState(WebSocketState.ConnectionOpen)) { throw new Exception("Failed to open a connection"); } } else { Thread.Sleep(3000); } if (this.WebSocketState == WebSocketState.ConnectionOpen) { IsOpened = true; } else { IsOpened = false; } Frame openingFrame = null; if (!IsAlwaysReading) { openingFrame = ReadData(); } else { openingFrame = Connection.DataReceived[0]; } return(openingFrame); }
public void Initiate() { string host = Address.DnsSafeHost; int port = Address.Port; Connection = new WebSocketConnect(); TestUtility.LogInformation("Connecting to {0} on {1}", host, port); Connection.TcpClient = new MyTcpClient(host, port); Connection.Stream = Connection.TcpClient.GetStream(); IsAlwaysReading = false; if (StoreData) { Connection.DataSent = new List <Frame>(); Connection.DataReceived = new List <Frame>(); } }
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(); } }
public Frame Connect(Uri address, bool storeData, bool isAlwaysReading, bool waitForConnectionOpen = true) { Address = address; StoreData = storeData; Connection = new WebSocketConnect(); if (isAlwaysReading) { InitiateWithAlwaysReading(); } SendWebSocketRequest(WebSocketClientUtility.WebSocketVersion); if (waitForConnectionOpen) { if (!WaitForWebSocketState(WebSocketState.ConnectionOpen)) { throw new Exception("Failed to open a connection"); } } else { Thread.Sleep(3000); } if (this.WebSocketState == WebSocketState.ConnectionOpen) { IsOpened = true; } else { IsOpened = false; } Frame openingFrame = null; if (!IsAlwaysReading) { openingFrame = ReadData(); } else { bool success = false; for (int i = 0; i < 10; i++) { if (Connection.DataReceived.Count > 0) { openingFrame = Connection.DataReceived[0]; success = true; break; } else { Thread.Sleep(500); } } if (!success) { throw new Exception("Failed to receive data from server after websocket opening handshake"); } } return(openingFrame); }
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(); } }