-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetplat.cpp
More file actions
71 lines (62 loc) · 1.87 KB
/
netplat.cpp
File metadata and controls
71 lines (62 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "network.hpp"
/// This file contains plateform specific network implementations
#ifdef WIN
#include <winsock2.h>
#include <ws2tcpip.h>
static WSADATA wsa_data;
static SOCKET connection;
bool net::pl_open(char const* address, uint16_t port) {
if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0) return false;
connection = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (connection == INVALID_SOCKET) return false;
struct sockaddr_in dest;
dest.sin_family = AF_INET;
inet_pton(AF_INET, address, &dest.sin_addr);
dest.sin_port = htons(port);
auto success = connect(connection,
(sockaddr const*)&dest,
sizeof(struct sockaddr_in));
if (success == SOCKET_ERROR) return false;
return true;
}
ssize_t net::pl_write(char const* buffer, size_t count) {
return send(connection, buffer, count, 0);
}
ssize_t net::pl_read(char* buffer, size_t count) {
return recv(connection, buffer, count, 0);
}
void net::pl_close() {
closesocket(connection);
WSACleanup();
}
#else
// UNIX Code
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/socket.h>
// File descriptor for network
static int connection = 1; // stdout lol
bool net::pl_open(char const* address, uint16_t port) {
connection = socket(AF_INET, SOCK_STREAM, 0);
if (connection == -1) return false;
// Localhost
struct sockaddr_in dest;
dest.sin_family = AF_INET;
inet_pton(AF_INET, address, &dest.sin_addr);
dest.sin_port = htons(port);
auto success = connect(connection,
(sockaddr const*)&dest,
sizeof(struct sockaddr_in)) >= 0;
if (!success) return false;
return true;
}
ssize_t net::pl_write(char const* buffer, size_t count) {
return write(connection, buffer, count);
}
ssize_t net::pl_read(char* buffer, size_t count) {
return read(connection, buffer, count);
}
void net::pl_close() {
close(connection);
}
#endif