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/system/scheduler.h> // changeCurrentThreadPriority()
14 #include <mrpt/poses/CPose3D.h>
15 #include <cstdio> // printf()
16 #include <thread>
17 #include <chrono>
18 #include <iostream>
19 
21 // Test payload:
22 const mrpt::poses::CPose3D p_tx(1.0, 2.0, 3.0, 0.2, 0.4, 0.6);
23 
25 
27 {
28  using namespace mrpt::comms;
29  using namespace std;
30 
31  try
32  {
33 #ifdef SOCKET_TEST_VERBOSE
34  printf("[Server] Started\n");
35 #endif
36 
37  CServerTCPSocket server(
38  15000, "127.0.0.1", 10,
41 #else
43 #endif
44  );
45  std::unique_ptr<CClientTCPSocket> client = server.accept(2000);
46 
47  if (client)
48  {
49 #ifdef SOCKET_TEST_VERBOSE
50  printf("[Server] Connection accepted\n");
51 #endif
52  // Send a message with the payload:
53  CMessage msg;
54  msg.type = 0x10;
55  msg.serializeObject(&p_tx);
56 
57  client->sendMessage(msg);
58 
59  std::this_thread::sleep_for(50ms);
60  }
61 
62 #ifdef SOCKET_TEST_VERBOSE
63  printf("[Server] Finish\n");
64 #endif
65  }
66  catch (std::exception& e)
67  {
68  cerr << e.what() << endl;
69  }
70  catch (...)
71  {
72  printf("[thread_server] Runtime error!\n");
73  }
74 }
75 
77 {
78  using namespace mrpt::comms;
79  using namespace std;
80 
82 
83  try
84  {
85  CClientTCPSocket sock;
86 
87 #ifdef SOCKET_TEST_VERBOSE
88  printf("[Client] Connecting\n");
89 #endif
90 
91  sock.connect("127.0.0.1", 15000);
92 
93 #ifdef SOCKET_TEST_VERBOSE
94  printf("[Client] Connected. Waiting for a message...\n");
95 #endif
96  // cout << "pending: " << sock.getReadPendingBytes() << endl;
97  // std::this_thread::sleep_for(4000ms);
98  // cout << "pending: " << sock.getReadPendingBytes() << endl;
99 
100  CMessage msg;
101  bool ok = sock.receiveMessage(msg, 2000, 2000);
102 
103  if (!ok)
104  {
105  printf("[Client] Error receiving message!!\n");
106  }
107  else
108  {
109 #ifdef SOCKET_TEST_VERBOSE
110  printf("[Client] Message received OK!:\n");
111  printf(" MSG Type: %i\n", msg.type);
112  printf(
113  " MSG Length: %u bytes\n", (unsigned int)msg.content.size());
114  printf("[Client] Parsing payload...\n");
115 #endif
118 
119 #ifdef SOCKET_TEST_VERBOSE
120  printf("[Client] Received payload: %s\n", p_rx.asString().c_str());
121  printf("[Client] tx payload: %s\n", p_tx.asString().c_str());
122  printf("[Client] Done!!\n");
123 #endif
124 
125  sockets_test_passed_ok = (p_rx == p_tx);
126  }
127 
128 #ifdef SOCKET_TEST_VERBOSE
129  printf("[Client] Finish\n");
130 #endif
131  }
132  catch (std::exception& e)
133  {
134  cerr << e.what() << endl;
135  }
136  catch (...)
137  {
138  cerr << "[thread_client] Runtime error!" << endl;
139  ;
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(100ms);
152 
153  std::thread(thread_client).detach();
154  std::this_thread::sleep_for(1000ms);
155 }
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:600
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:86
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 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020