Esempio n. 1
0
std::size_t SampleTestClient::recv_data(tcp::socket& socket, int t_length) 
{
    ptime       t1,t2;
    double      tdiff=0;
    double      last_status = 0;

    std::size_t h_recvd=0;
    std::size_t ret=0;

    boost::array<char, DATASIZE>    data;
    boost::system::error_code       error;

    t1 = get_pts();
    t2 = t1;
    h_recvd = 0;

    while ( get_diff(t1,t2) < t_length ) {
        ret = socket.receive(boost::asio::buffer(data), 0, error);
        if ( ret == 0 ) {
            if ( error && error != boost::asio::error::eof) {
                std::cout << error.message() << std::endl;
            }
            break;
        }
        h_recvd += ret;
        t2 = get_pts();
        tdiff = get_diff(t1, t2);
        if ( last_status + 1 < tdiff ) {
            status(tdiff, h_recvd);
            last_status = tdiff;
        }
    }
    status(tdiff, h_recvd);
    return h_recvd;
}
/**
 * Parse filepath URL from incoming HTTP request
 */
std::string HttpServer::parse_request(tcp::socket &socket, boost::system::error_code &error_code)
{
    std::regex url_path_regex("(\\/([^\\s]+)|\\/)");
    std::smatch url_path_match;

    std::vector<char> request_buffer(1024);
    socket.receive(boost::asio::buffer(request_buffer), {}, error_code);
    std::string request_str(request_buffer.data());
    std::regex_search(request_str, url_path_match, url_path_regex);

    return url_path_match[0];
}
Esempio n. 3
0
int SampleTestClient::recv_value(tcp::socket& socket, uint32_t *value) {
    boost::system::error_code       error;
    uint32_t                        n_value=0;
    int                             ret = 0;
    ret = socket.receive(boost::asio::buffer(&n_value, sizeof(n_value)), 0, error);
    if ( ret != sizeof(uint32_t) ) {
        if ( error && error != boost::asio::error::eof) {
            std::cout << error.message() << std::endl;
        }
        return -1;
    }
    *value = ntohl(n_value);
    return OK;
}
Esempio n. 4
0
int SampleTestClient::sure_recv(tcp::socket& socket, void *data, std::size_t size) {

    boost::system::error_code       error;
    std::size_t                     ret = 0;

    ret = socket.receive(boost::asio::buffer(data, size), 0, error);
    if ( ret != size ) {
        if ( error && error != boost::asio::error::eof) {
            std::cout << error.message() << std::endl;
        }
        return -1;
    }
    return OK;
}
Esempio n. 5
0
boost::shared_ptr<NetPacket>
receiveMessage(tcp::socket &socket)
{
	boost::shared_ptr<NetPacket> tmpPacket;

	do {
		// This is necessary, because we use TCP.
		// Packets may be received in multiple chunks or
		// several packets may be received at once.
		if (recBufPos >= NET_HEADER_SIZE) {
			// Read the size of the packet (first 4 bytes in network byte order).
			uint32_t nativeVal;
			memcpy(&nativeVal, recBuf.c_array(), sizeof(uint32_t));
			size_t packetSize = ntohl(nativeVal);
			if (packetSize > MAX_PACKET_SIZE) {
				recBufPos = 0;
				cout << "Packet too large" << endl;
				return boost::shared_ptr<NetPacket>();
			} else if (recBufPos >= packetSize + NET_HEADER_SIZE) {
				try {
					tmpPacket = NetPacket::Create(&recBuf.c_array()[NET_HEADER_SIZE], packetSize);
					if (tmpPacket) {
						recBufPos -= (packetSize + NET_HEADER_SIZE);
						if (recBufPos) {
							memmove(recBuf.c_array(), recBuf.c_array() + packetSize + NET_HEADER_SIZE, recBufPos);
						}
					}
				} catch (const exception &) {
					// Reset buffer on error.
					recBufPos = 0;
					cout << "Packet creation failed" << endl;
					return boost::shared_ptr<NetPacket>();
				}
			}
		}

		if (!tmpPacket) {
			recBufPos += socket.receive(boost::asio::buffer(recBuf.c_array() + recBufPos, BUF_SIZE - recBufPos));
			if (recBufPos == 0) {
				cout << "Receive failed" << endl;
				return boost::shared_ptr<NetPacket>();
			}
		}
	} while (!tmpPacket);

	return tmpPacket;
}