Skip to content

Commit

Permalink
Merge pull request #13 from Jacquwes/12-create-new-error-type
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacquwes authored Oct 16, 2024
2 parents a14e865 + 950ea5c commit 7b6bfad
Show file tree
Hide file tree
Showing 21 changed files with 211 additions and 182 deletions.
7 changes: 4 additions & 3 deletions client/include/client_connection.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
#include <error.h>
#include <string>
#include <thread>
#include "connection.h"
Expand Down Expand Up @@ -30,16 +31,16 @@ namespace pine

/// @brief Start listening for messages from the server.
/// @return An asynchronous task completed when the connection has been closed.
async_operation<void, std::error_code> listen() const;
async_operation<void> listen() const;

/// @brief Receive an HTTP response from the server.
/// @return An asynchronous task completed when the response has been received.
async_operation<http_response, std::error_code> receive_response() const;
async_operation<http_response> receive_response() const;

/// @brief Send an HTTP request to the server.
/// @param request The request to send.
/// @return An asynchronous task completed when the request has been sent.
async_operation<void, std::error_code> send_request(http_request const& request) const;
async_operation<void> send_request(http_request const& request) const;

private:
std::jthread client_thread;
Expand Down
9 changes: 5 additions & 4 deletions client/src/client_connection.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <coroutine.h>
#include <cstdint>
#include <error.h>
#include <http_request.h>
#include <http_response.h>
#include <string>
Expand Down Expand Up @@ -30,7 +31,7 @@ namespace pine
this->close();
}

async_operation<void, std::error_code>
async_operation<void>
client_connection::listen() const
{
while (true)
Expand All @@ -41,7 +42,7 @@ namespace pine
}
}

async_operation<http_response, std::error_code>
async_operation<http_response>
client_connection::receive_response() const
{
const auto& received_message_result = co_await this->receive_raw_message();
Expand All @@ -54,7 +55,7 @@ namespace pine
co_return response;
}

async_operation<void, std::error_code>
async_operation<void>
client_connection::send_request(http_request const& request) const
{
const auto& request_string = request.to_string();
Expand All @@ -63,6 +64,6 @@ namespace pine
if (!send_result)
co_return send_result.error();

co_return {};
co_return error(error_code::success);
}
}
23 changes: 12 additions & 11 deletions server/include/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <coroutine.h>
#include <cstdint>
#include <error.h>
#include <expected.h>
#include <functional>
#include <memory>
Expand All @@ -24,7 +25,7 @@ namespace pine
explicit server(const char* port = "80");

/// @brief Start listening for connections.
std::expected<void, std::error_code> start();
std::expected<void, pine::error> start();

/// @brief Stop listening for connections.
void stop();
Expand All @@ -33,7 +34,7 @@ namespace pine
/// @param client_id Id of the client to disconnect.
/// @return An asynchronous task completed when the client has been
/// disconnected.
async_operation<void, std::error_code> disconnect_client(
async_operation<void> disconnect_client(
uint64_t const& client_id);

private:
Expand All @@ -42,7 +43,7 @@ namespace pine
/// connection for each client.
/// @return An asynchronous task completed when the server has stopped
/// listening.
std::expected<void, std::error_code> accept_clients();
std::expected<void, pine::error> accept_clients();

std::mutex delete_clients_mutex;
std::mutex mutate_clients_mutex;
Expand All @@ -67,7 +68,7 @@ namespace pine
/// @return A reference to this server.
server&
on_connection_attempt(
std::function<async_operation<void, std::error_code>(server&,
std::function<async_operation<void>(server&,
std::shared_ptr<server_connection> const&)> const& callback
);

Expand All @@ -77,7 +78,7 @@ namespace pine
/// @return A reference to this server.
server&
on_connection_failed(
std::function < async_operation<void, std::error_code>(
std::function < async_operation<void>(
server&,
std::shared_ptr<server_connection> const&)> const& callback
);
Expand All @@ -87,7 +88,7 @@ namespace pine
/// connects to the server.
/// @return A reference to this server.
server&
on_connection(std::function < async_operation<void, std::error_code>(
on_connection(std::function < async_operation<void>(
server&,
std::shared_ptr<server_connection> const&)> const& callback
);
Expand All @@ -97,27 +98,27 @@ namespace pine
/// to accept connections.
/// @return A reference to this server.
server&
on_ready(std::function < async_operation<void, std::error_code>(
on_ready(std::function < async_operation<void>(
server&)> const& callback
);

private:
std::vector<std::function<async_operation<void, std::error_code>(
std::vector<std::function<async_operation<void>(
server&,
std::shared_ptr<server_connection> const&)>
> on_connection_attemps_callbacks;

std::vector<std::function<async_operation<void, std::error_code>(
std::vector<std::function<async_operation<void>(
server&,
std::shared_ptr<server_connection> const&)>
> on_connection_failed_callbacks;

std::vector<std::function<async_operation<void, std::error_code>(
std::vector<std::function<async_operation<void>(
server&,
std::shared_ptr<server_connection> const&)>
> on_connection_callbacks;

std::vector < std::function < async_operation<void, std::error_code>(
std::vector < std::function < async_operation<void>(
server&)>
> on_ready_callbacks;
};
Expand Down
6 changes: 3 additions & 3 deletions server/include/server_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ namespace pine

/// @brief Receive an HTTP request.
/// @return An asynchronous task completed when the request has been received.
async_operation<http_request, std::error_code> receive_request() const;
async_operation<http_request> receive_request() const;

/// @brief Send an HTTP response.
/// @param response The response to send.
/// @return An asynchronous task completed when the response has been sent.
async_operation<void, std::error_code> send_response(http_response const& response) const;
async_operation<void> send_response(http_response const& response) const;

/// @brief Start listening for messages from the client.
/// @return An asynchronous task completed when the connection has been closed.
async_operation<void, std::error_code> start();
async_operation<void> start();

private:
/// @brief Whether the connection is connected.
Expand Down
31 changes: 20 additions & 11 deletions server/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace pine
: port(port)
{}

std::expected<void, std::error_code> server::start()
std::expected<void, error> server::start()
{
if (const auto& init_result = initialize_wsa();
!init_result)
Expand All @@ -31,7 +31,8 @@ namespace pine
return std::make_unexpected(socket_result.error());
this->server_socket = socket_result.value();

if (const auto& bind_result = bind_socket(server_socket, this->address_info);
if (const auto& bind_result = bind_socket(server_socket,
this->address_info);
!bind_result)
return std::make_unexpected(bind_result.error());

Expand Down Expand Up @@ -64,7 +65,7 @@ namespace pine
this->clients.clear();
}

std::expected<void, std::error_code> server::accept_clients()
std::expected<void, error> server::accept_clients()
{
for (const auto& callback : on_ready_callbacks)
{
Expand Down Expand Up @@ -101,7 +102,8 @@ namespace pine
callback(*this, client);
}

if (const auto& start_result = client->start().await_resume(); !start_result)
if (const auto& start_result = client->start().await_resume();
!start_result)
{
this->clients.erase(client->get_id());
}
Expand All @@ -112,27 +114,29 @@ namespace pine
return {};
}

async_operation<void, std::error_code> server::disconnect_client(uint64_t const& client_id)
async_operation<void> server::disconnect_client(uint64_t const& client_id)
{
std::unique_lock lock{ mutate_clients_mutex };

const auto& client = clients.find(client_id);

if (client == clients.end())
{
co_return make_error_code(error::client_not_found);
co_return error(error_code::client_not_found,
"The client was not found.");
}

client->second->close();

clients.erase(client_id);

co_return make_error_code(error::success);
co_return error(error_code::success);
}

server& server::on_connection_attempt(
std::function<
async_operation<void, std::error_code>(server&, std::shared_ptr<server_connection>const&)
async_operation<void>
(server&, std::shared_ptr<server_connection>const&)
> const& callback)
{
on_connection_attemps_callbacks.push_back(callback);
Expand All @@ -141,7 +145,8 @@ namespace pine

server& server::on_connection_failed(
std::function<
async_operation<void, std::error_code>(server&, std::shared_ptr<server_connection>const&)
async_operation<void>
(server&, std::shared_ptr<server_connection>const&)
> const& callback)
{
on_connection_failed_callbacks.push_back(callback);
Expand All @@ -150,14 +155,18 @@ namespace pine

server& server::on_connection(
std::function<
async_operation<void, std::error_code>(server&, std::shared_ptr<server_connection>const&)
async_operation<void>
(server&, std::shared_ptr<server_connection>const&)
> const& callback)
{
on_connection_callbacks.push_back(callback);
return *this;
}

server& server::on_ready(std::function<async_operation<void, std::error_code>(server&)> const& callback)
server& server::on_ready(
std::function<
async_operation<void>
(server&)> const& callback)
{
on_ready_callbacks.push_back(callback);
return *this;
Expand Down
10 changes: 5 additions & 5 deletions server/src/server_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace pine
server_connection::server_connection(SOCKET socket) : connection(socket)
{}

async_operation<http_request, std::error_code>
async_operation<http_request>
pine::server_connection::receive_request() const
{
const auto& receive_message_result = co_await this->receive_raw_message();
Expand All @@ -29,7 +29,7 @@ namespace pine
co_return request_result.value();
}

async_operation<void, std::error_code>
async_operation<void>
server_connection::send_response(http_response const& response) const
{
const std::string& response_string = response.to_string();
Expand All @@ -39,10 +39,10 @@ namespace pine
if (!send_message_result)
co_return send_message_result.error();

co_return make_error_code(error::success);
co_return error(error_code::success);
}

async_operation<void, std::error_code>
async_operation<void>
server_connection::start()
{
this->is_connected = true;
Expand All @@ -58,6 +58,6 @@ namespace pine
}

this->is_connected = false;
co_return make_error_code(error::success);
co_return error(error_code::success);
}
}
1 change: 1 addition & 0 deletions shared/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ target_sources(shared
"include/wsa.h"

"src/connection.cpp"
"src/error.cpp"
"src/http.cpp"
"src/http_request.cpp"
"src/http_response.cpp"
Expand Down
5 changes: 3 additions & 2 deletions shared/include/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <memory>
#include <vector>
#include "coroutine.h"
#include "error.h"
#include "snowflake.h"

#ifdef _WIN32
Expand All @@ -24,13 +25,13 @@ namespace pine

/// @brief Receive a raw message to the connection.
/// @return An asynchronous operation that returns the received bytes.
async_operation<std::string, std::error_code>
async_operation<std::string>
receive_raw_message() const;

/// @brief Send a raw message to the connection.
/// @param raw_message Message to send.
/// @return An asynchronous task completed when the message has been sent.
async_operation<void, std::error_code>
async_operation<void>
send_raw_message(std::string_view raw_message) const;

/// @brief Close the connection.
Expand Down
Loading

0 comments on commit 7b6bfad

Please sign in to comment.