Remove ProcessResult
This commit is contained in:
@@ -6,12 +6,6 @@
|
||||
// Forward declaration to avoid circular dependency
|
||||
struct Connection;
|
||||
|
||||
enum class ProcessResult {
|
||||
Continue, // Keep connection open, continue processing
|
||||
CloseAfterSend, // Send response then close connection
|
||||
CloseNow // Close connection immediately
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract interface for handling connection data processing.
|
||||
*
|
||||
@@ -32,19 +26,17 @@ public:
|
||||
* @param data Incoming data buffer (may be partial message)
|
||||
* @param conn_ptr Unique pointer to connection - handler can take ownership
|
||||
* by releasing it
|
||||
* @return ProcessResult indicating how to handle the connection
|
||||
*
|
||||
* Implementation should:
|
||||
* - Parse incoming data using arena allocator when needed
|
||||
* - Use conn_ptr->appendMessage() to queue response data
|
||||
* - Return appropriate ProcessResult for connection management
|
||||
* - Handle partial messages and streaming protocols appropriately
|
||||
* - Can take ownership by calling conn_ptr.release() to pass to other threads
|
||||
* - If ownership is taken, handler must call Server::releaseBackToServer()
|
||||
* when done
|
||||
*/
|
||||
virtual ProcessResult process_data(std::string_view data,
|
||||
std::unique_ptr<Connection> &conn_ptr) = 0;
|
||||
virtual void process_data(std::string_view data,
|
||||
std::unique_ptr<Connection> &conn_ptr) = 0;
|
||||
|
||||
/**
|
||||
* Called when a new connection is established.
|
||||
@@ -53,8 +45,6 @@ public:
|
||||
*
|
||||
* Use this for:
|
||||
* - Connection-specific initialization
|
||||
* - Sending greeting messages
|
||||
* - Setting up connection state
|
||||
*/
|
||||
virtual void on_connection_established(Connection &) {}
|
||||
|
||||
@@ -65,8 +55,6 @@ public:
|
||||
*
|
||||
* Use this for:
|
||||
* - Cleanup of connection-specific resources
|
||||
* - Logging connection statistics
|
||||
* - Finalizing protocol state
|
||||
*/
|
||||
virtual void on_connection_closed(Connection &) {}
|
||||
};
|
||||
|
||||
15
src/main.cpp
15
src/main.cpp
@@ -6,10 +6,12 @@
|
||||
#include <csignal>
|
||||
#include <iostream>
|
||||
|
||||
// TODO this should be scoped to a particular Server, and it's definition should
|
||||
// be in server.cpp or connection.cpp
|
||||
std::atomic<int> activeConnections{0};
|
||||
|
||||
// Global server instance for signal handler access
|
||||
Server *g_server = nullptr;
|
||||
static Server *g_server = nullptr;
|
||||
|
||||
void signal_handler(int sig) {
|
||||
if (sig == SIGTERM || sig == SIGINT) {
|
||||
@@ -27,17 +29,10 @@ void signal_handler(int sig) {
|
||||
*/
|
||||
class EchoHandler : public ConnectionHandler {
|
||||
public:
|
||||
ProcessResult process_data(std::string_view data,
|
||||
std::unique_ptr<Connection> &conn_ptr) override {
|
||||
void process_data(std::string_view data,
|
||||
std::unique_ptr<Connection> &conn_ptr) override {
|
||||
// Echo the received data back to the client
|
||||
conn_ptr->appendMessage(data);
|
||||
return ProcessResult::Continue;
|
||||
}
|
||||
|
||||
void on_connection_established(Connection &conn) override {
|
||||
// Could send a welcome message if desired
|
||||
// conn.appendMessage("Welcome to WeaselDB echo server\n");
|
||||
(void)conn; // Suppress unused parameter warning
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -270,17 +270,7 @@ void Server::start_network_threads() {
|
||||
|
||||
// Call handler with unique_ptr - handler can take ownership if
|
||||
// needed
|
||||
ProcessResult result = handler_.process_data(data, conn);
|
||||
switch (result) {
|
||||
case ProcessResult::Continue:
|
||||
break;
|
||||
case ProcessResult::CloseAfterSend:
|
||||
conn->closeAfterSend();
|
||||
break;
|
||||
case ProcessResult::CloseNow:
|
||||
continue; // Connection will be destroyed when unique_ptr goes out
|
||||
// of scope
|
||||
}
|
||||
handler_.process_data(data, conn);
|
||||
|
||||
// If handler took ownership (conn is now null), don't continue
|
||||
// processing
|
||||
@@ -423,4 +413,4 @@ void Server::cleanup_resources() {
|
||||
close(listen_sockfd_);
|
||||
listen_sockfd_ = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user