Mineserver
A rewrite of Minecraft 1.8.9 in C++ !
Loading...
Searching...
No Matches
network.h
Go to the documentation of this file.
1
12#ifndef MINESERVER_NETWORK_H
13#define MINESERVER_NETWORK_H
14
15#include <string>
16#include <types/uuid.h>
17#if defined(__linux__)
18#include <netdb.h>
19#include <unistd.h>
20#include <error.h>
21#include <errno.h>
22#elif defined(_WIN32)
23#include <WS2tcpip.h>
24#include <Windef.h>
25#include <cstdio>
26#include <io.h>
27#endif
28
29#if defined(__linux__)
30typedef int socket_t;
31#elif defined(_WIN32)
32typedef SOCKET socket_t;
33typedef SSIZE_T ssize_t;
34#endif
35
42{
43private:
44 bool connected;
45 socket_t sock;
46 std::string address;
47
48public:
66 ClientSocket(int type);
71 ~ClientSocket() = default;
72
84 bool connect(const char *address, int port) const;
85
92 bool isValid() const;
101 ssize_t read(std::byte *buffer, size_t len) const;
110 ssize_t write(const std::byte *buffer, size_t len) const;
118 void close() const;
124 size_t getAvailableBytes() const;
125
132 {
133 return sock;
134 }
135
141 std::string getAddress() const
142 {
143 return address;
144 };
145
152 bool isLocal() const
153 {
154 return address == "127.0.0.1" || address == "localhost";
155 };
156};
157
164{
165private:
166 socket_t sock;
167
168public:
182 ServerSocket(int type);
187 ~ServerSocket() = default;
188
199 bool bind(const char *address, int port) const;
208 void start(unsigned int backlog) const;
217 ClientSocket accept() const;
225 void close() const;
226
239 static bool init();
250 static bool cleanup();
251};
252
257namespace mojangapi
258{
264 {
269 std::string name;
275 };
276
286 HasJoinedResponse hasJoined(const std::string &username, const std::string &serverId, const std::string &ip = "");
287}
288
289#endif
Network POSIX Client.
Definition network.h:42
~ClientSocket()=default
Destroy the Client Socket object.
ClientSocket(socket_t client, char *addr)
Construct a new Client Socket object.
Definition network.cpp:146
ssize_t write(const std::byte *buffer, size_t len) const
Writes to the socket.
Definition network.cpp:206
bool isLocal() const
Checks whether the connection is a local one.
Definition network.h:152
size_t getAvailableBytes() const
Get the number of available bytes.
Definition network.cpp:224
bool isValid() const
Whether the connection is valid.
Definition network.cpp:236
bool connect(const char *address, int port) const
Connects to an address on a certain port.
Definition network.cpp:173
ssize_t read(std::byte *buffer, size_t len) const
Reads from the socket.
Definition network.cpp:192
void close() const
Closes the connection.
Definition network.cpp:215
std::string getAddress() const
Get the Address of the client.
Definition network.h:141
socket_t getHandle() const
Get the Handle of the socket.
Definition network.h:131
MinecraftUUID compliant data holder.
Definition uuid.h:27
Network POSIX Server.
Definition network.h:164
static bool init()
Inits the POSIX API.
Definition network.cpp:100
bool bind(const char *address, int port) const
Binds the server to port / address.
Definition network.cpp:38
ServerSocket()
Construct a new empty Server Socket object.
void close() const
Closes the connection.
Definition network.cpp:91
static bool cleanup()
Cleansup the POSIX API.
Definition network.cpp:137
ClientSocket accept() const
Accepts a new client.
Definition network.cpp:73
void start(unsigned int backlog) const
Starts the server.
Definition network.cpp:68
~ServerSocket()=default
Destroy the Server Socket object.
constexpr std::string_view type_name()
Gets the name of the type paramater.
Definition event.h:56
The Mojang API holder.
Definition network.h:258
HasJoinedResponse hasJoined(const std::string &username, const std::string &serverId, const std::string &ip="")
Checks whether the player has joined the server.
Definition network.cpp:246
Reponse for hasJoined endpoint.
Definition network.h:264
MinecraftUUID id
MinecraftUUID of the player.
Definition network.h:274
std::string name
Name of the player.
Definition network.h:269
The file containing MinecraftUUID logic.