-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.cpp
More file actions
82 lines (64 loc) · 1.34 KB
/
Copy pathNetwork.cpp
File metadata and controls
82 lines (64 loc) · 1.34 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
72
73
74
75
76
77
78
79
80
81
82
#pragma once
#include "Network.hpp"
#ifndef UNICODE
#define UNICODE 1
#endif
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
int NetSend(s64 sock, char* data, int len, int flags)
{
return send(sock, data, len, flags);
}
int NetRecv(s64 sock, char* dst, int len, int flags)
{
return recv(sock, dst, len, flags);
}
int NetBind(s64 sock, const struct sockaddr* name, int namelen)
{
return bind(sock, name, namelen);
}
int NetListen(s64 sock, int backlog)
{
return listen(sock, backlog);
}
s64 NetAccept(s64 sock, struct sockaddr* addr, int* addrlen)
{
return accept(sock, addr, addrlen);
}
s64 NetOpen(int af, int type, int protocol)
{
return socket(af, type, protocol);
}
int NetShutdown(s64 sock, int how)
{
return shutdown(sock, how);
}
int NetClose(s64 sock)
{
return closesocket(sock);
}
bool NetIsSockAvaliable(s64 sock)
{
s32 result = {};
char c = {};
// Important to just Peek, so we don't consume the buffer in case the socket was available.
result = recv(sock, &c, 1, MSG_PEEK);
return result > 0;
}
void NetSendAll(s64 sock, String8 data)
{
int sent = 0;
while (sent < data.length)
{
sent += NetSend(sock, data.data, static_cast<int>(data.length - sent));
}
}
void NetRecvAll(s64 sock, String8 dest)
{
int recvd = 0;
while (recvd < dest.length)
{
recvd += NetRecv(sock, &dest.data[recvd], 512);
}
}