public void StartClient() { RequestResponse response = GetBatch; if (response == null) { return; } // Connect to a remote device. _received.Clear(); remoteEP = new IPEndPoint(IPAddress.Parse(Params.TEST_RECEIVER_HOST), Params.TEST_RECEIVER_PORT); sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { _notified = false; sender.LingerState = _linger; // Establish the remote endpoint for the socket. // This example uses port 11000 on the local computer. // Connect the socket to the remote endpoint. Catch any errors. try { IAsyncResult result = sender.BeginConnect(remoteEP, null, null); bool success = result.AsyncWaitHandle.WaitOne(Params.TIMEOUT, true); if (success && sender.Connected) { sender.EndConnect(result); // Encode the data string into a byte array. byte[] msg = Encoder.EncodeRequestResponse(response); // Send the data through the socket. int bytesSent = sender.Send(msg); // bool sendSuccess = Task.Run(() => { // Data buffer for incoming data. byte[] bytes = new byte[2048 * Params.BATCH_SIZE]; // hope thats enough // byte[] bytes = new byte[10240]; // hope thats enough // Receive the response from the remote device. int bytesRec = sender.Receive(bytes); if (bytesRec < 1) { HandleFailure(response); } try { StatusResponse r1 = Encoder.DecodePointResponse(bytes); OnResponse(response, r1); _notified = true; } catch { HandleFailure(response); } // }).Wait(Params.TIMEOUT); if (!_notified) { HandleFailure(response); } // if (!sendSuccess && !_notified) HandleFailure(response); } } catch (ArgumentNullException ane) { Logger.Write(Logger.Tag.ERROR, string.Format("ArgumentNullException : {0}", ane.ToString())); } catch (SocketException se) { Logger.Write(Logger.Tag.ERROR, string.Format("ArgumentNullException : {0}", se.ToString())); } catch (Exception e) { Logger.Write(Logger.Tag.ERROR, string.Format("ArgumentNullException : {0}", e.ToString())); } } catch (Exception e) { Logger.Write(Logger.Tag.ERROR, e.ToString()); } finally { try { // Release the socket. sender.Shutdown(SocketShutdown.Both); sender.Close(0); } catch {} } if (!_notified) { HandleFailure(response); } }
/// <summary> /// Establishes connection to the remote host, sends the request an activates OnResponse on the requesthandler /// </summary> public void StartClient(Driver driver, DataRequestBatch request) { _received.Clear(); remoteEP = new IPEndPoint(IPAddress.Parse(Host), Port); sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { _notified = false; sender.LingerState = _linger; // Establish the remote endpoint for the socket. try { IAsyncResult result = sender.BeginConnect(remoteEP, null, null); bool success = result.AsyncWaitHandle.WaitOne(Params.TIMEOUT, true); if (success && sender.Connected) { sender.EndConnect(result); // Encode the data string into a byte array. byte[] msg = Encoder.EncodeDataRequestBatch(request); // Send the data through the socket. bool sendSuccess = Task.Run(() => { int bytesSent = sender.Send(msg); // Data buffer for incoming data. // TODO: implement proper parsing of incoming response data byte[] bytes = new byte[2048 * Params.BATCH_SIZE]; // hope thats enough // byte[] bytes = new byte[10240]; // hope thats enough // Receive the response from the remote device. int bytesRec = sender.Receive(bytes); if (bytesRec < 1) { HandleFailure(driver, request); } StatusResponse response = Encoder.DecodePointResponse(bytes); if (response == null) { HandleFailure(driver, request); } else { new DriverResponseHandler().OnResponse(driver, request, response); } _notified = true; }).Wait(Params.TIMEOUT); if (!sendSuccess && !_notified) { HandleFailure(driver, request); } if (!_notified) { HandleFailure(driver, request); } } } catch (ArgumentNullException ane) { Logger.Write(Logger.Tag.ERROR, string.Format("ArgumentNullException : {0}", ane.ToString())); } catch (SocketException se) { Logger.Write(Logger.Tag.ERROR, string.Format("ArgumentNullException : {0}", se.ToString())); } catch (Exception e) { Logger.Write(Logger.Tag.ERROR, string.Format("ArgumentNullException : {0}", e.ToString())); } } catch (Exception e) { Logger.Write(Logger.Tag.ERROR, e.ToString()); } finally { try { // Release the socket. sender.Shutdown(SocketShutdown.Both); sender.Close(0); } catch {} } if (!_notified) { HandleFailure(driver, request); } }