tcp.hpp
Go to the documentation of this file.
1 //
2 // TCP.hpp
3 //
4 // Ethernet TCP data sender/receiver.
5 //
6 // Sick AG
7 //
8 // HISTORY
9 //
10 // 1.0.0 10.11.2011, VWi
11 // Initial version.
12 //
13 
14 
15 
16 #ifndef TCP_HPP
17 #define TCP_HPP
18 
19 #include "../BasicDatatypes.hpp"
20 #include <sys/socket.h> /* for socket(), bind(), and connect() */
21 #include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
22 #include "../tools/Mutex.hpp"
23 #include "../tools/SickThread.hpp"
24 #include <list>
25 
26 
27 //
28 // Sender and receiver for data over a TCP connection. Client!
29 //
30 class Tcp
31 {
32 public:
33  Tcp();
34  ~Tcp();
35 
36  // Opens the connection.
37  bool open(std::string ipAddress, UINT16 port, bool enableVerboseDebugOutput = false);
38  bool open(UINT32 ipAddress, UINT16 port, bool enableVerboseDebugOutput = false);
39  void close(); // Closes the connection, if it was open.
40  bool isOpen(); // "True" if a connection is currently open.
41  bool write(UINT8* buffer, UINT32 numberOfBytes); // Writes numberOfBytes bytes to the open connection.
42  std::string readString(UINT8 delimiter); // Reads a string, if available. Strings are separated with the delimiter char.
43 
44  // Buffer read function (for polling)
45  UINT32 getNumReadableBytes(); // Returns the number of bytes currently available for reading.
46  UINT32 read(UINT8* buffer, UINT32 bufferLen); // Reads up to bufferLen bytes from the buffer.
47 
48  // Read callbacks (for being called when data is available)
49  typedef void (*ReadFunction)(void* obj, UINT8* inputBuffer, UINT32& numBytes); // ReadFunction
50  void setReadCallbackFunction(ReadFunction readFunction, void* obj);
51 
52  // Information if the connection is disconnected.
53  typedef void (*DisconnectFunction)(void* obj); // Called on disconnect
54  void setDisconnectCallbackFunction(DisconnectFunction discFunction, void* obj);
55 
56 
57 private:
59  std::string m_rxString; // fuer readString()
61  std::list<unsigned char> m_rxBuffer; // Main input buffer
62  void stopReadThread();
63  void startServerThread();
64  void stopServerThread();
65 
66  struct sockaddr_in m_serverAddr; // Local address
69 
70  INT32 m_connectionSocket; // Socket, wenn wir der Client sind (z.B. Verbindung zum Scanner)
71 
72  void readThreadFunction(bool& endThread, UINT16& waitTimeMs);
75 
76  ReadFunction m_readFunction; // Receive callback
77  void* m_readFunctionObjPtr; // Object of the Receive callback
79  void* m_disconnectFunctionObjPtr; // Object of the Disconect callback
80 
81 
82 /*
83  bool m_beVerbose; ///< Enable *very* verbose debug output
84  asio::io_service m_io_service;
85  asio::ip::tcp::socket* m_socket;
86 
87  boost::thread* m_readThreadPtr;
88  bool m_readThreadIsRunning;
89  bool m_readThreadShouldRun;
90  boost::condition m_readThreadCondition; ///< Condition to wait for the spawned thread to be started
91  boost::mutex m_readThreadMutex; ///< Mutex to wait for the spawned thread to be started
92  void readThread(); ///< Receive-thread for incoming scans.
93  INT32 readInputData();
94 
98  EventMonitor* m_disconnectedEventMonitor;
99 
103  EventMonitor::Mask m_disconnectedEventMask;
104 
105  BoundedBuffer<UINT8> m_rxBuffer; ///< Main input buffer
106  std::string m_rxString; ///< Eingangspuffer fuer Strings
107  bool m_longStringWarningPrinted;
108 */
109 };
110 
111 #endif // TCP_HPP
UINT32 getNumReadableBytes()
Definition: tcp.cpp:332
void stopServerThread()
bool m_longStringWarningPrinted
Definition: tcp.hpp:58
uint16_t UINT16
struct sockaddr_in m_serverAddr
Definition: tcp.hpp:66
void(* DisconnectFunction)(void *obj)
Definition: tcp.hpp:53
uint32_t UINT32
Definition: Mutex.hpp:16
void * m_disconnectFunctionObjPtr
Definition: tcp.hpp:79
bool write(UINT8 *buffer, UINT32 numberOfBytes)
Definition: tcp.cpp:47
SickThread< Tcp,&Tcp::readThreadFunction > m_readThread
Definition: tcp.hpp:73
bool m_beVerbose
Definition: tcp.hpp:67
std::string m_rxString
Definition: tcp.hpp:59
std::list< unsigned char > m_rxBuffer
Definition: tcp.hpp:61
void readThreadFunction(bool &endThread, UINT16 &waitTimeMs)
Definition: tcp.cpp:184
bool open(std::string ipAddress, UINT16 port, bool enableVerboseDebugOutput=false)
Definition: tcp.cpp:129
void * m_readFunctionObjPtr
Definition: tcp.hpp:77
ReadFunction m_readFunction
Definition: tcp.hpp:76
void startServerThread()
void(* ReadFunction)(void *obj, UINT8 *inputBuffer, UINT32 &numBytes)
Definition: tcp.hpp:49
INT32 readInputData()
Definition: tcp.cpp:217
DisconnectFunction m_disconnectFunction
Definition: tcp.hpp:78
Definition: tcp.hpp:30
std::string readString(UINT8 delimiter)
Definition: tcp.cpp:367
int32_t INT32
bool isClientConnected_unlocked()
Mutex m_socketMutex
Definition: tcp.hpp:68
~Tcp()
Definition: tcp.cpp:36
bool isOpen()
Definition: tcp.cpp:88
Tcp()
Definition: tcp.cpp:18
UINT32 read(UINT8 *buffer, UINT32 bufferLen)
Definition: tcp.cpp:346
void setReadCallbackFunction(ReadFunction readFunction, void *obj)
Definition: tcp.cpp:103
void setDisconnectCallbackFunction(DisconnectFunction discFunction, void *obj)
Definition: tcp.cpp:75
void close()
Definition: tcp.cpp:285
INT32 m_connectionSocket
Definition: tcp.hpp:70
void stopReadThread()
Definition: tcp.cpp:315
uint8_t UINT8


libsick_ldmrs
Author(s): SICK AG , Martin Günther , Jochen Sprickerhof
autogenerated on Mon Oct 26 2020 03:27:30