-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cpp
129 lines (118 loc) · 3.91 KB
/
Client.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "Client.h"
#include "Packet.h"
#include "Datagram.h"
#include "Address6.h"
#include "Address4.h"
#include "WSA.h"
namespace Tool
{
namespace Network
{
Client::Client(Address6 remoteAddress, u16 localPort): data(0)
{
this->wsa = new WSA();
sockaddr_in6 localAddress;
u32 blockmode = 1;
u32 outcount;
int length = sizeof(localAddress);
localAddress.sin6_addr = in6addr_any;
localAddress.sin6_family = AF_INET6;
localAddress.sin6_port = htons(localPort);
localAddress.sin6_flowinfo = 0;
localAddress.sin6_scope_id = 0;
sock = WSASocket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0, 0, 0);
WSAERROR(sock == INVALID_SOCKET)
WSAIoctl( sock, FIONBIO, &blockmode, sizeof(u32), &blockmode, sizeof(u32), &outcount, 0, 0);
WSAERROR(bind( sock, (const sockaddr*) &localAddress, length))
WSAERROR(WSAConnect(sock,(sockaddr*) &remoteAddress.Tosockaddr(), sizeof(sockaddr_in6), 0, 0, 0, 0))
}
Client::Client(Address4 remoteAddress, u16 localPort): data(0)
{
this->wsa = new WSA();
sockaddr_in localAddress;
u32 blockmode = 1;
u32 outcount;
int length = sizeof(localAddress);
localAddress.sin_addr.S_un.S_addr = 0;
localAddress.sin_family = AF_INET;
localAddress.sin_port = htons(localPort);
sock = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0, 0, 0);
WSAERROR(sock == INVALID_SOCKET)
WSAIoctl( sock, FIONBIO, &blockmode, sizeof(u32), &blockmode, sizeof(u32), &outcount, 0, 0);
WSAERROR(bind( sock, (const sockaddr*) &localAddress, length))
WSAERROR(WSAConnect(sock,(sockaddr*) &remoteAddress.Tosockaddr(), sizeof(sockaddr_in), 0, 0, 0, 0))
}
Client::Client(Address4 remoteAddress, Address4 localAddress): data(0)
{
this->wsa = new WSA();
u32 blockmode = 1;
u32 outcount;
int length = sizeof(localAddress);
sock = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0, 0, 0);
WSAERROR(sock == INVALID_SOCKET)
WSAIoctl( sock, FIONBIO, &blockmode, sizeof(u32), &blockmode, sizeof(u32), &outcount, 0, 0);
WSAERROR(bind( sock, (const sockaddr*) &localAddress, length))
WSAERROR(WSAConnect(sock,(sockaddr*) &remoteAddress.Tosockaddr(), sizeof(sockaddr_in), 0, 0, 0, 0))
}
Client::Client(Address6 remoteAddress, Address6 localAddress): data(0)
{
this->wsa = new WSA();
u32 blockmode = 1;
u32 outcount;
int length = sizeof(localAddress);
sock = WSASocket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0, 0, 0);
WSAERROR(sock == INVALID_SOCKET)
WSAIoctl( sock, FIONBIO, &blockmode, sizeof(u32), &blockmode, sizeof(u32), &outcount, 0, 0);
WSAERROR(bind( sock, (const sockaddr*) &localAddress, length))
WSAERROR(WSAConnect(sock,(sockaddr*) &remoteAddress.Tosockaddr(), sizeof(sockaddr_in6), 0, 0, 0, 0))
}
Client::~Client(void)
{
closesocket(this->sock);
delete data;
delete this->wsa;
}
void Client::Send(Packet* packet)
{
Datagram data;
u16 iD = packet->GetID();
data.Write<w16>(iD);
send(packet->Get(&data));
}
u32 Client::send(Datagram* data)
{
WSABUF buffer = { data->Size(), (CHAR*)data->Data()};
u32 sentBytes;
WSAERROR(WSASend(sock, &buffer, 1, &sentBytes, 0, 0, 0))
return sentBytes;
}
int Client::receive(WSABUF* buffer)
{
int result;
DWORD flags = 0;
length = sizeof(sockaddr_in6);
buffer->buf = (CHAR*) new w8[65536];
buffer->len = 65536;
result = WSARecvFrom(sock, buffer, 1, &(buffer->len), &flags, (sockaddr*) &this->lastReceiveAddress, &length, 0, 0);
return result;
}
Address6 Client::GetLastReceiveAddress(void)
{
return Address6(lastReceiveAddress);
}
u16 Client::Checkout(void)
{
WSABUF buf;
receive(&buf);
data = new Datagram((w8*)buf.buf, buf.len);
delete[] buf.buf;
buf.len = 0;
return data->Read<u16>();
}
Packet* Client::Receive(Packet* outPacket)
{
outPacket->Set(data);
return outPacket;
}
}
}