Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
 public static byte[] EncodeRequestResponse(RequestResponse msg)
 {
     return(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(msg, Formatting.None) + "<EOF>"));
 }