GNU Radio's TEST Package
tcp_client.h
Go to the documentation of this file.
1/* -*- c++ -*- */
2/*
3 * Copyright 2018 Lucas Teske <lucas@teske.com.br>
4 *
5 * GNU Radio is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3, or (at your option)
8 * any later version.
9 *
10 * GNU Radio is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GNU Radio; see the file COPYING. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifndef TCPCLIENT_H_
22#define TCPCLIENT_H_
23
24#include <atomic>
25#include <chrono>
26#include <thread>
27#include <cstdint>
28#include <stdint.h>
29#include <memory.h>
30#include <sstream>
31#include <iostream>
32
33#ifdef _WIN32
34# include <winsock2.h>
35# include <Ws2tcpip.h>
36#else
37# include <sys/socket.h>
38# include <netinet/in.h>
39#endif
40
41
42#if defined(__GNUC__) || defined(__MINGW32__)
43#include <unistd.h>
44#endif
45
46
48private:
49 int port;
50 #ifdef _WIN32
51 static std::atomic_bool initialized;
52 static std::atomic_uint sockCount;
53 void socket_initialize();
54 #endif
55protected:
56 struct sockaddr_in socketAddr;
57 int s;
58public:
60 tcp_client(std::string addr, int port);
62
64 void close_conn();
65
66 void receive_data(char *data, int length);
67 void send_data(char *data, int length);
68 uint64_t available_data();
69
70 inline void wait_for_data(uint64_t bytes, uint32_t timeout) {
71 uint32_t checkTime = (int) time(NULL);
72 while (available_data() < bytes) {
73 if (((int) time(NULL)) - checkTime > timeout) {
74 return;
75 }
76
77 std::this_thread::sleep_for(std::chrono::microseconds(10));
78 }
79 }
80};
81
82#endif /* TCPCLIENT_H_ */
Definition: tcp_client.h:47
void connect_conn()
void close_conn()
void wait_for_data(uint64_t bytes, uint32_t timeout)
Definition: tcp_client.h:70
void send_data(char *data, int length)
void receive_data(char *data, int length)
tcp_client(std::string addr, int port)
int s
Definition: tcp_client.h:57
tcp_client()
Definition: tcp_client.h:59
uint64_t available_data()
struct sockaddr_in socketAddr
Definition: tcp_client.h:56