diff --git a/src/.main.cpp.un~ b/src/.main.cpp.un~ new file mode 100644 index 0000000..1cc340b Binary files /dev/null and b/src/.main.cpp.un~ differ diff --git a/src/Network/.Socket.hpp.un~ b/src/Network/.Socket.hpp.un~ new file mode 100644 index 0000000..611b2a4 Binary files /dev/null and b/src/Network/.Socket.hpp.un~ differ diff --git a/src/Network/Socket.hpp b/src/Network/Socket.hpp new file mode 100644 index 0000000..dac889a --- /dev/null +++ b/src/Network/Socket.hpp @@ -0,0 +1,51 @@ +#include +#include "../Core/Logger.hpp" + +#pragma once + +#define NET_SOCK_ADDR "0.0.0.0" +#define NET_SOCK_PORT 25565 + +class Socket +{ +private: + Logger log; + +public: + int winInit() + { + WSADATA wsaData; + int result = WSAStartup( MAKEWORD(2, 2), & wsaData ); + if (result != 0) log.err("Network.Socket.winInit: Initialization error"); + + log.info("Network.Socket.winInit: Socket initialized"); + + SOCKET mainSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); + if (mainSocket == INVALID_SOCKET) { + log.err("Network.Socket.winInit: Fatal error: Error creating socket"); + log.err(WSAGetLastError()); + WSACleanup(); + return 1; + } + + log.info("Network.Socket.winInit: Socket created"); + + sockaddr_in service; + memset( & service, 0, sizeof(service) ); + service.sin_family = AF_INET; + service.sin_addr.s_addr = inet_addr(NET_SOCK_ADDR); + service.sin_port = htons(NET_SOCK_PORT); + + log.info("Network.Socket.winInit: Socket configured"); + + if (bind(mainSocket, (SOCKADDR *) & service, sizeof(service)) == SOCKET_ERROR ) { + log.err("Network.Socket.winInit: Fatal error: Bind failed"); + closesocket(mainSocket); + return 1; + } + + log.info("Network.Socket.winInit: Socket bound to address " + NET_SOCK_ADDR + " on port " + string to_string(NET_SOCK_PORT) + ". Ready to listen for connections"); + + return 0; + } +}; diff --git a/src/Network/Socket.hpp~ b/src/Network/Socket.hpp~ new file mode 100644 index 0000000..8d215a6 --- /dev/null +++ b/src/Network/Socket.hpp~ @@ -0,0 +1,51 @@ +#include +#include "../Core/Logger.hpp" + +#pragma once + +#define NET_SOCK_ADDR "0.0.0.0" +#define NET_SOCK_PORT 25565 + +class Socket +{ +private: + Logger log; + +public: + int winInit() + { + WSADATA wsaData; + int result = WSAStartup( MAKEWORD(2, 2), & wsaData ); + if (result != 0) log.err("Network.Socket.winInit: Initialization error"); + + log.info("Network.Socket.winInit: Socket initialized"); + + SOCKET mainSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); + if (mainSocket == INVALID_SOCKET) { + log.err("Network.Socket.winInit: Fatal error: Error creating socket"); + log.err(WSAGetLastError()); + WSACleanup(); + return 1; + } + + log.info("Network.Socket.winInit: Socket created\n"); + + sockaddr_in service; + memset( & service, 0, sizeof(service) ); + service.sin_family = AF_INET; + service.sin_addr.s_addr = inet_addr(NET_SOCK_ADDR); + service.sin_port = htons(NET_SOCK_PORT); + + log.info("Network.Socket.winInit: Socket configured\n"); + + if (bind(mainSocket, (SOCKADDR *) & service, sizeof(service)) == SOCKET_ERROR ) { + log.err("Network.Socket.winInit: Fatal error: Bind failed"); + closesocket(mainSocket); + return 1; + } + + log.info("Network.Socket.winInit: Socket bound to address " + NET_SOCK_ADDR + " on port " + string to_string(NET_SOCK_PORT) + ". Ready to listen for connections"); + + return 0; + } +}; diff --git a/src/main.cpp b/src/main.cpp index e7b0cb5..9f01155 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include "Core/Logger.hpp" +#include "Network/Socket.hpp" #include @@ -9,51 +10,6 @@ using namespace std; Logger log; -class Network -{ -public: - class Socket - { - public: - int winInit() - { - WSADATA wsaData; - int result = WSAStartup( MAKEWORD(2, 2), & wsaData ); - if (result != 0) log.err("Network.Socket.winInit: Initialization error"); - - log.info("Network.Socket.winInit: Socket initialized"); - - SOCKET mainSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); - if (mainSocket == INVALID_SOCKET) { - log.err("Network.Socket.winInit: Fatal error: Error creating socket"); - log.err(WSAGetLastError()); - WSACleanup(); - return 1; - } - - log.info("Network.Socket.winInit: Socket created\n"); - - sockaddr_in service; - memset( & service, 0, sizeof(service) ); - service.sin_family = AF_INET; - service.sin_addr.s_addr = inet_addr(NET_SOCK_ADDR); - service.sin_port = htons(NET_SOCK_PORT); - - log.info("Network.Socket.winInit: Socket configured\n"); - - if (bind(mainSocket, (SOCKADDR *) & service, sizeof(service)) == SOCKET_ERROR ) { - log.err("Network.Socket.winInit: Fatal error: Bind failed"); - closesocket(mainSocket); - return 1; - } - - log.info("Network.Socket.winInit: Socket bound to address " + NET_SOCK_ADDR + " on port " + string to_string(NET_SOCK_PORT) + ". Ready to listen for connections"); - - return 0; - } - }; -}; - int main() { log.raw("mcc v0.0.0"); Network::Socket socket; diff --git a/src/main.cpp~ b/src/main.cpp~ new file mode 100644 index 0000000..195554f --- /dev/null +++ b/src/main.cpp~ @@ -0,0 +1,24 @@ +#include "Core/Logger.hpp" + +#include + +#define NET_SOCK_ADDR "0.0.0.0" +#define NET_SOCK_PORT 25565 + +using namespace std; + +Logger log; + +class Network +{ +public: + +}; + +int main() { + log.raw("mcc v0.0.0"); + Network::Socket socket; + socket.winInit(); + + return 0; +}