summaryrefslogtreecommitdiff
path: root/ecp/platform/fe310/transport.c
blob: 777a9569627a184b1666dd1f4fe5842f086dd224 (plain)
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
#include <stdlib.h>

#include <core.h>
#include <tr.h>

#include <eos/eos.h>
#include <eos/net.h>

static unsigned char _flags = 0;

ECPSocket *_ecp_tr_sock = NULL;

static void packet_handler(unsigned char type, unsigned char *buffer, uint16_t len) {
    ECPNetAddr addr;

    ECP2Buffer bufs;
    ECPBuffer packet;
    ECPBuffer payload;
    unsigned char pld_buf[ECP_MAX_PLD];

    bufs.packet = &packet;
    bufs.payload = &payload;

    packet.buffer = buffer+EOS_SOCK_SIZE_UDP_HDR;
    packet.size = ECP_MAX_PKT;
    payload.buffer = pld_buf;
    payload.size = ECP_MAX_PLD;

    if ((buffer == NULL) || (len < EOS_SOCK_SIZE_UDP_HDR)) {
        eos_net_free(buffer, 0);
        return;
    }

    eos_sock_getfrom(buffer, &addr);
    ssize_t rv = ecp_pkt_handle(_ecp_tr_sock, &addr, NULL, &bufs, len-EOS_SOCK_SIZE_UDP_HDR);
#ifdef ECP_DEBUG
    if (rv < 0) {
        char b[16];
        puts("ERR:");
        puts(itoa(rv, b, 10));
        puts("\n");
    }
#endif
    if (bufs.packet->buffer) eos_net_free(buffer, 0);
    eos_net_release();
}

int ecp_tr_init(ECPContext *ctx) {
    return ECP_OK;
}

int ecp_tr_addr_eq(ECPNetAddr *addr1, ECPNetAddr *addr2) {
    if (addr1->port != addr2->port) return 0;
    if (memcmp(addr1->host, addr2->host, sizeof(addr1->host)) != 0) return 0;
    return 1;
}

int ecp_tr_addr_set(ECPNetAddr *addr, void *addr_s) {
    return ECP_ERR;
}

int ecp_tr_open(ECPSocket *sock, void *addr_s) {
    sock->sock = eos_sock_open_udp(packet_handler);
    if (sock->sock < 0) {
        sock->sock = 0;
        return ECP_ERR_SEND;
    }
    _ecp_tr_sock = sock;
    return ECP_OK;
}

void ecp_tr_close(ECPSocket *sock) {
    eos_sock_close(sock->sock);
    _ecp_tr_sock = NULL;
}

ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags) {
    unsigned char *buf = NULL;
    int rv;

    flags |= _flags;
    if (packet && packet->buffer) {
        if (flags & ECP_SEND_FLAG_REPLY) {
            buf = packet->buffer-EOS_SOCK_SIZE_UDP_HDR;
            packet->buffer = NULL;
        } else {
            buf = eos_net_alloc();
            memcpy(buf+EOS_SOCK_SIZE_UDP_HDR, packet->buffer, msg_size);
        }
    }
    if (buf == NULL) return ECP_ERR;
    rv = eos_sock_sendto(sock->sock, buf, msg_size+EOS_SOCK_SIZE_UDP_HDR, flags & ECP_SEND_FLAG_MORE, addr);
    if (rv) return ECP_ERR_SEND;
    return msg_size;
}

ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout) {
    return ECP_ERR;
}

void ecp_tr_release(ECPBuffer *packet, unsigned char more) {
    if (packet && packet->buffer) {
        eos_net_free(packet->buffer-EOS_SOCK_SIZE_UDP_HDR, more);
        packet->buffer = NULL;
    } else if (!more) {
        eos_net_release();
    }
}

void ecp_tr_flag_set(unsigned char flags) {
    _flags |= flags;
}

void ecp_tr_flag_clear(unsigned char flags) {
    _flags &= ~flags;
}