Skip to content

Commit 88a162f

Browse files
expaned protocall support
1 parent 6d5b9e3 commit 88a162f

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

code/logic/fossil/io/network.h

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
#define FOSSIL_IO_INVALID_SOCKET (-1)
3333
#endif
3434

35+
#define FOSSIL_IO_SOCKET_TYPE_TCP SOCK_STREAM
36+
#define FOSSIL_IO_SOCKET_TYPE_UDP SOCK_DGRAM
37+
#define FOSSIL_IO_SOCKET_TYPE_RAW SOCK_RAW
38+
#define FOSSIL_IO_SOCKET_TYPE_RDM SOCK_RDM
39+
#define FOSSIL_IO_SOCKET_TYPE_SEQPACKET SOCK_SEQPACKET
40+
3541
#ifdef __cplusplus
3642
extern "C" {
3743
#endif
@@ -51,7 +57,7 @@ void fossil_io_network_destroy(void);
5157
* Create a new TCP socket.
5258
* Returns a valid socket on success or FOSSIL_IO_INVALID_SOCKET on failure.
5359
*/
54-
fossil_io_socket_t fossil_io_network_create_socket(void);
60+
fossil_io_socket_t fossil_io_network_create_socket(int type);
5561

5662
/**
5763
* Bind a socket to a specific port (IPv4/IPv6).
@@ -94,8 +100,21 @@ int fossil_io_network_receive(fossil_io_socket_t sock, void *buffer, size_t len)
94100
*/
95101
void fossil_io_network_close(fossil_io_socket_t sock);
96102

103+
/**
104+
* Send data to a specific IP address and port.
105+
* Returns the number of bytes sent, or -1 on failure.
106+
*/
107+
int fossil_io_network_sendto(fossil_io_socket_t sock, const void *data, size_t len, const char *ip, uint16_t port);
108+
109+
/**
110+
* Receive data from a specific IP address and port.
111+
* Returns the number of bytes received, or -1 on failure.
112+
*/
113+
int fossil_io_network_recvfrom(fossil_io_socket_t sock, void *buffer, size_t len, char *ip, uint16_t *port);
114+
97115
#ifdef __cplusplus
98116
}
117+
99118
/**
100119
* C++ wrapper for the output functions.
101120
*/
@@ -128,8 +147,8 @@ namespace fossil {
128147
* Create a new TCP socket.
129148
* Returns a valid socket on success or FOSSIL_IO_INVALID_SOCKET on failure.
130149
*/
131-
static fossil_io_socket_t create_socket(void) {
132-
return fossil_io_network_create_socket();
150+
static fossil_io_socket_t create_socket(int type) {
151+
return fossil_io_network_create_socket(type);
133152
}
134153

135154
/**
@@ -187,6 +206,22 @@ namespace fossil {
187206
fossil_io_network_close(sock);
188207
}
189208

209+
/**
210+
* Send data to a specific IP address and port.
211+
* Returns the number of bytes sent, or -1 on failure.
212+
*/
213+
static int sendto(fossil_io_socket_t sock, const void *data, size_t len, const char *ip, uint16_t port) {
214+
return fossil_io_network_sendto(sock, data, len, ip, port);
215+
}
216+
217+
/**
218+
* Receive data from a specific IP address and port.
219+
* Returns the number of bytes received, or -1 on failure.
220+
*/
221+
static int recvfrom(fossil_io_socket_t sock, void *buffer, size_t len, char *ip, uint16_t *port) {
222+
return fossil_io_network_recvfrom(sock, buffer, len, ip, port);
223+
}
224+
190225
};
191226
}
192227
}

code/logic/network.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ void fossil_io_network_destroy(void) {
3535
#endif
3636
}
3737

38-
fossil_io_socket_t fossil_io_network_create_socket(void) {
39-
fossil_io_socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
38+
fossil_io_socket_t fossil_io_network_create_socket(int type) {
39+
fossil_io_socket_t sock = socket(AF_INET, type, 0);
4040
if (sock == FOSSIL_IO_INVALID_SOCKET) {
4141
#ifdef _WIN32
4242
fprintf(stderr, "Socket creation failed with error: %d\n", WSAGetLastError());
@@ -138,6 +138,43 @@ int fossil_io_network_receive(fossil_io_socket_t sock, void *buffer, size_t len)
138138
return bytes_received;
139139
}
140140

141+
int fossil_io_network_sendto(fossil_io_socket_t sock, const void *data, size_t len, const char *ip, uint16_t port) {
142+
struct sockaddr_in dest_addr;
143+
dest_addr.sin_family = AF_INET;
144+
dest_addr.sin_port = htons(port);
145+
dest_addr.sin_addr.s_addr = inet_addr(ip);
146+
147+
int bytes_sent = sendto(sock, data, (int)len, 0, (struct sockaddr*)&dest_addr, sizeof(dest_addr));
148+
if (bytes_sent == -1) {
149+
#ifdef _WIN32
150+
fprintf(stderr, "Sendto failed with error: %d\n", WSAGetLastError());
151+
#else
152+
perror("Sendto failed");
153+
#endif
154+
}
155+
return bytes_sent;
156+
}
157+
158+
int fossil_io_network_recvfrom(fossil_io_socket_t sock, void *buffer, size_t len, char *ip, uint16_t *port) {
159+
struct sockaddr_in src_addr;
160+
socklen_t addr_len = sizeof(src_addr);
161+
162+
int bytes_received = recvfrom(sock, buffer, (int)len, 0, (struct sockaddr*)&src_addr, &addr_len);
163+
if (bytes_received == -1) {
164+
#ifdef _WIN32
165+
fprintf(stderr, "Recvfrom failed with error: %d\n", WSAGetLastError());
166+
#else
167+
perror("Recvfrom failed");
168+
#endif
169+
} else if (ip) {
170+
strcpy(ip, inet_ntoa(src_addr.sin_addr));
171+
if (port) {
172+
*port = ntohs(src_addr.sin_port);
173+
}
174+
}
175+
return bytes_received;
176+
}
177+
141178
void fossil_io_network_close(fossil_io_socket_t sock) {
142179
#ifdef _WIN32
143180
if (closesocket(sock) == SOCKET_ERROR) {

0 commit comments

Comments
 (0)