| 183 | } |
| 184 | |
| 185 | std::shared_ptr<LemonMessage> MessageClient::PollSync(){ |
| 186 | retry: |
| 187 | if(queue.size() > 0){ |
| 188 | auto element = queue.front(); |
| 189 | queue.pop_front(); |
| 190 | return element; |
| 191 | } |
| 192 | |
| 193 | LemonMessage msg; |
| 194 | |
| 195 | ssize_t len = recv(sock.fd, &msg, sizeof(LemonMessage), 0); |
| 196 | |
| 197 | if(len < (ssize_t)sizeof(LemonMessage)){ |
| 198 | //printf("invalid length: %d\n", len); |
| 199 | |
| 200 | return std::shared_ptr<LemonMessage>(nullptr); |
| 201 | } |
| 202 | |
| 203 | if(msg.magic != LEMON_MESSAGE_MAGIC){ |
| 204 | printf("Invalid magic: %x, discarding data.\n", msg.magic); |
| 205 | while(msg.magic != LEMON_MESSAGE_MAGIC && len >= static_cast<ssize_t>(sizeof(msg))){ |
| 206 | len = recv(sock.fd, &msg, sizeof(msg), MSG_DONTWAIT); // Discard everything until we find a message |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if(msg.magic != LEMON_MESSAGE_MAGIC){ |
| 211 | printf("invalid magic: %x\n", msg.magic); |
| 212 | |
| 213 | return std::shared_ptr<LemonMessage>(nullptr); |
| 214 | } |
| 215 | |
| 216 | std::shared_ptr<LemonMessage> newMsg((LemonMessage*)malloc(sizeof(LemonMessage) + msg.length)); |
| 217 | *newMsg = msg; |
| 218 | |
| 219 | len = recv(sock.fd, newMsg->data, msg.length, 0); |
| 220 | |
| 221 | if(len < msg.length){ |
| 222 | printf("Warning: invalid message length %u. Only read %ld bytes\n", msg.length, len); |
| 223 | |
| 224 | return std::shared_ptr<LemonMessage>(nullptr); |
| 225 | } |
| 226 | |
| 227 | queue.push_back(newMsg); |
| 228 | |
| 229 | goto retry; |
| 230 | } |
| 231 | |
| 232 | void MessageClient::Wait(){ |
| 233 | char c; |
nothing calls this directly
no test coverage detected