Skip to content

Commit

Permalink
refactor, fix: removed constexpr for http_version, moved send to std:…
Browse files Browse the repository at this point in the history
…:string_view, removed spaces
  • Loading branch information
Jaskowicz1 committed Jul 24, 2024
1 parent 1aa25dc commit 76ddba8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion include/dpp/httpsclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

namespace dpp {

static inline constexpr std::string http_version = "DiscordBot (https://github.com/brainboxdotcc/DPP, "
static inline const std::string http_version = "DiscordBot (https://github.com/brainboxdotcc/DPP, "
+ to_hex(DPP_VERSION_MAJOR, false) + "."
+ to_hex(DPP_VERSION_MINOR, false) + "."
+ to_hex(DPP_VERSION_PATCH, false) + ")";
Expand Down
2 changes: 1 addition & 1 deletion include/dpp/sslclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class DPP_EXPORT ssl_client
* @param data Data to be written to the buffer
* @note The data may not be written immediately and may be written at a later time to the socket.
*/
virtual void write(const std::string &data);
virtual void write(const std::string_view data);

/**
* @brief Close socket connection
Expand Down
38 changes: 19 additions & 19 deletions include/dpp/wsclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum websocket_protocol_t : uint8_t {
* @brief JSON data, text, UTF-8 character set
*/
ws_json = 0,

/**
* @brief Erlang Term Format (ETF) binary protocol
*/
Expand Down Expand Up @@ -68,32 +68,32 @@ enum ws_opcode : uint8_t {
/**
* @brief Continuation.
*/
OP_CONTINUATION = 0x00,
OP_CONTINUATION = 0x00,

/**
* @brief Text frame.
*/
OP_TEXT = 0x01,
OP_TEXT = 0x01,

/**
* @brief Binary frame.
*/
OP_BINARY = 0x02,
OP_BINARY = 0x02,

/**
* @brief Close notification with close code.
*/
OP_CLOSE = 0x08,
OP_CLOSE = 0x08,

/**
* @brief Low level ping.
*/
OP_PING = 0x09,
OP_PING = 0x09,

/**
* @brief Low level pong.
*/
OP_PONG = 0x0a
OP_PONG = 0x0a
};

/**
Expand Down Expand Up @@ -130,7 +130,7 @@ class DPP_EXPORT websocket_client : public ssl_client {
* @param buffer The buffer to operate on. Will modify the string removing completed items from the head of the queue
* @return true if a complete header has been received
*/
bool parseheader(std::string &buffer);
bool parseheader(std::string& buffer);

/**
* @brief Unpack a frame and pass completed frames up the stack.
Expand All @@ -139,7 +139,7 @@ class DPP_EXPORT websocket_client : public ssl_client {
* @param first True if is the first element (reserved for future use)
* @return true if a complete frame has been received
*/
bool unpack(std::string &buffer, uint32_t offset, bool first = true);
bool unpack(std::string& buffer, uint32_t offset, bool first = true);

/**
* @brief Fill a header for outbound messages
Expand All @@ -154,7 +154,7 @@ class DPP_EXPORT websocket_client : public ssl_client {
* @brief Handle ping requests.
* @param payload The ping payload, to be returned as-is for a pong
*/
void handle_ping(const std::string &payload);
void handle_ping(const std::string& payload);

protected:

Expand All @@ -167,7 +167,7 @@ class DPP_EXPORT websocket_client : public ssl_client {
* @brief Get websocket state
* @return websocket state
*/
ws_state get_state();
ws_state get_state() const;

public:

Expand All @@ -180,41 +180,41 @@ class DPP_EXPORT websocket_client : public ssl_client {
* @note Voice websockets only support OP_TEXT, and other websockets must be
* OP_BINARY if you are going to send ETF.
*/
websocket_client(const std::string &hostname, const std::string &port = "443", const std::string &urlpath = "", ws_opcode opcode = OP_BINARY);
websocket_client(const std::string& hostname, const std::string& port = "443", const std::string& urlpath = "", ws_opcode opcode = OP_BINARY);

/**
* @brief Destroy the websocket client object
*/
virtual ~websocket_client() = default;
virtual ~websocket_client() = default;

/**
* @brief Write to websocket. Encapsulates data in frames if the status is CONNECTED.
* @param data The data to send.
*/
virtual void write(const std::string &data);
virtual void write(const std::string_view data);

/**
* @brief Processes incoming frames from the SSL socket input buffer.
* @param buffer The buffer contents. Can modify this value removing the head elements when processed.
*/
virtual bool handle_buffer(std::string &buffer);
virtual bool handle_buffer(std::string& buffer);

/**
* @brief Close websocket
*/
virtual void close();
virtual void close();

/**
* @brief Receives raw frame content only without headers
*
*
* @param buffer The buffer contents
* @return True if the frame was successfully handled. False if no valid frame is in the buffer.
*/
virtual bool handle_frame(const std::string &buffer);
virtual bool handle_frame(const std::string& buffer);

/**
* @brief Called upon error frame.
*
*
* @param errorcode The error code from the websocket server
*/
virtual void error(uint32_t errorcode);
Expand Down
2 changes: 1 addition & 1 deletion src/dpp/sslclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ void ssl_client::connect()
}
}

void ssl_client::write(const std::string &data)
void ssl_client::write(const std::string_view data)
{
/* If we are in nonblocking mode, append to the buffer,
* otherwise just use SSL_write directly. The only time we
Expand Down
19 changes: 10 additions & 9 deletions src/dpp/wsclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ constexpr size_t WS_MAX_PAYLOAD_LENGTH_SMALL = 125;
constexpr size_t WS_MAX_PAYLOAD_LENGTH_LARGE = 65535;
constexpr size_t MAXHEADERSIZE = sizeof(uint64_t) + 2;

websocket_client::websocket_client(const std::string &hostname, const std::string &port, const std::string &urlpath, ws_opcode opcode)
websocket_client::websocket_client(const std::string& hostname, const std::string& port, const std::string& urlpath, ws_opcode opcode)
: ssl_client(hostname, port),
state(HTTP_HEADERS),
path(urlpath),
Expand Down Expand Up @@ -73,7 +73,7 @@ void websocket_client::connect()
);
}

bool websocket_client::handle_frame(const std::string &buffer)
bool websocket_client::handle_frame(const std::string& buffer)
{
/* This is a stub for classes that derive the websocket client */
return true;
Expand Down Expand Up @@ -111,7 +111,7 @@ size_t websocket_client::fill_header(unsigned char* outbuf, size_t sendlength, w
}


void websocket_client::write(const std::string &data)
void websocket_client::write(const std::string_view data)
{
if (state == HTTP_HEADERS) {
/* Simple write */
Expand All @@ -125,7 +125,7 @@ void websocket_client::write(const std::string &data)
}
}

bool websocket_client::handle_buffer(std::string &buffer)
bool websocket_client::handle_buffer(std::string& buffer)
{
if (state == HTTP_HEADERS) {
/* We can expect Discord to end all packets with this.
Expand Down Expand Up @@ -174,19 +174,19 @@ bool websocket_client::handle_buffer(std::string &buffer)
return false;
}
} else if (state == CONNECTED) {
/* Process packets until we can't */
while (this->parseheader(buffer));
/* Process packets until we can't (buffer will erase data until parseheader returns false) */
while (this->parseheader(buffer)){}
}

return true;
}

ws_state websocket_client::get_state()
ws_state websocket_client::get_state() const
{
return this->state;
}

bool websocket_client::parseheader(std::string &data)
bool websocket_client::parseheader(std::string& data)
{
if (data.size() < 4) {
/* Not enough data to form a frame yet */
Expand Down Expand Up @@ -244,9 +244,10 @@ bool websocket_client::parseheader(std::string &data)
return false;
}

/* If we received a ping, we need to handle it. */
if ((opcode & ~WS_FINBIT) == OP_PING) {
handle_ping(data.substr(payloadstartoffset, len));
} else if ((opcode & ~WS_FINBIT) != OP_PONG) {
} else if ((opcode & ~WS_FINBIT) != OP_PONG) { /* Otherwise, handle everything else apart from a PONG. */
/* Pass this frame to the deriving class */
this->handle_frame(data.substr(payloadstartoffset, len));
}
Expand Down

0 comments on commit 76ddba8

Please sign in to comment.