@@ -27,11 +27,14 @@ echo_binary_text_client_demo.cpp -lpthread -o echo_client
27
27
#include < iostream>
28
28
#include < cstdlib> // EXIT_SUCCESS
29
29
#include < cstddef> // std::size_t
30
+ #include < cstdint> // std::uint16_t, etc
30
31
#include < string>
31
32
#include < thread>
32
33
#include < chrono>
33
34
#include < cassert>
34
35
36
+ #include " utility/cast_ptr_to.hpp"
37
+
35
38
#include " net_ip/net_ip.hpp"
36
39
#include " net_ip/net_entity.hpp"
37
40
#include " net_ip_component/worker.hpp"
@@ -83,9 +86,9 @@ bool process_args(int argc, char* argv[], bool& print_errors, std::string& ip_ad
83
86
return EXIT_SUCCESS;
84
87
}
85
88
89
+ constexpr std::size_t HDR_SIZE{2 }; // 1st 2 bytes of data is message size
86
90
87
91
int main (int argc, char * argv[]) {
88
- const std::size_t HDR_SIZE = 2 ; // 1st 2 bytes of data is message size
89
92
const std::string PORT = " 5002" ;
90
93
const std::string LOCAL_LOOP = " 127.0.0.1" ;
91
94
@@ -106,7 +109,7 @@ int main(int argc, char* argv[]) {
106
109
// receive text, display to console
107
110
auto msg_hndlr = [] (const_buf buf, io_output io_out, endpoint ep) {
108
111
// create string from buf, omit 1st 2 bytes (header)
109
- std::string s (static_cast < const char *> (buf.data ()) + 2 , buf.size () - 2 );
112
+ std::string s (chops::cast_ptr_to< char > (buf.data ()) + 2 , buf.size () - 2 );
110
113
std::cout << s << std::endl;
111
114
112
115
return true ;
@@ -124,8 +127,8 @@ int main(int argc, char* argv[]) {
124
127
hdr_processed = true ;
125
128
// 1st 2 bytes is message size
126
129
// endian correct data marshalling
127
- uint16_t size = chops::extract_val<uint16_t >
128
- (static_cast <std::byte*> (buf.data ()));
130
+ std:: uint16_t size = chops::extract_val<std:: uint16_t >
131
+ (chops::cast_ptr_to <std::byte> (buf.data ()));
129
132
130
133
return size;
131
134
}
@@ -194,11 +197,11 @@ int main(int argc, char* argv[]) {
194
197
chops::mutable_shared_buffer buf_out;
195
198
196
199
// 1st 2 bytes size of message (header) are the string length
197
- uint16_t size_val = s.size ();
200
+ std:: uint16_t size_val = static_cast <std:: uint16_t >( s.size ()); // narrowing, hence the static cast
198
201
// endian correct data marshalling
199
202
std::byte tbuf[HDR_SIZE]; // temp buffer to hold the header
200
203
// write those 2 bytes to the temp buffer
201
- std::size_t result = chops::append_val<uint16_t >(tbuf, size_val);
204
+ std::size_t result = chops::append_val<std:: uint16_t >(tbuf, size_val);
202
205
assert (result == HDR_SIZE);
203
206
// now append our header and string data to the output buffer
204
207
buf_out.append (tbuf, sizeof (tbuf)); // write the header
0 commit comments