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 
20 #include <sys/socket.h> /* for socket(), bind(), and connect() */
21 #include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
22 #include "sick_scan/tcp/Mutex.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 #ifndef _MSC_VER
70  INT32 m_connectionSocket; // Socket, wenn wir der Client sind (z.B. Verbindung zum Scanner)
71 #else
72  SOCKET m_connectionSocket; // Socket, wenn wir der Client sind (z.B. Verbindung zum Scanner)
73 
74 #endif
75  void readThreadFunction(bool& endThread, UINT16& waitTimeMs);
78 
79  ReadFunction m_readFunction; // Receive callback
80  void* m_readFunctionObjPtr; // Object of the Receive callback
82  void* m_disconnectFunctionObjPtr; // Object of the Disconect callback
83 
84 
85 /*
86  bool m_beVerbose; ///< Enable *very* verbose debug output
87  asio::io_service m_io_service;
88  asio::ip::tcp::socket* m_socket;
89 
90  boost::thread* m_readThreadPtr;
91  bool m_readThreadIsRunning;
92  bool m_readThreadShouldRun;
93  boost::condition m_readThreadCondition; ///< Condition to wait for the spawned thread to be started
94  boost::mutex m_readThreadMutex; ///< Mutex to wait for the spawned thread to be started
95  void readThread(); ///< Receive-thread for incoming scans.
96  INT32 readInputData();
97 
101  EventMonitor* m_disconnectedEventMonitor;
102 
106  EventMonitor::Mask m_disconnectedEventMask;
107 
108  BoundedBuffer<UINT8> m_rxBuffer; ///< Main input buffer
109  std::string m_rxString; ///< Eingangspuffer fuer Strings
110  bool m_longStringWarningPrinted;
111 */
112 };
113 
114 #endif // TCP_HPP
Tcp::isClientConnected_unlocked
bool isClientConnected_unlocked()
UINT16
uint16_t UINT16
Definition: BasicDatatypes.hpp:27
UINT8
uint8_t UINT8
Definition: BasicDatatypes.hpp:29
Tcp::isOpen
bool isOpen()
Definition: tcp.cpp:95
Tcp::m_rxBuffer
std::list< unsigned char > m_rxBuffer
Definition: tcp.hpp:61
BasicDatatypes.hpp
Tcp::getNumReadableBytes
UINT32 getNumReadableBytes()
Definition: tcp.cpp:375
Tcp::m_disconnectFunctionObjPtr
void * m_disconnectFunctionObjPtr
Definition: tcp.hpp:82
Tcp::DisconnectFunction
void(* DisconnectFunction)(void *obj)
Definition: tcp.hpp:53
Tcp::m_rxString
std::string m_rxString
Definition: tcp.hpp:59
Tcp::stopReadThread
void stopReadThread()
Definition: tcp.cpp:358
Tcp::ReadFunction
void(* ReadFunction)(void *obj, UINT8 *inputBuffer, UINT32 &numBytes)
Definition: tcp.hpp:49
Tcp::~Tcp
~Tcp()
Definition: tcp.cpp:40
INT32
int32_t INT32
Definition: BasicDatatypes.hpp:25
Tcp::m_readThread
SickThread< Tcp, &Tcp::readThreadFunction > m_readThread
Definition: tcp.hpp:76
Tcp::write
bool write(UINT8 *buffer, UINT32 numberOfBytes)
Definition: tcp.cpp:51
Tcp::setDisconnectCallbackFunction
void setDisconnectCallbackFunction(DisconnectFunction discFunction, void *obj)
Definition: tcp.cpp:82
Tcp::stopServerThread
void stopServerThread()
SickThread< Tcp, &Tcp::readThreadFunction >
Mutex.hpp
Tcp::m_readFunctionObjPtr
void * m_readFunctionObjPtr
Definition: tcp.hpp:80
Tcp::m_serverAddr
struct sockaddr_in m_serverAddr
Definition: tcp.hpp:66
Tcp::readString
std::string readString(UINT8 delimiter)
Definition: tcp.cpp:410
Mutex
Definition: Mutex.hpp:15
Tcp::m_longStringWarningPrinted
bool m_longStringWarningPrinted
Definition: tcp.hpp:58
Tcp::m_beVerbose
bool m_beVerbose
Definition: tcp.hpp:67
Tcp
Definition: tcp.hpp:30
Tcp::readInputData
INT32 readInputData()
Definition: tcp.cpp:229
Tcp::setReadCallbackFunction
void setReadCallbackFunction(ReadFunction readFunction, void *obj)
Definition: tcp.cpp:110
Tcp::m_readFunction
ReadFunction m_readFunction
Definition: tcp.hpp:79
SickThread.hpp
Tcp::read
UINT32 read(UINT8 *buffer, UINT32 bufferLen)
Definition: tcp.cpp:389
Tcp::m_connectionSocket
INT32 m_connectionSocket
Definition: tcp.hpp:70
Tcp::startServerThread
void startServerThread()
UINT32
uint32_t UINT32
Definition: BasicDatatypes.hpp:26
Tcp::close
void close()
Definition: tcp.cpp:326
Tcp::m_socketMutex
Mutex m_socketMutex
Definition: tcp.hpp:68
Tcp::readThreadFunction
void readThreadFunction(bool &endThread, UINT16 &waitTimeMs)
Definition: tcp.cpp:195
Tcp::m_disconnectFunction
DisconnectFunction m_disconnectFunction
Definition: tcp.hpp:81
Tcp::Tcp
Tcp()
Definition: tcp.cpp:22
Tcp::open
bool open(std::string ipAddress, UINT16 port, bool enableVerboseDebugOutput=false)
Definition: tcp.cpp:136


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Thu Sep 8 2022 02:30:19