|
| 1 | +/* |
| 2 | + * ----------------------------------------------------------------------------- |
| 3 | + * Project: Fossil Logic |
| 4 | + * |
| 5 | + * This file is part of the Fossil Logic project, which aims to develop high- |
| 6 | + * performance, cross-platform applications and libraries. The code contained |
| 7 | + * herein is subject to the terms and conditions defined in the project license. |
| 8 | + * |
| 9 | + * Author: Michael Gene Brockus (Dreamer) |
| 10 | + * |
| 11 | + * Copyright (C) 2024 Fossil Logic. All rights reserved. |
| 12 | + * ----------------------------------------------------------------------------- |
| 13 | + */ |
| 14 | +#ifndef FOSSIL_IO_NETWORK_H |
| 15 | +#define FOSSIL_IO_NETWORK_H |
| 16 | + |
| 17 | +#include <stdint.h> |
| 18 | + |
| 19 | +#ifdef _WIN32 |
| 20 | + #include <winsock2.h> |
| 21 | + #include <ws2tcpip.h> |
| 22 | + typedef SOCKET fossil_io_socket_t; |
| 23 | + #define FOSSIL_IO_INVALID_SOCKET INVALID_SOCKET |
| 24 | +#else |
| 25 | + #include <sys/types.h> |
| 26 | + #include <sys/socket.h> |
| 27 | + #include <netinet/in.h> |
| 28 | + #include <arpa/inet.h> |
| 29 | + #include <unistd.h> |
| 30 | + #define closesocket close |
| 31 | + typedef int fossil_io_socket_t; |
| 32 | + #define FOSSIL_IO_INVALID_SOCKET (-1) |
| 33 | +#endif |
| 34 | + |
| 35 | +#ifdef __cplusplus |
| 36 | +extern "C" { |
| 37 | +#endif |
| 38 | + |
| 39 | +/** |
| 40 | + * Initialize the network stack (needed for Windows). |
| 41 | + * Returns 0 on success, non-zero on failure. |
| 42 | + */ |
| 43 | +int fossil_io_network_create(void); |
| 44 | + |
| 45 | +/** |
| 46 | + * Clean up network stack (needed for Windows). |
| 47 | + */ |
| 48 | +void fossil_io_network_destroy(void); |
| 49 | + |
| 50 | +/** |
| 51 | + * Create a new TCP socket. |
| 52 | + * Returns a valid socket on success or FOSSIL_IO_INVALID_SOCKET on failure. |
| 53 | + */ |
| 54 | +fossil_io_socket_t fossil_io_network_create_socket(void); |
| 55 | + |
| 56 | +/** |
| 57 | + * Bind a socket to a specific port (IPv4/IPv6). |
| 58 | + * Returns 0 on success, -1 on failure. |
| 59 | + */ |
| 60 | +int fossil_io_network_bind(fossil_io_socket_t sock, const char *ip, uint16_t port); |
| 61 | + |
| 62 | +/** |
| 63 | + * Listen for incoming connections. |
| 64 | + * Returns 0 on success, -1 on failure. |
| 65 | + */ |
| 66 | +int fossil_io_network_listen(fossil_io_socket_t sock, int backlog); |
| 67 | + |
| 68 | +/** |
| 69 | + * Accept a new connection. |
| 70 | + * Returns a valid socket on success, or FOSSIL_IO_INVALID_SOCKET on failure. |
| 71 | + */ |
| 72 | +fossil_io_socket_t fossil_io_network_accept(fossil_io_socket_t sock, char *client_ip, uint16_t *client_port); |
| 73 | + |
| 74 | +/** |
| 75 | + * Connect to a remote server. |
| 76 | + * Returns 0 on success, -1 on failure. |
| 77 | + */ |
| 78 | +int fossil_io_network_connect(fossil_io_socket_t sock, const char *ip, uint16_t port); |
| 79 | + |
| 80 | +/** |
| 81 | + * Send data over a socket. |
| 82 | + * Returns the number of bytes sent, or -1 on failure. |
| 83 | + */ |
| 84 | +int fossil_io_network_send(fossil_io_socket_t sock, const void *data, size_t len); |
| 85 | + |
| 86 | +/** |
| 87 | + * Receive data from a socket. |
| 88 | + * Returns the number of bytes received, or -1 on failure. |
| 89 | + */ |
| 90 | +int fossil_io_network_receive(fossil_io_socket_t sock, void *buffer, size_t len); |
| 91 | + |
| 92 | +/** |
| 93 | + * Close a socket. |
| 94 | + */ |
| 95 | +void fossil_io_network_close(fossil_io_socket_t sock); |
| 96 | + |
| 97 | +#ifdef __cplusplus |
| 98 | +} |
| 99 | +/** |
| 100 | + * C++ wrapper for the output functions. |
| 101 | + */ |
| 102 | +namespace fossil { |
| 103 | + /** |
| 104 | + * Namespace for input/output operations. |
| 105 | + */ |
| 106 | + namespace io { |
| 107 | + /** |
| 108 | + * Class for network operations. |
| 109 | + */ |
| 110 | + class Network { |
| 111 | + public: |
| 112 | + /** |
| 113 | + * Initialize the network stack (needed for Windows). |
| 114 | + * Returns 0 on success, non-zero on failure. |
| 115 | + */ |
| 116 | + static int init(void) { |
| 117 | + return fossil_io_network_create(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Clean up network stack (needed for Windows). |
| 122 | + */ |
| 123 | + static void cleanup(void) { |
| 124 | + fossil_io_network_destroy(); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Create a new TCP socket. |
| 129 | + * Returns a valid socket on success or FOSSIL_IO_INVALID_SOCKET on failure. |
| 130 | + */ |
| 131 | + static fossil_io_socket_t create_socket(void) { |
| 132 | + return fossil_io_network_create_socket(); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Bind a socket to a specific port (IPv4/IPv6). |
| 137 | + * Returns 0 on success, -1 on failure. |
| 138 | + */ |
| 139 | + static int bind(fossil_io_socket_t sock, const char *ip, uint16_t port) { |
| 140 | + return fossil_io_network_bind(sock, ip, port); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Listen for incoming connections. |
| 145 | + * Returns 0 on success, -1 on failure. |
| 146 | + */ |
| 147 | + static int listen(fossil_io_socket_t sock, int backlog) { |
| 148 | + return fossil_io_network_listen(sock, backlog); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Accept a new connection. |
| 153 | + * Returns a valid socket on success, or FOSSIL_IO_INVALID_SOCKET on failure. |
| 154 | + */ |
| 155 | + static fossil_io_socket_t accept(fossil_io_socket_t sock, char *client_ip, uint16_t *client_port) { |
| 156 | + return fossil_io_network_accept(sock, client_ip, client_port); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Connect to a remote server. |
| 161 | + * Returns 0 on success, -1 on failure. |
| 162 | + */ |
| 163 | + static int connect(fossil_io_socket_t sock, const char *ip, uint16_t port) { |
| 164 | + return fossil_io_network_connect(sock, ip, port); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Send data over a socket. |
| 169 | + * Returns the number of bytes sent, or -1 on failure. |
| 170 | + */ |
| 171 | + static int send(fossil_io_socket_t sock, const void *data, size_t len) { |
| 172 | + return fossil_io_network_send(sock, data, len); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Receive data from a socket. |
| 177 | + * Returns the number of bytes received, or -1 on failure. |
| 178 | + */ |
| 179 | + static int receive(fossil_io_socket_t sock, void *buffer, size_t len) { |
| 180 | + return fossil_io_network_receive(sock, buffer, len); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Close a socket. |
| 185 | + */ |
| 186 | + static void close(fossil_io_socket_t sock) { |
| 187 | + fossil_io_network_close(sock); |
| 188 | + } |
| 189 | + |
| 190 | + }; |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +#endif |
| 195 | + |
| 196 | +#endif /* FOSSIL_IO_FRAMEWORK_H */ |
0 commit comments