Main MRPT website > C++ reference for MRPT 1.9.9
SocketsTest_impl.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
13 #include <mrpt/poses/CPose3D.h>
14 #include <cstdio> // printf()
15 #include <thread>
16 #include <chrono>
17 #include <iostream>
18 
20 // Test payload:
21 const mrpt::poses::CPose3D p_tx(1.0, 2.0, 3.0, 0.2, 0.4, 0.6);
22 
24 
26 {
27  using namespace mrpt::comms;
28  using namespace std;
29 
30  try
31  {
32 #ifdef SOCKET_TEST_VERBOSE
33  printf("[Server] Started\n");
34 #endif
35 
36  CServerTCPSocket server(
37  15000, "127.0.0.1", 10,
40 #else
42 #endif
43  );
44  std::unique_ptr<CClientTCPSocket> client = server.accept(2000);
45 
46  if (client)
47  {
48 #ifdef SOCKET_TEST_VERBOSE
49  printf("[Server] Connection accepted\n");
50 #endif
51  // Send a message with the payload:
52  CMessage msg;
53  msg.type = 0x10;
54  msg.serializeObject(&p_tx);
55 
56  client->sendMessage(msg);
57 
58  std::this_thread::sleep_for(50ms);
59  }
60 
61 #ifdef SOCKET_TEST_VERBOSE
62  printf("[Server] Finish\n");
63 #endif
64  }
65  catch (std::exception& e)
66  {
67  cerr << e.what() << endl;
68  }
69  catch (...)
70  {
71  printf("[thread_server] Runtime error!\n");
72  }
73 }
74 
76 {
77  using namespace mrpt::comms;
78  using namespace std;
79 
80  try
81  {
82  CClientTCPSocket sock;
83 
84 #ifdef SOCKET_TEST_VERBOSE
85  printf("[Client] Connecting\n");
86 #endif
87 
88  sock.connect("127.0.0.1", 15000);
89 
90 #ifdef SOCKET_TEST_VERBOSE
91  printf("[Client] Connected. Waiting for a message...\n");
92 #endif
93  // cout << "pending: " << sock.getReadPendingBytes() << endl;
94  // std::this_thread::sleep_for(4000ms);
95  // cout << "pending: " << sock.getReadPendingBytes() << endl;
96 
97  CMessage msg;
98  bool ok = sock.receiveMessage(msg, 2000, 2000);
99 
100  if (!ok)
101  {
102  printf("[Client] Error receiving message!!\n");
103  }
104  else
105  {
106 #ifdef SOCKET_TEST_VERBOSE
107  printf("[Client] Message received OK!:\n");
108  printf(" MSG Type: %i\n", msg.type);
109  printf(
110  " MSG Length: %u bytes\n", (unsigned int)msg.content.size());
111  printf("[Client] Parsing payload...\n");
112 #endif
115 
116 #ifdef SOCKET_TEST_VERBOSE
117  printf("[Client] Received payload: %s\n", p_rx.asString().c_str());
118  printf("[Client] tx payload: %s\n", p_tx.asString().c_str());
119  printf("[Client] Done!!\n");
120 #endif
121 
122  sockets_test_passed_ok = (p_rx == p_tx);
123  }
124 
125 #ifdef SOCKET_TEST_VERBOSE
126  printf("[Client] Finish\n");
127 #endif
128  }
129  catch (std::exception& e)
130  {
131  cerr << e.what() << endl;
132  }
133  catch (...)
134  {
135  cerr << "[thread_client] Runtime error!" << endl;
136  ;
137  }
138 }
139 
140 // ------------------------------------------------------
141 // SocketsTest
142 // ------------------------------------------------------
144 {
145  using namespace std::chrono_literals;
146 
147  std::thread(thread_server).detach();
148  std::this_thread::sleep_for(100ms);
149 
150  std::thread(thread_client).detach();
151  std::this_thread::sleep_for(1000ms);
152 }
thread_server
void thread_server()
Definition: SocketsTest_impl.cpp:25
CClientTCPSocket.h
mrpt::comms::CClientTCPSocket
A TCP socket that can be connected to a TCP server, implementing MRPT's CStream interface for passing...
Definition: CClientTCPSocket.h:34
mrpt::comms::CServerTCPSocket
A TCP socket that can be wait for client connections to enter.
Definition: CServerTCPSocket.h:26
mrpt::comms::CServerTCPSocket::accept
std::unique_ptr< CClientTCPSocket > accept(int timeout_ms=-1)
Waits for an incoming connection (indefinitely, or with a given timeout) The returned object represen...
Definition: CServerTCPSocket_common.cpp:87
mrpt::comms::CClientTCPSocket::receiveMessage
bool receiveMessage(MESSAGE &inMsg, const unsigned int timeoutStart_ms=100, const unsigned int timeoutBetween_ms=1000)
Waits for an incoming message through the TCP stream.
Definition: CClientTCPSocket.h:194
CServerTCPSocket.h
mrpt::comms
Serial and networking devices and utilities.
Definition: CClientTCPSocket.h:21
mrpt::serialization::CMessage::type
uint32_t type
An identifier of the message type (only the least-sig byte is typically sent)
Definition: CMessage.h:34
mrpt::serialization::CMessage
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
Definition: CMessage.h:29
mrpt::comms::CClientTCPSocket::sendMessage
bool sendMessage(const MESSAGE &outMsg, const int timeout_ms=-1)
Send a message through the TCP stream.
Definition: CClientTCPSocket.h:160
mrpt::serialization::CMessage::content
std::vector< uint8_t > content
The contents of the message (memory is automatically handled by the std::vector object)
Definition: CMessage.h:37
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
mrpt::comms::CClientTCPSocket::connect
void connect(const std::string &remotePartAddress, unsigned short remotePartTCPPort, unsigned int timeout_ms=0)
Establishes a connection with a remote part.
Definition: CClientTCPSocket.cpp:137
SocketsTest
void SocketsTest()
Definition: SocketsTest_impl.cpp:143
CPose3D.h
mrpt::system::LVL_DEBUG
@ LVL_DEBUG
Definition: system/COutputLogger.h:30
mrpt::system::LVL_ERROR
@ LVL_ERROR
Definition: system/COutputLogger.h:33
p_tx
const mrpt::poses::CPose3D p_tx(1.0, 2.0, 3.0, 0.2, 0.4, 0.6)
thread_client
void thread_client()
Definition: SocketsTest_impl.cpp:75
SOCKET_TEST_VERBOSE
#define SOCKET_TEST_VERBOSE
Definition: vision_stereo_rectify/test.cpp:10
sockets_test_passed_ok
bool sockets_test_passed_ok
Definition: SocketsTest_impl.cpp:19
mrpt::serialization::CMessage::deserializeIntoExistingObject
void deserializeIntoExistingObject(CSerializable *obj)
A method that parse the data in the message into an existing object.
Definition: CMessage.cpp:44
CMessage.h
mrpt::serialization::CMessage::serializeObject
void serializeObject(const CSerializable *obj)
A method for serializing a MRPT's object into the content.
Definition: CMessage.cpp:21
mrpt::poses::CPose3D::asString
void asString(std::string &s) const
Returns a human-readable textual representation of the object (eg: "[x y z yaw pitch roll]",...
Definition: CPose3D.h:602



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST