-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.c
More file actions
143 lines (129 loc) · 2.93 KB
/
client.c
File metadata and controls
143 lines (129 loc) · 2.93 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
/*
* Copyright (c) 2019-2022 Maksymilian Mruszczak <u at one u x dot o r g>
*
* Simplified sockets client-side API implementation
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <poll.h>
enum {
ON_MESSG,
ON_CLOSE,
ON_STDIN
};
typedef struct Conn Conn;
struct Conn {
struct sockaddr_in addr;
struct pollfd pfd[2];
void (*on_messg)(Conn *, const char *, unsigned int);
void (*on_close)(Conn *, const char *, unsigned int);
void (*on_stdin)(Conn *, const char *, unsigned int);
};
Conn *
client_dial(const char *saddr, unsigned int port)
{
static socklen_t slen = (socklen_t)sizeof(struct sockaddr_in);
Conn *conn = (Conn *)calloc(1, sizeof(Conn));
if (!conn)
return conn;
conn->on_messg = NULL;
conn->on_close = NULL;
conn->on_stdin = NULL;
conn->pfd[0].fd = -1;
conn->pfd[0].events = POLLIN;
conn->pfd[1].fd = socket(AF_INET, SOCK_STREAM, 0);
conn->pfd[1].events = POLLIN;
if (conn->pfd[1].fd < 0) {
perror("Failed to create socket!");
free(conn);
return NULL;
}
conn->addr.sin_family = AF_INET;
conn->addr.sin_addr.s_addr = inet_addr(saddr);
conn->addr.sin_port = htons(port);
if (connect(conn->pfd[1].fd, (struct sockaddr *)&conn->addr, slen) < 0) {
perror("Failed to connex!");
free(conn);
return NULL;
}
fcntl(conn->pfd[1].fd, F_SETFL, O_NONBLOCK);
return conn;
}
void
client_poll(Conn *conn, int timeout)
{
poll(conn->pfd, 2, timeout);
if ((conn->pfd[0].revents & POLLIN) && conn->on_stdin != NULL) {
static char buf[1024]; // TODO read whole msg
memset((void *)buf, 0, sizeof(char)*1024);
read(conn->pfd[0].fd, buf, 1024);
(*conn->on_stdin)(conn, buf, 1024);
// TODO handle stdin
}
if ((conn->pfd[1].revents & POLLIN) && conn->on_messg != NULL) {
static char buf[1024]; // TODO read whole msg
memset((void *)buf, 0, sizeof(char)*1024);
if (recv(conn->pfd[1].fd, buf, 1024, 0))
(*conn->on_messg)(conn, buf, 1024);
else {
perror("Lost connexion with server");
conn->pfd[1].fd = -1;
if (conn->on_close != NULL)
(*conn->on_close)(conn, NULL, 0);
}
}
}
void
client_send(Conn *conn, const char *msg, unsigned int len)
{
// TODO maybe some checks
if (len > 1024) {
perror("Message too long");
return;
}
send(conn->pfd[1].fd, msg, len, 0);
}
void
client_bind(Conn *conn, int event, void (*fn)(Conn *, const char *, unsigned int))
{
switch (event) {
case ON_MESSG:
conn->on_messg = fn;
break;
case ON_CLOSE:
conn->on_close = fn;
break;
case ON_STDIN:
conn->on_stdin = fn;
if (fn == NULL)
conn->pfd[0].fd = -1;
else
conn->pfd[0].fd = STDIN_FILENO;
break;
}
}
void
client_close(Conn *conn)
{
close(conn->pfd[1].fd);
memset((void *)conn, 0, sizeof(Conn));
free(conn);
}
int
client_alive(Conn *conn)
{
if (!conn)
return 0;
if (conn->pfd[1].fd < 0)
return 0;
return 1;
}