MRPT  2.0.4
CServerTCPSocket.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/core/common.h>
13 #include <memory> // unique_ptr
14 #include <string>
15 
16 namespace mrpt::comms
17 {
18 class CClientTCPSocket;
19 
20 /** A TCP socket that can be wait for client connections to enter.
21  * Unless otherwise noticed, operations are blocking.
22  * \ingroup mrpt_comms_grp
23  */
25 {
26  public:
27  /** Constructor that creates the socket, performs binding, and start
28  * listening mode.
29  * \param listenPort The port to bound to.
30  * \param IPaddress The interface to bound the socket to. By default is
31  * 127.0.0.1 for localhost, for all network interfaces use 0.0.0.0.
32  * \param maxConnectionsWaiting Maximum number of incoming connections
33  * waiting for "accept" before new ones are rejected.
34  * You can check if the socket has been created OK with "isListening".
35  * \sa isListening
36  * \exception std::exception If there is any error creating the socket,
37  * with a textual description of the error.
38  */
40  unsigned short listenPort,
41  const std::string& IPaddress = std::string("127.0.0.1"),
42  int maxConnectionsWaiting = 50,
44  /** Dtor */
45  ~CServerTCPSocket() override;
46 
47  /** Returns true if the socket was successfully open and it's bound to the
48  * desired port. */
49  bool isListening();
50 
51  /** Waits for an incoming connection (indefinitely, or with a given timeout)
52  * The returned object represents the new connection, and MUST BE deleted
53  * by the user when no longer needed.
54  * \param timeout_ms The timeout for the waiting, in milliseconds. Set this
55  * to "-1" to disable timeout (i.e. timeout=infinite)
56  * \return The incoming connection, or nullptr on timeout or error.
57  */
58  std::unique_ptr<CClientTCPSocket> accept(int timeout_ms = -1);
59 
60  private:
61 /** The handle for the listening server TCP socket. */
62 #ifdef _WIN32
63  unsigned int
64 #else
65  int
66 #endif
68 
69  /** Returns a description of the last Sockets error */
70  std::string getLastErrorStr();
71 
72  /** Common code called from the platform-dependant constructor */
73  void setupSocket(
74  unsigned short listenPort, const std::string& IPaddress,
75  int maxConnectionsWaiting);
76 
77 }; // End of class def.
78 
79 } // namespace mrpt::comms
VerbosityLevel
Enumeration of available verbosity levels.
std::string getLastErrorStr()
Returns a description of the last Sockets error.
unsigned int m_serverSock
The handle for the listening server TCP socket.
std::unique_ptr< CClientTCPSocket > accept(int timeout_ms=-1)
Waits for an incoming connection (indefinitely, or with a given timeout) The returned object represen...
A TCP socket that can be wait for client connections to enter.
void setupSocket(unsigned short listenPort, const std::string &IPaddress, int maxConnectionsWaiting)
Common code called from the platform-dependant constructor.
Versatile class for consistent logging and management of output messages.
bool isListening()
Returns true if the socket was successfully open and it&#39;s bound to the desired port.
Serial and networking devices and utilities.
CServerTCPSocket(unsigned short listenPort, const std::string &IPaddress=std::string("127.0.0.1"), int maxConnectionsWaiting=50, mrpt::system::VerbosityLevel verbosityLevel=mrpt::system::LVL_INFO)
Constructor that creates the socket, performs binding, and start listening mode.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020