mgba/src/util/socket.h

119 lines
2.6 KiB
C
Raw Normal View History

2014-02-04 07:01:26 +00:00
#ifndef SOCKET_H
#define SOCKET_H
2014-10-12 01:18:47 +00:00
#include "util/common.h"
2014-07-20 23:37:54 +00:00
#ifdef __cplusplus
#define restrict __restrict__
#endif
2014-02-04 07:01:26 +00:00
#ifdef _WIN32
#include <winsock2.h>
2014-04-24 04:42:08 +00:00
#include <ws2tcpip.h>
2014-02-04 07:01:26 +00:00
2014-04-24 04:42:08 +00:00
#define SOCKET_FAILED(s) (s) == INVALID_SOCKET
2014-02-04 07:01:26 +00:00
typedef SOCKET Socket;
#else
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
2014-02-04 07:01:26 +00:00
#include <sys/socket.h>
2014-04-24 04:42:08 +00:00
#define INVALID_SOCKET (-1)
#define SOCKET_FAILED(s) (s) < 0
2014-02-04 07:01:26 +00:00
typedef int Socket;
#endif
2014-02-05 09:22:34 +00:00
static inline void SocketSubsystemInitialize() {
2014-02-04 07:01:26 +00:00
#ifdef _WIN32
WSAStartup(MAKEWORD(2, 2), 0);
#endif
}
2014-02-05 09:22:34 +00:00
static inline ssize_t SocketSend(Socket socket, const void* buffer, size_t size) {
2014-02-04 07:01:26 +00:00
return write(socket, buffer, size);
}
2014-02-05 09:22:34 +00:00
static inline ssize_t SocketRecv(Socket socket, void* buffer, size_t size) {
2014-02-04 07:01:26 +00:00
return read(socket, buffer, size);
}
2014-02-05 09:22:34 +00:00
static inline Socket SocketOpenTCP(int port, uint32_t bindAddress) {
2014-02-04 07:01:26 +00:00
Socket sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
2014-04-24 04:42:08 +00:00
if (SOCKET_FAILED(sock)) {
2014-02-04 07:01:26 +00:00
return sock;
}
struct sockaddr_in bindInfo = {
.sin_family = AF_INET,
.sin_port = htons(port),
.sin_addr = {
.s_addr = htonl(bindAddress)
}
};
int err = bind(sock, (const struct sockaddr*) &bindInfo, sizeof(struct sockaddr_in));
if (err) {
close(sock);
return -1;
}
return sock;
}
static inline Socket SocketConnectTCP(int port, uint32_t destinationAddress) {
Socket sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
2014-04-24 04:42:08 +00:00
if (SOCKET_FAILED(sock)) {
return sock;
}
struct sockaddr_in bindInfo = {
.sin_family = AF_INET,
.sin_port = htons(port),
.sin_addr = {
.s_addr = htonl(destinationAddress)
}
};
int err = connect(sock, (const struct sockaddr*) &bindInfo, sizeof(struct sockaddr_in));
if (err) {
close(sock);
return -1;
}
return sock;
}
2014-02-05 09:22:34 +00:00
static inline Socket SocketListen(Socket socket, int queueLength) {
2014-02-04 07:01:26 +00:00
return listen(socket, queueLength);
}
2014-02-05 09:22:34 +00:00
static inline Socket SocketAccept(Socket socket, struct sockaddr* restrict address, socklen_t* restrict addressLength) {
2014-02-04 07:01:26 +00:00
return accept(socket, address, addressLength);
}
2014-02-05 09:22:34 +00:00
static inline int SocketClose(Socket socket) {
2014-02-04 07:01:26 +00:00
return close(socket) >= 0;
}
2014-02-05 09:22:34 +00:00
static inline int SocketSetBlocking(Socket socket, int blocking) {
2014-02-04 07:01:26 +00:00
#ifdef _WIN32
2014-04-24 04:42:08 +00:00
u_long unblocking = !blocking;
return ioctlsocket(socket, FIONBIO, &unblocking) == NO_ERROR;
2014-02-04 07:01:26 +00:00
#else
int flags = fcntl(socket, F_GETFL);
if (flags == -1) {
return 0;
}
if (blocking) {
flags &= ~O_NONBLOCK;
} else {
flags |= O_NONBLOCK;
}
return fcntl(socket, F_SETFL, flags) >= 0;
#endif
}
static inline int SocketSetTCPPush(Socket socket, int push) {
return setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (char*) &push, sizeof(int)) >= 0;
}
2014-02-04 07:01:26 +00:00
#endif