Changed code to support Windows libraries and functions as well

master
Aadhavan Srinivasan 8 months ago
parent ddbbc322a6
commit 4b3d5387a1

@ -5,7 +5,7 @@
#include <cerrno>
#include <fcntl.h>
#ifndef _WIN_32
#ifndef _WIN32
const int INVALID_SOCKET = -1;
#endif
@ -27,10 +27,8 @@ int sock_quit(void) {
}
/* Function to create a socket - Accepts IP version(4 or 6), protocol
type (PROTO_TCP or PROTO_UDP) and a flag to indicate whether the socket
should be set in blocking mode or not. This flag is ONLY FOR TCP. It does
nothing if the protocol is UDP.*/
int create_socket(int network, char transport, bool is_blocking) {
type (PROTO_TCP or PROTO_UDP). */
SOCKET create_socket(int network, char transport) {
sock_init();
int domain;
int type;
@ -57,11 +55,6 @@ int create_socket(int network, char transport, bool is_blocking) {
int set_opt = 1;
setsockopt(newSock, SOL_SOCKET, SO_REUSEADDR, (char *)&set_opt, sizeof(set_opt));
if (is_blocking && transport == ES_TCP) {
int flags = fcntl(newSock, F_GETFL);
flags |= O_NONBLOCK;
fcntl(newSock,F_SETFL,flags);
}
return newSock;
}
@ -90,8 +83,8 @@ int create_addr(int network, char* address, int port,struct sockaddr* dest) {
}
int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct, bool is_blocking) {
int socket = create_socket(network,transport, is_blocking);
SOCKET create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct) {
int socket = create_socket(network,transport);
if (socket < 0) {
return (-1 * errno);
}
@ -116,7 +109,7 @@ int create_local (int network, char transport, char* address, int port,struct so
return socket;
}
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct, bool is_blocking) {
SOCKET create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct) {
struct addrinfo hints; /* Used to tell getaddrinfo what kind of address we want */
struct addrinfo* results; /* Used by getaddrinfo to store the addresses */
@ -142,7 +135,7 @@ int create_remote (int network,char transport,char* address,int port,struct sock
create_addr(network,address,port,remote_addr_struct);
}
int socket = create_socket(network,transport, is_blocking);
int socket = create_socket(network,transport);
if (socket < 0) {
return (-1 * errno);
}

@ -1,10 +1,12 @@
#ifndef EASYSOCK_HPP_
#define EASYSOCK_HPP_
#ifdef _WIN_32
#ifdef _WIN32
#include <winsock2.h>
#include <Ws2tcpip.h>
#else
#include <winsock.h>
#include <ws2tcpip.h>
#endif
#ifdef linux
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
@ -12,7 +14,7 @@
#include <arpa/inet.h>
#endif
#ifndef _WIN_32
#ifndef _WIN32
typedef int SOCKET;
#endif
@ -25,13 +27,12 @@ const char ES_UDP = 'U';
a layer 3 - network layer - integer, which must be '4' for IPv4
and 6 for IPv6;
a layer 4 - transport layer - character, which must be 'T' for
TCP or 'U' for UDP; and
a bool that indicates whether the socket should be blocking or non-blocking.
TCP or 'U' for UDP.
It returns the created socket, or -1 if the socket creation failed.*/
It creates a _blocking_ socket, and returns the created socket, or -1
if the socket creation failed.*/
SOCKET create_socket(int network, char transport, bool is_blocking = false);
SOCKET create_socket(int network, char transport);
/* This function fills in the sockaddr struct 'dest' based on the given information.
@ -43,7 +44,7 @@ and dest is a pointer to the sockaddr struct that will be filled in.
The function returns with -202 if the network parameter contained neither '4'
nor '6'. */
SOCKET create_addr(int network, char* address, int port,struct sockaddr* dest);
int create_addr(int network, char* address, int port,struct sockaddr* dest);
@ -53,7 +54,7 @@ same as above.
It prints the error returned by 'bind' if something went wrong, and returns ( -1 * errno ).*/
SOCKET create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct, bool is_blocking = false);
SOCKET create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct);
/* This function utilizes the same functions as 'create_local' but _connects_ to the
@ -62,7 +63,7 @@ as above. This function needs an empty 'sockaddr *' structure passed to it, whic
If something goes wrong, this function returns with ( -1 * errno ). */
SOCKET create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct, bool is_blocking = false);
SOCKET create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct);
/* check_ip_ver - This function checks if the given string is an IPv4 address (returns 4),
IPv6 address (returns 6) or neither (returns -1). */

@ -2,7 +2,13 @@
#define _SOCK_CLASS
#include <string>
#include <sys/socket.h>
#ifdef linux
#include <sys/socket.h>
#endif
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
/* Global constants - can be used by children classes as return values, and by any clients to check what type the socket is of */
const int SOCK_CLIENT = 'C';

@ -1,3 +1,18 @@
#if defined(_WIN32)
#define NOGDI // All GDI defines and routines
#define NOUSER // All USER defines and routines
#define WIN32_LEAN_AND_MEAN
#include <windows.h> // or any library that uses Windows.h
#endif
#if defined(_WIN32) // raylib uses these names as function parameters
#undef near
#undef far
#endif
#include <iostream>
#include <cmath>
#include <cstring>

@ -1,9 +1,15 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#ifdef linux
#include <arpa/inet.h>
#endif
#ifdef _WIN32
#include <winsock2.h>
#endif
#include "includes/serialization.h"
/* Takes in float values, casts them to uint16_t and creates a Serial_Data struct */
Serial_Data Serial_create_data(float pad_x, float pad_y, float ball_x, float ball_y) {
Serial_Data data;

@ -1,4 +1,9 @@
#include <sys/socket.h>
#ifdef linux
#include <sys/socket.h>
#endif
#ifdef _WIN32
#include <winsock2.h>
#endif
#include <fcntl.h>
#include "includes/sock.hpp"
#include "includes/server.hpp"
@ -15,7 +20,6 @@ Server::~Server() {
/* Sends given data through the peer socket - This method is overriden from the
base method, because a different socket must be used. In the server's case, the
'peer' socket i.e. the socket returned after calling 'accept', must be used. */
void Server::sendAll(std::string to_send) {
Sock::sendAll(to_send);
}

Loading…
Cancel
Save