uint32_t MetaServerPacket::getIntData(unsigned int offset) const { // initial int to arbitrary value for potential debugging purposes uint32_t tmpint = 222; unpack_uint32(&tmpint, m_readPtr + offset ); return tmpint; }
virtual void unpack(const std::vector<uint8_t>& bytes) { int32_t unpackedInt; uint32_t unpackedUint; float unpackedFloat; unpack_int32(&(bytes[0]), unpackedInt); unpack_uint32(&(bytes[4]), unpackedUint); unpack_float32(&(bytes[8]), unpackedFloat); AssertEquals(testInt, unpackedInt); AssertEquals(testUint, unpackedUint); AssertEquals(testFloat, unpackedFloat); }
/* Attempt to read data from the named pipe on strict mode. * Note: For now it only reads on new connections, i.e., onopen. * * If there's less data than requested, 0 is returned * If the thread is done, 1 is returned */ int read_fifo (GWSReader * gwsreader, fd_set rfds, fd_set wfds, void (*f) (int)) { WSPacket **pa = &gwsreader->packet; char *ptr; int bytes = 0, readh = 0, need = 0, fd = gwsreader->fd, max = 0; uint32_t listener = 0, type = 0, size = 0; FD_ZERO (&rfds); FD_ZERO (&wfds); /* self-pipe trick to stop the event loop */ FD_SET (gwsreader->self_pipe[0], &rfds); /* fifo */ FD_SET (fd, &rfds); max = MAX (fd, gwsreader->self_pipe[0]); if (select (max + 1, &rfds, &wfds, NULL, NULL) == -1) { switch (errno) { case EINTR: break; default: FATAL ("Unable to select: %s.", strerror (errno)); } } /* handle self-pipe trick */ if (FD_ISSET (gwsreader->self_pipe[0], &rfds)) return 1; if (!FD_ISSET (fd, &rfds)) { LOG (("No file descriptor set on read_message()\n")); return 0; } readh = gwsreader->hlen; /* read from header so far */ need = HDR_SIZE - readh; /* need to read */ if (need > 0) { if ((bytes = ws_read_fifo (fd, gwsreader->hdr, &gwsreader->hlen, readh, need)) < 0) return 0; if (bytes != need) return 0; } /* unpack size, and type */ ptr = gwsreader->hdr; ptr += unpack_uint32 (ptr, &listener); ptr += unpack_uint32 (ptr, &type); ptr += unpack_uint32 (ptr, &size); if ((*pa) == NULL) { (*pa) = xcalloc (1, sizeof (WSPacket)); (*pa)->type = type; (*pa)->size = size; (*pa)->data = xcalloc (size, sizeof (char)); } readh = (*pa)->len; /* read from payload so far */ need = (*pa)->size - readh; /* need to read */ if (need > 0) { if ((bytes = ws_read_fifo (fd, (*pa)->data, &(*pa)->len, readh, need)) < 0) return 0; if (bytes != need) return 0; } clear_fifo_packet (gwsreader); /* fast forward JSON data to the given client */ (*f) (listener); return 0; }
void metaserver_keepalive(int sockfd, const SA *servaddr, socklen_t servlen) { char mesg[MAXLINE]; char *mesg_ptr; uint32_t handshake=0, command=0; SA addr; socklen_t addrlen; unsigned int packet_size; packet_size = 0; mesg_ptr = pack_uint32(SKEEP_ALIVE, mesg, &packet_size); Sendto(sockfd, mesg, packet_size, 0, servaddr, servlen); addrlen = sizeof(addr); Recvfrom(sockfd, mesg, MAXLINE, 0, &addr, &addrlen); mesg_ptr = unpack_uint32(&command, mesg); if(command == HANDSHAKE) { mesg_ptr = unpack_uint32(&handshake, mesg_ptr); printf("Server contacted successfully.\n"); packet_size = 0; mesg_ptr = pack_uint32(SERVERSHAKE, mesg, &packet_size); mesg_ptr = pack_uint32(handshake, mesg_ptr, &packet_size); Sendto(sockfd, mesg, packet_size, 0, servaddr, servlen); } }