57 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#pragma once
 | 
						|
 | 
						|
#include <cstdint>
 | 
						|
#include <string>
 | 
						|
#include <vector>
 | 
						|
 | 
						|
namespace net_protocol {
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief Defines the opcodes for client-server communication.
 | 
						|
 *
 | 
						|
 * Each message payload begins with one of these single-byte opcodes to identify
 | 
						|
 * the purpose of the message.
 | 
						|
 */
 | 
						|
enum class Opcode : uint8_t {
 | 
						|
  /* Client -> Server messages. */
 | 
						|
  C2S_CREATE_ACCOUNT,
 | 
						|
  C2S_LOGIN,
 | 
						|
  C2S_CREATE_SESSION,
 | 
						|
  C2S_COMMAND,    /* args: [session_id, command] */
 | 
						|
  C2S_WRITE_FILE, /* args: [session_id, path, content] */
 | 
						|
  C2S_READ_FILE,  /* args: [session_id, path] */
 | 
						|
  C2S_BUILD_FILE, /* args: [session_id, path, content] */
 | 
						|
 | 
						|
  /* Server -> Client messages. */
 | 
						|
  S2C_CREATE_ACCOUNT_SUCCESS,
 | 
						|
  S2C_CREATE_ACCOUNT_FAIL,
 | 
						|
  S2C_LOGIN_SUCCESS,
 | 
						|
  S2C_LOGIN_FAIL,
 | 
						|
  S2C_SESSION_CREATED,    /* args: [session_id] */
 | 
						|
  S2C_COMMAND_RESPONSE,   /* args: [session_id, response] */
 | 
						|
  S2C_FILE_DATA,          /* args: [session_id, path, content] */
 | 
						|
  S2C_DISCONNECT,         /* args: [session_id] */
 | 
						|
  S2C_CLOSE_WINDOW,       /* args: [session_id] */
 | 
						|
};
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief Builds a raw message payload from an opcode and arguments.
 | 
						|
 * @param opcode The message type.
 | 
						|
 * @param args Vector of string arguments to be included in the message.
 | 
						|
 * @return A payload string to be sent over the network.
 | 
						|
 */
 | 
						|
std::string build_message(Opcode opcode, const std::vector<std::string>& args = {});
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief Parse a raw message payload into its opcode and arguments.
 | 
						|
 * @param payload The raw payload received from the netwrok.
 | 
						|
 * @param out_opcode The output variable for the parsed opcode.
 | 
						|
 * @param out_args the output vector for the parsed string arguments.
 | 
						|
 *
 | 
						|
 * The format is: [1-byte Opcode][arg1]\0[arg2]\0...
 | 
						|
 */
 | 
						|
void parse_message(const std::string& payload, Opcode& out_opcode,
 | 
						|
                   std::vector<std::string>& out_args);
 | 
						|
 | 
						|
}; /* namespace net_protocol. */
 |