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
|
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "eos.h"
#include "event.h"
#include "net.h"
#include "sock.h"
static eos_evt_fptr_t evt_handler[EOS_SOCK_MAX_SOCK];
static uint16_t evt_handler_flags_buf_free = 0;
static uint16_t evt_handler_flags_buf_acq = 0;
static void sock_handler_evt(unsigned char type, unsigned char *buffer, uint16_t len) {
if ((buffer == NULL) || (len < 2)) {
eos_evtq_bad_handler(type, buffer, len);
eos_net_free(buffer, 0);
return;
}
if (buffer[0] == EOS_SOCK_MTYPE_PKT) {
uint8_t sock = buffer[1];
if (sock && (sock <= EOS_SOCK_MAX_SOCK)) {
sock--;
} else {
eos_evtq_bad_handler(type, buffer, len);
eos_net_free(buffer, 0);
return;
}
_eos_net_handle(type, buffer, len, sock, evt_handler, &evt_handler_flags_buf_free, &evt_handler_flags_buf_acq);
}
}
void eos_sock_init(void) {
int i;
for (i=0; i<EOS_SOCK_MAX_SOCK; i++) {
evt_handler[i] = eos_evtq_bad_handler;
}
eos_net_set_handler(EOS_NET_MTYPE_SOCK, sock_handler_evt, 0);
}
void eos_sock_set_handler(int sock, eos_evt_fptr_t handler, uint8_t flags) {
if (sock && (sock <= EOS_SOCK_MAX_SOCK)) {
sock--;
} else {
return;
}
_eos_net_set_handler(sock, handler, evt_handler, flags, &evt_handler_flags_buf_free, &evt_handler_flags_buf_acq);
}
int eos_sock_open_udp(void) {
unsigned char type = EOS_SOCK_MTYPE_OPEN_DGRAM;
unsigned char *buffer = eos_net_alloc();
uint16_t buf_size;
int rv, sock;
buffer[0] = type;
rv = eos_net_send(EOS_NET_MTYPE_SOCK, buffer, 1, 0);
if (rv) return rv;
eos_evtq_get(EOS_NET_MTYPE_SOCK, &type, 1, &buffer, &buf_size);
if (buf_size < 2) {
eos_net_free(buffer, 0);
return EOS_ERR_NET;
}
sock = buffer[1];
eos_net_free(buffer, 1);
if (sock == 0) return EOS_ERR_NET;
return sock;
}
void eos_sock_close(int sock) {
unsigned char *buffer = eos_net_alloc();
buffer[0] = EOS_SOCK_MTYPE_CLOSE;
buffer[1] = sock;
eos_net_send(EOS_NET_MTYPE_SOCK, buffer, 2, 1);
}
int eos_sock_sendto(int sock, unsigned char *buffer, uint16_t size, unsigned char more, EOSNetAddr *addr) {
unsigned char type = EOS_NET_MTYPE_SOCK;
buffer[0] = EOS_SOCK_MTYPE_PKT;
buffer[1] = sock;
memcpy(buffer+2, addr->host, sizeof(addr->host));
memcpy(buffer+2+sizeof(addr->host), &addr->port, sizeof(addr->port));
return eos_net_send(type, buffer, size, more);
}
void eos_sock_getfrom(unsigned char *buffer, EOSNetAddr *addr) {
memcpy(addr->host, buffer+2, sizeof(addr->host));
memcpy(&addr->port, buffer+2+sizeof(addr->host), sizeof(addr->port));
}
|