libcyberradio  22.01.24
ClientSocket.h
1 /***************************************************************************
2  * \file ClientSocket.h
3  *
4  * \brief NDR651 client socket class.
5  *
6  * \author NH
7  * \copyright Copyright (c) 2015-2021 CyberRadio Solutions, Inc.
8  *
9  */
10 
11 #ifndef INCLUDED_LIBCYBERRADIO_NDR651_CLIENTSOCKET_H
12 #define INCLUDED_LIBCYBERRADIO_NDR651_CLIENTSOCKET_H
13 
14 //#include <unistd.h>
15 #include <iostream> //cout
16 #include <stdio.h> //printf
17 #include <string.h> //strlen
18 #include <string> //string
19 #include <sys/socket.h> //socket
20 #include <arpa/inet.h> //inet_addr
21 #include <netdb.h> //hostent
22 #include <vector>
23 #include <boost/thread/mutex.hpp>
24 #include "LibCyberRadio/Common/BasicList.h"
25 
26 #define RX_BUFF_SIZE 1024
27 
28 
32 namespace LibCyberRadio
33 {
38  namespace NDR651
39  {
44  {
45  public:
52  ClientSocket(const std::string& hostname, unsigned int port,
53  bool debug=true);
57  virtual ~ClientSocket();
62  bool isConnected(void) { return _connected; }
67  bool connectToServer(void);
72  bool disconnect(void);
78  bool sendCmd(const std::string& cmd);
85  bool getRsp(BasicStringList &rsp, float timeout);
94  bool sendCmdAndGetRsp(const std::string& cmd, BasicStringList &rsp,
95  float timeout, bool print);
103  bool sendCmdAndGetRsp(const std::string& cmd, BasicStringList &rsp,
104  float timeout);
105 
106  private:
107  bool _connected;
108  int _sockfd;
109  fd_set set;
110 
111  std::string _serverHostname;
112  int _serverPort;
113  bool _debug;
114 
115  char _rxBuff[RX_BUFF_SIZE];
116  void _clearRxBuff(void) { memset(_rxBuff, 0, RX_BUFF_SIZE); }
117  void _tcpRx(void) { recv(_sockfd, _rxBuff, RX_BUFF_SIZE, 0); }
118 
119  boost::mutex _trxMutex;
120 
121  };
122 
123  }
124 }
125 
126 #endif /* INCLUDED_LIBCYBERRADIO_NDR651_CLIENTSOCKET_H */
bool connectToServer(void)
Connects to the server.
bool disconnect(void)
Disconnects from the server.
bool sendCmdAndGetRsp(const std::string &cmd, BasicStringList &rsp, float timeout, bool print)
Sends a command to the server and gets the response.
bool isConnected(void)
Gets whether or not the socket is connected.
Definition: ClientSocket.h:62
bool sendCmd(const std::string &cmd)
Sends a command to the server.
BASIC_LIST_CONTAINER< std::string > BasicStringList
Type representing a list of strings.
Definition: BasicList.h:25
Defines functionality for LibCyberRadio applications.
Definition: App.h:23
bool getRsp(BasicStringList &rsp, float timeout)
Gets a command response from the server.
ClientSocket(const std::string &hostname, unsigned int port, bool debug=true)
Constructs a ClientSocket object.
virtual ~ClientSocket()
Destroys a ClientSocket object.