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
UINT32 getNumReadableBytes()
Definition: tcp.cpp:375
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:15
void * m_disconnectFunctionObjPtr
Definition: tcp.hpp:82
bool write(UINT8 *buffer, UINT32 numberOfBytes)
Definition: tcp.cpp:51
SickThread< Tcp,&Tcp::readThreadFunction > m_readThread
Definition: tcp.hpp:76
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:195
bool open(std::string ipAddress, UINT16 port, bool enableVerboseDebugOutput=false)
Definition: tcp.cpp:136
void * m_readFunctionObjPtr
Definition: tcp.hpp:80
ReadFunction m_readFunction
Definition: tcp.hpp:79
void startServerThread()
void(* ReadFunction)(void *obj, UINT8 *inputBuffer, UINT32 &numBytes)
Definition: tcp.hpp:49
INT32 readInputData()
Definition: tcp.cpp:229
DisconnectFunction m_disconnectFunction
Definition: tcp.hpp:81
Definition: tcp.hpp:30
std::string readString(UINT8 delimiter)
Definition: tcp.cpp:410
int32_t INT32
bool isClientConnected_unlocked()
Mutex m_socketMutex
Definition: tcp.hpp:68
~Tcp()
Definition: tcp.cpp:40
bool isOpen()
Definition: tcp.cpp:95
Tcp()
Definition: tcp.cpp:22
UINT32 read(UINT8 *buffer, UINT32 bufferLen)
Definition: tcp.cpp:389
void setReadCallbackFunction(ReadFunction readFunction, void *obj)
Definition: tcp.cpp:110
void setDisconnectCallbackFunction(DisconnectFunction discFunction, void *obj)
Definition: tcp.cpp:82
void close()
Definition: tcp.cpp:326
INT32 m_connectionSocket
Definition: tcp.hpp:70
void stopReadThread()
Definition: tcp.cpp:358
uint8_t UINT8


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Tue Jul 9 2019 04:55:32