@@ -34,6 +34,13 @@ SocketAddress udp_addr;
34
34
Mutex iomutex;
35
35
char uuid[GREENTEA_UUID_LENGTH] = {0 };
36
36
37
+ // Thread safe printf macro
38
+ #define TS_PRINTF (...) {\
39
+ iomutex.lock ();\
40
+ printf (__VA_ARGS__);\
41
+ iomutex.unlock ();\
42
+ }
43
+
37
44
// NOTE: assuming that "id" stays in the single digits
38
45
//
39
46
// Creates a buffer that first contains the thread's id.
@@ -64,7 +71,6 @@ void prep_buffer(unsigned int id, char *uuid, char *tx_buffer, size_t tx_size) {
64
71
}
65
72
}
66
73
67
-
68
74
// Each echo class is in charge of one parallel transaction
69
75
class Echo {
70
76
private:
@@ -105,36 +111,26 @@ class Echo {
105
111
prep_buffer (id, uuid, tx_buffer, sizeof (tx_buffer));
106
112
int ret = sock.sendto (udp_addr, tx_buffer, sizeof (tx_buffer));
107
113
if (ret >= 0 ) {
108
- iomutex.lock ();
109
- printf (" [ID:%01u][%02u] sent %d bytes - %.*s \n " , id, i, ret, ret, tx_buffer);
110
- iomutex.unlock ();
114
+ TS_PRINTF (" [ID:%01u][%02u] sent %d bytes - %.*s \n " , id, i, ret, ret, tx_buffer);
111
115
} else {
112
- iomutex.lock ();
113
- printf (" [ID:%01u][%02u] Network error %d\n " , id, i, ret);
114
- iomutex.unlock ();
116
+ TS_PRINTF (" [ID:%01u][%02u] Network error %d\n " , id, i, ret);
115
117
continue ;
116
118
}
117
119
118
120
SocketAddress temp_addr;
119
121
ret = sock.recvfrom (&temp_addr, rx_buffer, sizeof (rx_buffer));
120
122
if (ret >= 0 ) {
121
- iomutex.lock ();
122
- printf (" [ID:%01u][%02u] recv %d bytes - %.*s \n " , id, i, ret, ret, tx_buffer);
123
- iomutex.unlock ();
123
+ TS_PRINTF (" [ID:%01u][%02u] recv %d bytes - %.*s \n " , id, i, ret, ret, tx_buffer);
124
124
} else {
125
- iomutex.lock ();
126
- printf (" [ID:%01u][%02u] Network error %d\n " , id, i, ret);
127
- iomutex.unlock ();
125
+ TS_PRINTF (" [ID:%01u][%02u] Network error %d\n " , id, i, ret);
128
126
continue ;
129
127
}
130
128
131
129
if ((temp_addr == udp_addr &&
132
130
ret == sizeof (tx_buffer) &&
133
131
memcmp (rx_buffer, tx_buffer, sizeof (rx_buffer)) == 0 )) {
134
132
success += 1 ;
135
- iomutex.lock ();
136
- printf (" [ID:%01u][%02u] success #%d\n " , id, i, success);
137
- iomutex.unlock ();
133
+ TS_PRINTF (" [ID:%01u][%02u] success #%d\n " , id, i, success);
138
134
continue ;
139
135
}
140
136
@@ -151,9 +147,15 @@ class Echo {
151
147
152
148
result = success == ECHO_LOOPS;
153
149
150
+ if (result) {
151
+ TS_PRINTF (" [ID:%01u] Succeeded all %d times!\n " , id, success);
152
+ } else {
153
+ TS_PRINTF (" [ID:%01u] Only succeeded %d times out of a required %d.\n " , id, success, ECHO_LOOPS);
154
+ }
155
+
154
156
err = sock.close ();
155
- printf (" [ID:%01u] Failed to close socket!\n " , id);
156
157
if (err) {
158
+ TS_PRINTF (" [ID:%01u] Failed to close socket!\n " , id);
157
159
result = false ;
158
160
}
159
161
}
0 commit comments