Cleaned up code. Updated documentation.
This commit is contained in:
parent
7f1c25a73b
commit
c23a77d4cc
@ -95,7 +95,6 @@ namespace fr
|
||||
Http(const Http&)= default;
|
||||
Http &operator=(const Http &)=default;
|
||||
Http &operator=(Http &&)=default;
|
||||
virtual ~Http() = default;
|
||||
|
||||
|
||||
/*!
|
||||
@ -326,7 +325,10 @@ namespace fr
|
||||
* sockets.
|
||||
*
|
||||
* @param socket The socket to send through
|
||||
* @return Status indicating if the send succeeded or not.
|
||||
* @return Status indicating if the send succeeded or not:
|
||||
* 'Success': All good, object still valid.
|
||||
* 'WouldBlock' or 'Timeout': No data received. Object still valid though.
|
||||
* Anything else: Object invalid. Call disconnect().
|
||||
*/
|
||||
Socket::Status receive(Socket *socket) override;
|
||||
|
||||
|
||||
@ -21,7 +21,6 @@ namespace fr
|
||||
HttpRequest(const HttpRequest&)= default;
|
||||
HttpRequest &operator=(const HttpRequest &)=default;
|
||||
HttpRequest &operator=(HttpRequest &&)=default;
|
||||
virtual ~HttpRequest() = default;
|
||||
|
||||
/*!
|
||||
* Parse a HTTP response.
|
||||
|
||||
@ -21,7 +21,6 @@ namespace fr
|
||||
HttpResponse(const HttpResponse&)= default;
|
||||
HttpResponse &operator=(const HttpResponse &)=default;
|
||||
HttpResponse &operator=(HttpResponse &&)=default;
|
||||
virtual ~HttpResponse() = default;
|
||||
|
||||
/*!
|
||||
* Parse a raw request or response from a string
|
||||
|
||||
@ -28,8 +28,6 @@ namespace fr
|
||||
|
||||
}
|
||||
|
||||
virtual ~Packet() = default;
|
||||
|
||||
//Nasty constructor to allow things like Packet{1, 2, 3, "bob"}.
|
||||
template <typename T, typename ...Args>
|
||||
Packet(T const &part, Args &&...args)
|
||||
@ -721,8 +719,10 @@ namespace fr
|
||||
* custom types to be directly received through
|
||||
* sockets.
|
||||
*
|
||||
* @param socket The socket to send through
|
||||
* @return Status indicating if the send succeeded or not.
|
||||
* @return Status indicating if the send succeeded or not:
|
||||
* 'Success': All good, object still valid.
|
||||
* 'WouldBlock' or 'Timeout': No data received. Object still valid though.
|
||||
* Anything else: Object invalid. Call disconnect().
|
||||
*/
|
||||
Socket::Status receive(Socket *socket) override
|
||||
{
|
||||
@ -742,11 +742,14 @@ namespace fr
|
||||
//Now we've got the length, read the rest of the data in
|
||||
if(packet_length + PACKET_HEADER_LENGTH > buffer.size())
|
||||
buffer.resize(packet_length + PACKET_HEADER_LENGTH);
|
||||
status = socket->receive_all(&buffer[PACKET_HEADER_LENGTH], packet_length);
|
||||
if(status != Socket::Status::Success)
|
||||
return status;
|
||||
|
||||
return Socket::Status::Success;
|
||||
do
|
||||
{
|
||||
status = socket->receive_all(&buffer[PACKET_HEADER_LENGTH], packet_length);
|
||||
} while(status == fr::Socket::WouldBlock);
|
||||
if(status == fr::Socket::Timeout)
|
||||
status = fr::Socket::Disconnected;
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -49,7 +49,8 @@ namespace fr
|
||||
* @param data_size The number of bytes to try and receive. Be sure that it's not larger than data.
|
||||
* @param received Will be filled with the number of bytes actually received, might be less than you requested.
|
||||
* @return The status of the operation:
|
||||
* 'WouldBlock' if no data has been received, and the socket is in non-blocking mode or the operation has timed out
|
||||
* 'WouldBlock' if no data has been received and the socket is nonblockins
|
||||
* 'TimedOut' if the socket is blocking and no data was received in time.
|
||||
* 'Disconnected' if the socket has disconnected.
|
||||
* 'SSLError' An SSL error has occurred.
|
||||
* 'Success' All the bytes you wanted have been read
|
||||
|
||||
@ -159,10 +159,10 @@ namespace fr
|
||||
*
|
||||
* @param dest Where to read the data into
|
||||
* @param buffer_size The number of bytes to read
|
||||
* @return Operation status:
|
||||
* 'Disconnected' if the socket disconnected
|
||||
* 'Success' if buffer_size bytes could be read successfully
|
||||
* 'WouldBlock' if the socket is in blocking mode and no data could be read, or if the read timed out before any data was received
|
||||
* @return Status indicating if the send succeeded or not:
|
||||
* 'Success': All good, object still valid.
|
||||
* 'WouldBlock' or 'Timeout': No data received. Object still valid though.
|
||||
* Anything else: Object invalid. Call disconnect().
|
||||
*/
|
||||
Status receive_all(void *dest, size_t buffer_size);
|
||||
|
||||
|
||||
@ -59,7 +59,8 @@ namespace fr
|
||||
* @param received Will be filled with the number of bytes actually received, might be less than you requested if in non-blocking mode/error.
|
||||
* @return The status of the operation:
|
||||
* 'WouldBlock' if no data has been received, and the socket is in non-blocking mode or operation has timed out
|
||||
* 'Disconnected' if the socket has disconnected.
|
||||
* 'Timeout' Socket is in blocking mode and it timed out.
|
||||
* 'Disconnected' if the socket has disconnected. (you must now call disconnect())
|
||||
* 'ReceiveError' A receive error occurred.
|
||||
* 'Success' All the bytes you wanted have been read
|
||||
*/
|
||||
|
||||
@ -32,7 +32,7 @@ namespace fr
|
||||
explicit WebFrame(Opcode type = Text);
|
||||
|
||||
/*!
|
||||
* Get's the received payload data. (Data received).
|
||||
* Get's the receied payload data. (Data received).
|
||||
*
|
||||
* @return The payload
|
||||
*/
|
||||
@ -112,7 +112,10 @@ namespace fr
|
||||
*
|
||||
* @note If the maximum message length is exceeded, then the connection will be closed
|
||||
* @param socket The socket to send through
|
||||
* @return Status indicating if the send succeeded or not.
|
||||
* @return Status indicating if the send succeeded or not:
|
||||
* 'Success': All good, object still valid.
|
||||
* 'WouldBlock' or 'Timeout': No data received. Object still valid though.
|
||||
* Anything else: Object invalid. Call disconnect().
|
||||
*/
|
||||
Socket::Status receive(Socket *socket) override;
|
||||
|
||||
|
||||
10
src/Http.cpp
10
src/Http.cpp
@ -1014,10 +1014,16 @@ namespace fr
|
||||
do
|
||||
{
|
||||
//Receive the request
|
||||
Socket::Status status = socket->receive_raw(recv_buffer, RECV_CHUNK_SIZE, received);
|
||||
auto status = socket->receive_raw(recv_buffer, RECV_CHUNK_SIZE, received);
|
||||
total_received += received;
|
||||
if(status != Socket::Success && !(status == fr::Socket::WouldBlock && total_received != 0))
|
||||
if(status != Socket::Success)
|
||||
{
|
||||
if(total_received == 0)
|
||||
return status;
|
||||
if(status == Socket::WouldBlock)
|
||||
continue;
|
||||
return Socket::Disconnected;
|
||||
}
|
||||
|
||||
//Parse it
|
||||
state = parse(recv_buffer, received);
|
||||
|
||||
@ -34,30 +34,25 @@ namespace fr
|
||||
|
||||
Socket::Status Socket::receive(Sendable &obj)
|
||||
{
|
||||
if(!connected())
|
||||
return Socket::Disconnected;
|
||||
|
||||
return obj.receive(this);
|
||||
}
|
||||
|
||||
Socket::Status Socket::receive_all(void *dest, size_t buffer_size)
|
||||
{
|
||||
if(!connected())
|
||||
return Socket::Disconnected;
|
||||
|
||||
auto bytes_remaining = (int32_t) buffer_size;
|
||||
size_t bytes_read = 0;
|
||||
auto bytes_remaining = (ssize_t) buffer_size;
|
||||
while(bytes_remaining > 0)
|
||||
{
|
||||
size_t received = 0;
|
||||
auto *arr = (char*)dest;
|
||||
Status status = receive_raw(&arr[bytes_read], (size_t)bytes_remaining, received);
|
||||
if(status != fr::Socket::WouldBlock && status != fr::Socket::Success)
|
||||
return status;
|
||||
Status status = receive_raw((char*)dest + (buffer_size - bytes_remaining), (size_t)bytes_remaining, received);
|
||||
bytes_remaining -= received;
|
||||
bytes_read += received;
|
||||
if(status == fr::Socket::WouldBlock && bytes_read == 0)
|
||||
if(status != Socket::Success)
|
||||
{
|
||||
if((ssize_t)buffer_size == bytes_remaining)
|
||||
return status;
|
||||
if(status == Socket::WouldBlock)
|
||||
continue;
|
||||
return Socket::Disconnected;
|
||||
}
|
||||
}
|
||||
|
||||
return Socket::Status::Success;
|
||||
|
||||
@ -119,14 +119,24 @@ namespace fr
|
||||
if(payload_length == 126) //Length is longer than 7 bit, so read 16bit length
|
||||
{
|
||||
uint16_t length;
|
||||
do
|
||||
{
|
||||
status = socket->receive_all(&length, sizeof(length));
|
||||
} while(status == fr::Socket::WouldBlock);
|
||||
if(status == fr::Socket::Timeout)
|
||||
status = fr::Socket::Disconnected;
|
||||
payload_length = ntohs(length);
|
||||
if(status != fr::Socket::Success)
|
||||
return status;
|
||||
}
|
||||
else if(payload_length == 127) //Length is longer than 16 bit, so read 64bit length
|
||||
{
|
||||
do
|
||||
{
|
||||
status = socket->receive_all(&payload_length, sizeof(payload_length));
|
||||
} while(status == fr::Socket::WouldBlock);
|
||||
if(status == fr::Socket::Timeout)
|
||||
status = fr::Socket::Disconnected;
|
||||
payload_length = fr_ntohll(payload_length);
|
||||
if(status != fr::Socket::Success)
|
||||
return status;
|
||||
@ -145,15 +155,25 @@ namespace fr
|
||||
char str_mask_key[4];
|
||||
} mask_union{};
|
||||
if(mask)
|
||||
{
|
||||
do
|
||||
{
|
||||
status = socket->receive_all(&mask_union.mask_key, 4);
|
||||
} while(status == fr::Socket::WouldBlock);
|
||||
if(status == fr::Socket::Timeout)
|
||||
status = fr::Socket::Disconnected;
|
||||
if(status != fr::Socket::Success)
|
||||
return status;
|
||||
}
|
||||
|
||||
//Read payload
|
||||
payload.resize(payload_length, '\0');
|
||||
do
|
||||
{
|
||||
status = socket->receive_all(&payload[0], payload_length);
|
||||
} while(status == fr::Socket::WouldBlock);
|
||||
if(status == fr::Socket::Timeout)
|
||||
status = fr::Socket::Disconnected;
|
||||
if(status != fr::Socket::Success)
|
||||
return status;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user