-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHvsClient.cpp
More file actions
160 lines (128 loc) · 3.37 KB
/
HvsClient.cpp
File metadata and controls
160 lines (128 loc) · 3.37 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdint.h>
#include <time.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <atomic>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std::chrono_literals;
std::atomic<int> msgCount {};
#define SEND_BUFFER_LENGTH 4 * 1024 * 1024
typedef struct _GUID {
uint32_t Data1;
uint16_t Data2;
uint16_t Data3;
uint8_t Data4[8];
} GUID;
typedef struct _SOCKADDR_VM
{
unsigned short Family;
unsigned short Reserved;
unsigned int SvmPort;
unsigned int SvmCID;
unsigned char svm_zero[sizeof(struct sockaddr) -
sizeof(sa_family_t) - sizeof(unsigned short) -
sizeof(unsigned int) - sizeof(unsigned int)];
} SOCKADDR_VM;
#define DEFINE_GUID1(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
const GUID name \
= { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
#define SERVICE_PORT 0x3049197c
#define INVALID_SOCKET -1
#define VMADDR_CID_HOST 2
typedef int SOCKET;
#define SOCKET_ERROR -1
#define closesocket(_fd) close(_fd)
#define SD_SEND SHUT_WR
class Client {
public:
Client()
{
ConnectSocket = INVALID_SOCKET;
}
bool Start()
{
ConnectSocket = socket(AF_VSOCK, SOCK_STREAM, 0);
if (ConnectSocket == INVALID_SOCKET)
{
fprintf(stderr, "Socket Error: %d. %s\n", errno, strerror(errno));
return false;
}
// Connect to server
SOCKADDR_VM savm;
memset(&savm, 0, sizeof(savm));
savm.Family = AF_VSOCK;
savm.SvmCID = VMADDR_CID_HOST;
savm.SvmPort = SERVICE_PORT;
int iResult = connect(ConnectSocket, (const struct sockaddr *)&savm, sizeof(savm));
if (iResult == SOCKET_ERROR)
{
fprintf(stderr, "Connect Error: %d. %s\n", errno, strerror(errno));
return false;
}
return true;
};
// Free the resouces
void Stop()
{
int iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR)
{
fprintf(stderr, "shutdown Error: %d. %s\n", errno, strerror(errno));
}
closesocket(ConnectSocket);
};
// Send message to server
bool Send(char* msg, int len)
{
int iResult = send(ConnectSocket, msg, len, 0);
if (iResult == SOCKET_ERROR)
{
fprintf(stderr, "send Error: %d. %s\n", errno, strerror(errno));
Stop();
return false;
}
++msgCount;
return true;
};
private:
SOCKET ConnectSocket;
};
int main(int argc, char* argv[])
{
std::string msg;
Client client;
if (!client.Start())
return 1;
std::thread t([&]
{
while (true)
{
std::this_thread::sleep_for(10s);
std::cout << "Throughput " << msgCount / 10 << " req/sec" << std::endl;
msgCount = 0;
}
});
t.detach();
std::vector<char> sendbuf(SEND_BUFFER_LENGTH);
for (int i = 0; i < 99999999; ++i)
{
if(!client.Send(sendbuf.data(), SEND_BUFFER_LENGTH))
{
std::cout << "Send failed at loop " << i << std::endl;
break;
}
}
client.Stop();
return 0;
}