Use std::expected for socket return types, enum class sock_error
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <optional>
|
||||
|
||||
namespace w6300 {
|
||||
|
||||
@@ -152,24 +154,23 @@ void init();
|
||||
bool check();
|
||||
void init_net(net_info info);
|
||||
|
||||
constexpr int16_t SOCK_OK = 1;
|
||||
constexpr int16_t SOCK_BUSY = 0;
|
||||
constexpr int16_t SOCK_FATAL = -1000;
|
||||
constexpr int16_t SOCK_ERROR = 0;
|
||||
constexpr int16_t SOCKERR_SOCKNUM = SOCK_ERROR - 1;
|
||||
constexpr int16_t SOCKERR_SOCKOPT = SOCK_ERROR - 2;
|
||||
constexpr int16_t SOCKERR_SOCKINIT = SOCK_ERROR - 3;
|
||||
constexpr int16_t SOCKERR_SOCKCLOSED = SOCK_ERROR - 4;
|
||||
constexpr int16_t SOCKERR_SOCKMODE = SOCK_ERROR - 5;
|
||||
constexpr int16_t SOCKERR_SOCKFLAG = SOCK_ERROR - 6;
|
||||
constexpr int16_t SOCKERR_SOCKSTATUS = SOCK_ERROR - 7;
|
||||
constexpr int16_t SOCKERR_ARG = SOCK_ERROR - 10;
|
||||
constexpr int16_t SOCKERR_PORTZERO = SOCK_ERROR - 11;
|
||||
constexpr int16_t SOCKERR_IPINVALID = SOCK_ERROR - 12;
|
||||
constexpr int16_t SOCKERR_TIMEOUT = SOCK_ERROR - 13;
|
||||
constexpr int16_t SOCKERR_DATALEN = SOCK_ERROR - 14;
|
||||
constexpr int16_t SOCKERR_BUFFER = SOCK_ERROR - 15;
|
||||
constexpr int16_t SOCKFATAL_PACKLEN = SOCK_FATAL - 1;
|
||||
enum class sock_error : int16_t {
|
||||
busy = 0,
|
||||
sock_num = -1,
|
||||
sock_opt = -2,
|
||||
sock_init = -3,
|
||||
sock_closed = -4,
|
||||
sock_mode = -5,
|
||||
sock_flag = -6,
|
||||
sock_status = -7,
|
||||
arg = -10,
|
||||
port_zero = -11,
|
||||
ip_invalid = -12,
|
||||
timeout = -13,
|
||||
data_len = -14,
|
||||
buffer = -15,
|
||||
fatal_packlen = -1001,
|
||||
};
|
||||
|
||||
enum class protocol : uint8_t {
|
||||
tcp = 0x01,
|
||||
@@ -240,12 +241,12 @@ enum class sock_io_mode : uint8_t {
|
||||
nonblock = 1,
|
||||
};
|
||||
|
||||
int8_t open_socket(socket_id sn, protocol proto, port_num port, sock_flag flag);
|
||||
int8_t close(socket_id sn);
|
||||
int8_t listen(socket_id sn);
|
||||
int8_t disconnect(socket_id sn);
|
||||
int32_t send(socket_id sn, uint8_t* buf, uint16_t len);
|
||||
int32_t recv(socket_id sn, uint8_t* buf, uint16_t len);
|
||||
std::expected<socket_id, sock_error> open_socket(socket_id sn, protocol proto, port_num port, sock_flag flag);
|
||||
std::expected<void, sock_error> close(socket_id sn);
|
||||
std::expected<void, sock_error> listen(socket_id sn);
|
||||
std::expected<void, sock_error> disconnect(socket_id sn);
|
||||
std::expected<uint16_t, sock_error> send(socket_id sn, uint8_t* buf, uint16_t len);
|
||||
std::expected<uint16_t, sock_error> recv(socket_id sn, uint8_t* buf, uint16_t len);
|
||||
|
||||
enum sockint_kind {
|
||||
SIK_CONNECTED = (1 << 0),
|
||||
@@ -287,13 +288,13 @@ enum sockopt_type {
|
||||
SO_PACKINFO
|
||||
};
|
||||
|
||||
int8_t ctl_socket(socket_id sn, sock_ctl type, void* arg);
|
||||
int8_t set_sockopt(socket_id sn, sockopt_type type, void* arg);
|
||||
int8_t get_sockopt(socket_id sn, sockopt_type type, void* arg);
|
||||
int16_t peek_socket_msg(socket_id sn, uint8_t* submsg, uint16_t subsize);
|
||||
std::expected<void, sock_error> ctl_socket(socket_id sn, sock_ctl type, void* arg);
|
||||
std::expected<void, sock_error> set_sockopt(socket_id sn, sockopt_type type, void* arg);
|
||||
std::expected<void, sock_error> get_sockopt(socket_id sn, sockopt_type type, void* arg);
|
||||
std::optional<uint16_t> peek_socket_msg(socket_id sn, uint8_t* submsg, uint16_t subsize);
|
||||
|
||||
int8_t connect(socket_id sn, uint8_t* addr, port_num port, uint8_t addrlen);
|
||||
int32_t sendto(socket_id sn, uint8_t* buf, uint16_t len, uint8_t* addr, port_num port, uint8_t addrlen);
|
||||
int32_t recvfrom(socket_id sn, uint8_t* buf, uint16_t len, uint8_t* addr, port_num* port, uint8_t* addrlen);
|
||||
std::expected<void, sock_error> connect(socket_id sn, uint8_t* addr, port_num port, uint8_t addrlen);
|
||||
std::expected<uint16_t, sock_error> sendto(socket_id sn, uint8_t* buf, uint16_t len, uint8_t* addr, port_num port, uint8_t addrlen);
|
||||
std::expected<uint16_t, sock_error> recvfrom(socket_id sn, uint8_t* buf, uint16_t len, uint8_t* addr, port_num* port, uint8_t* addrlen);
|
||||
|
||||
} // namespace w6300
|
||||
|
||||
Reference in New Issue
Block a user