-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
131 lines (112 loc) · 2.6 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
130
131
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <poll.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <atomic>
#include "io.h"
#define INPUT_CANCEL_ORDER 'C'
#define INPUT_BUY_ORDER 'B'
#define INPUT_SELL_ORDER 'S'
static char* line_buffer;
static size_t line_buffer_size = 0;
static std::atomic<bool> main_is_exiting = 0;
static void* poll_thread(void* fdptr)
{
struct pollfd pfd {};
pfd.fd = (int) (long) fdptr;
pfd.events = 0;
while(!main_is_exiting)
{
if(poll(&pfd, 1, -1) == -1)
{
perror("poll");
_exit(1);
}
if(main_is_exiting)
{
break;
}
if(pfd.revents & (POLLERR | POLLHUP))
{
fprintf(stderr, "Connection closed by server\n");
_exit(0);
}
}
return 0;
}
int main(int argc, char* argv[])
{
if(argc < 2)
{
fprintf(stderr, "Usage: %s <path of socket to connect to> < <input>\n", argv[0]);
return 1;
}
int clientfd = socket(AF_UNIX, SOCK_STREAM, 0);
if(clientfd == -1)
{
perror("socket");
return 1;
}
{
struct sockaddr_un sockaddr {};
sockaddr.sun_family = AF_UNIX;
strncpy(sockaddr.sun_path, argv[1], sizeof(sockaddr.sun_path) - 1);
if(connect(clientfd, (const struct sockaddr*) &sockaddr, sizeof(sockaddr)) != 0)
{
perror("connect");
return 1;
}
}
FILE* client = fdopen(clientfd, "r+");
setbuf(client, NULL);
pthread_t poll_thread_handle;
if(pthread_create(&poll_thread_handle, NULL, poll_thread, (void*) (long) clientfd) < 0)
{
fprintf(stderr, "Failed to create poll thread\n");
return 1;
}
while(1)
{
ClientCommand input {};
ssize_t line_length = getline(&line_buffer, &line_buffer_size, stdin);
if(line_length == -1)
break;
switch(line_buffer[0])
{
case '#':
case '\n': continue;
case INPUT_CANCEL_ORDER:
input.type = input_cancel;
if(sscanf(line_buffer + 1, " %u", &input.order_id) != 1)
{
fprintf(stderr, "Invalid cancel order: %s\n", line_buffer);
return 1;
}
break;
case INPUT_BUY_ORDER: input.type = input_buy; goto new_order;
case INPUT_SELL_ORDER:
input.type = input_sell;
new_order:
if(sscanf(line_buffer + 1, " %u %8s %u %u", &input.order_id, input.instrument, &input.price, &input.count) != 4)
{
fprintf(stderr, "Invalid new order: %s\n", line_buffer);
return 1;
}
break;
default: fprintf(stderr, "Invalid command '%c'\n", line_buffer[0]); return 1;
}
if(fwrite(&input, 1, sizeof(input), client) != sizeof(input))
{
fprintf(stderr, "Failed to write command\n");
return 1;
}
}
main_is_exiting = 1;
fclose(client);
return ferror(stderr) ? 1 : 0;
}