MRPT  2.0.2
SocketsTest_impl.cpp
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 
12 #include <mrpt/poses/CPose3D.h>
14 #include <mrpt/system/scheduler.h> // changeCurrentThreadPriority()
15 #include <chrono>
16 #include <cstdio> // printf()
17 #include <iostream>
18 #include <thread>
19 
21 
22 // Test payload:
23 const mrpt::poses::CPose3D p_tx(1.0, 2.0, 3.0, 0.2, 0.4, 0.6);
24 
26 
28 {
29  using namespace mrpt::comms;
30  using namespace std;
31 
32  try
33  {
34 #ifdef SOCKET_TEST_VERBOSE
35  printf("[Server] Started\n");
36 #endif
37 
38  CServerTCPSocket server(
39  15000, "127.0.0.1", 10,
42 #else
44 #endif
45  );
46  std::unique_ptr<CClientTCPSocket> client = server.accept(5000);
47 
48  if (client)
49  {
50 #ifdef SOCKET_TEST_VERBOSE
51  printf("[Server] Connection accepted\n");
52 #endif
53  // Send a message with the payload:
54  CMessage msg;
55  msg.type = 0x10;
56  msg.serializeObject(&p_tx);
57 
58  client->sendMessage(msg);
59 
60  std::this_thread::sleep_for(2000ms);
61  }
62 
63 #ifdef SOCKET_TEST_VERBOSE
64  printf("[Server] Finish\n");
65 #endif
66  }
67  catch (const std::exception& e)
68  {
69  cerr << e.what() << endl;
70  }
71  catch (...)
72  {
73  printf("[thread_server] Runtime error!\n");
74  }
75 }
76 
78 {
79  using namespace mrpt::comms;
80  using namespace std;
81 
83 
84  try
85  {
86  CClientTCPSocket sock;
87 
88 #ifdef SOCKET_TEST_VERBOSE
89  printf("[Client] Connecting\n");
90 #endif
91 
92  sock.connect("127.0.0.1", 15000);
93 
94 #ifdef SOCKET_TEST_VERBOSE
95  printf("[Client] Connected. Waiting for a message...\n");
96 #endif
97  // cout << "pending: " << sock.getReadPendingBytes() << endl;
98  // std::this_thread::sleep_for(500ms);
99  // cout << "pending: " << sock.getReadPendingBytes() << endl;
100 
101  CMessage msg;
102  bool ok = sock.receiveMessage(msg, 2000, 2000);
103 
104  if (!ok)
105  {
106  printf("[Client] Error receiving message!!\n");
107  }
108  else
109  {
110 #ifdef SOCKET_TEST_VERBOSE
111  printf("[Client] Message received OK!:\n");
112  printf(" MSG Type: %i\n", msg.type);
113  printf(
114  " MSG Length: %u bytes\n", (unsigned int)msg.content.size());
115  printf("[Client] Parsing payload...\n");
116 #endif
119 
120 #ifdef SOCKET_TEST_VERBOSE
121  printf("[Client] Received payload: %s\n", p_rx.asString().c_str());
122  printf("[Client] tx payload: %s\n", p_tx.asString().c_str());
123  printf("[Client] Done!!\n");
124 #endif
125 
126  sockets_test_passed_ok = (p_rx == p_tx);
127  }
128 
129 #ifdef SOCKET_TEST_VERBOSE
130  printf("[Client] Finish\n");
131 #endif
132  }
133  catch (const std::exception& e)
134  {
135  cerr << e.what() << endl;
136  }
137  catch (...)
138  {
139  cerr << "[thread_client] Runtime error!" << endl;
140  }
141 }
142 
143 // ------------------------------------------------------
144 // SocketsTest
145 // ------------------------------------------------------
147 {
148  using namespace std::chrono_literals;
149 
150  std::thread(thread_server).detach();
151  std::this_thread::sleep_for(20ms);
152 
153  std::thread t2(thread_client);
154  std::this_thread::sleep_for(200ms);
155  t2.join();
156 }
bool sendMessage(const MESSAGE &outMsg, const int timeout_ms=-1)
Send a message through the TCP stream.
void connect(const std::string &remotePartAddress, unsigned short remotePartTCPPort, unsigned int timeout_ms=0)
Establishes a connection with a remote part.
void changeCurrentThreadPriority(TThreadPriority priority)
Change the priority of the current thread - for Windows, see also changeCurrentProcessPriority() ...
Definition: scheduler.cpp:43
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.
uint32_t type
An identifier of the message type (only the least-sig byte is typically sent)
Definition: CMessage.h:32
STL namespace.
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 thread_client()
void thread_server()
bool sockets_test_passed_ok
const mrpt::poses::CPose3D p_tx(1.0, 2.0, 3.0, 0.2, 0.4, 0.6)
void SocketsTest()
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:618
A class that contain generic messages, that can be sent and received from a "CClientTCPSocket" object...
Definition: CMessage.h:27
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
A TCP socket that can be connected to a TCP server, implementing MRPT&#39;s CStream interface for passing...
void deserializeIntoExistingObject(CSerializable *obj)
A method that parse the data in the message into an existing object.
Definition: CMessage.cpp:44
std::vector< uint8_t > content
The contents of the message (memory is automatically handled by the std::vector object) ...
Definition: CMessage.h:35
Serial and networking devices and utilities.
void serializeObject(const CSerializable *obj)
A method for serializing a MRPT&#39;s object into the content.
Definition: CMessage.cpp:21
Win32: THREAD_PRIORITY_BELOW_NORMAL.
Definition: scheduler.h:36
#define SOCKET_TEST_VERBOSE



Page generated by Doxygen 1.8.14 for MRPT 2.0.2 Git: 9b4fd2465 Mon May 4 16:59:08 2020 +0200 at lun may 4 17:26:07 CEST 2020