forked from vic4key/Vutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.AsyncSocket.h
More file actions
94 lines (77 loc) · 2.3 KB
/
Sample.AsyncSocket.h
File metadata and controls
94 lines (77 loc) · 2.3 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
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once
// https://github.com/vic4key/Async-Socket-Example-MFC
#include "Sample.h"
#if defined(VU_SOCKET_ENABLED)
void fnExampleBinding(const vu::CSocket::TEndPoint& endpoint)
{
vu::CAsyncSocket server;
server.On(vu::CAsyncSocket::OPEN, [](vu::CSocket& client) -> void
{
printf("\n");
printf("client %d opened\n", client.GetRemoteSAI().sin_port);
});
server.On(vu::CAsyncSocket::CLOSE, [](vu::CSocket& client) -> void
{
printf("client %d closed\n", client.GetRemoteSAI().sin_port);
});
server.On(vu::CAsyncSocket::SEND, [](vu::CSocket& client) -> void
{
std::string s = "hello from server";
client.Send(s.data(), int(s.size()));
printf("client %d send `%s`\n", client.GetRemoteSAI().sin_port, s.c_str());
});
server.On(vu::CAsyncSocket::RECV, [](vu::CSocket& client) -> void
{
vu::CBuffer data(KiB);
client.Recv(data);
printf("client %d recv `%s`\n", client.GetRemoteSAI().sin_port, data.ToStringA().c_str());
});
server.Bind(endpoint);
server.Listen();
server.Run();
server.Close();
}
void fnExampleInheritance(const vu::CSocket::TEndPoint& endpoint)
{
class CAsyncSocketServer : public vu::CAsyncSocket
{
public:
virtual void OnOpen(vu::CSocket& client)
{
printf("\n");
printf("client %d opened\n", client.GetRemoteSAI().sin_port);
}
virtual void OnClose(vu::CSocket& client)
{
printf("client %d closed\n", client.GetRemoteSAI().sin_port);
}
virtual void OnSend(vu::CSocket& client)
{
std::string s = "hello from server";
client.Send(s.data(), int(s.size()));
printf("client %d send `%s`\n", client.GetRemoteSAI().sin_port, s.c_str());
}
virtual void OnRecv(vu::CSocket& client)
{
vu::CBuffer data(KiB);
client.Recv(data);
std::string s(reinterpret_cast<char*>(data.GetpBytes()));
printf("client %d recv `%s`\n", client.GetRemoteSAI().sin_port, s.c_str());
}
};
CAsyncSocketServer server;
server.Bind(endpoint);
server.Listen();
server.Run();
server.Close();
}
#endif // VU_SOCKET_ENABLED
DEF_SAMPLE(AsyncSocket)
{
#if defined(VU_SOCKET_ENABLED)
// const vu::CSocket::TEndPoint EndPoint("127.0.0.1", 1609);
// fnExampleBinding(EndPoint);
// fnExampleInheritance(EndPoint);
#endif // VU_SOCKET_ENABLED
return vu::VU_OK;
}