aboutsummaryrefslogtreecommitdiff
path: root/net/net.c
blob: 0a2dc9829cdfc2f7e1cefc3bb86694484c4f4057 (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
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
161
162
163
164
165
166
167
168
#include <net.h>

void itob (char *buf, int n) {
    buf[0] = (n >> 24) & 0xFF;
    buf[1] = (n >> 16) & 0xFF;
    buf[2] = (n >> 8) & 0xFF;
    buf[3] = n & 0xFF;
}

int btoi (char *buf) {
    int n = 0;
    n += buf[0]*256*256*256;
    n += buf[1]*256*256;
    n += buf[2]*256;
    n += buf[3];
    return n;
}


void net_init () {
    p_libsys_init ();
}

int net_open_socket(PSocket **socket, char ip[], int port) {
    *socket = p_socket_new (P_SOCKET_FAMILY_INET,
        P_SOCKET_TYPE_STREAM,
        P_SOCKET_PROTOCOL_TCP,
        NULL);
    p_socket_set_blocking (*socket, FALSE);
    p_socket_set_timeout (*socket, 10);
    PSocketAddress *sock_addr = p_socket_address_new(ip, port);
    int sender_port = 0;
    if (p_socket_bind (*socket, sock_addr, FALSE, NULL) == FALSE) {
        p_socket_free (*socket);
        p_socket_address_free (sock_addr);
        return 1;
    } else {
        p_socket_address_free (sock_addr);
        PSocketAddress *local_addr = p_socket_get_local_address(*socket, NULL);
        if (local_addr == NULL) {
            p_socket_free (*socket);
            p_uthread_exit (-1);
            return 2;
        }
        sender_port = p_socket_address_get_port (local_addr);
        p_socket_address_free (local_addr);
    }
    return 0;
}

int net_recv (PSocket *socket, char buffer[]) {
    int len = 0;
    for (int i=0; ; i++) {
        psize recv_now = p_socket_receive (socket, 
            buffer+i*1024, 1024, NULL);
        if (recv_now == -1) {
            if (len > 0) { 
                printf("received (%d)\n", len);
                return len; 
            }
            printf("error recv\n");
            return -1;
        } else {
            len += recv_now;
            if (recv_now < 1024) { 
                printf("received (%d)\n", len);
                return len; 
            }
        }
    }
    printf("received (%d)\n", len);
    return len;
}


void net_client_open(net_client *c) {
    int err = net_open_socket(&c->socket, "127.0.0.1", 0);
    if (err > 0) printf("error opening client socket: %d\n", err);
}

int net_client_connect (net_client *c, char ip[], int port) {
    c->addr_server = p_socket_address_new(ip, port);
    if (c->addr_server == NULL) printf("error client making server addr\n");
    int is_connected = p_socket_connect(c->socket, c->addr_server, NULL);
    if (is_connected == FALSE) {
        if (p_socket_io_condition_wait 
            (c->socket, P_SOCKET_IO_CONDITION_POLLOUT, NULL) == TRUE &&
            p_socket_check_connect_result (c->socket, NULL) == FALSE)
        {
            p_socket_address_free (c->addr_server);
            p_socket_free (c->socket);
            printf("error client socket freed\n");
            return 1;
        }
    }

    is_connected = p_socket_is_connected (c->socket);
    if (is_connected == FALSE) {
        printf("error client can't connect to %s:%d\n", ip, port);
        return 1;
    }
    return 0;
}

void net_client_send (net_client *c, char data[], int sizeofdata) {
    if(p_socket_is_connected (c->socket) == FALSE) { 
        printf("error client send lost connection\n");
    }
    PError *err;
    int send_now = p_socket_send (c->socket, data, sizeofdata, &err);
    if (send_now == -1) printf("error: %d\n", p_error_get_code(err));
}

int net_client_recv (net_client *c, char buffer[]) {
    net_recv(c->socket, buffer);
}

void net_client_close (net_client *c) {
    if (c->socket != NULL) p_socket_close (c->socket, NULL);
    if (c->addr_server != NULL) p_socket_address_free (c->addr_server);
	if (c->socket != NULL) p_socket_free (c->socket);
}



void net_server_open(net_server *s, char ip[], int port) {
    int err = net_open_socket(&s->socket, ip, port);
    if (err > 0) printf("error opening server socket: %d\n", err);
    if (p_socket_listen (s->socket, NULL) == FALSE) {
        printf("error server socket listen, nobody in.\n");
        return;
    }
}

void net_server_accept (net_server *s) {
    pboolean poll = p_socket_io_condition_wait (s->socket, 
        P_SOCKET_IO_CONDITION_POLLIN, NULL);
    if (poll == FALSE) {
        printf("server socket, no one in queue\n");
        return;
    }
    s->sock_client = p_socket_accept (s->socket, NULL);
    if (s->sock_client == NULL) {
        printf("server socket, no accept\n");
    }
    p_socket_set_blocking (s->sock_client, FALSE);
    printf("connected\n");
}

int net_server_recv (net_server *s, char buffer[]) {
    net_recv(s->sock_client, buffer);
}

void net_server_send (net_server *s, char data[], int sizeofdata) {
    if(p_socket_is_connected (s->sock_client) == FALSE) { 
        printf("error client send lost connection\n");
        return;
    }
    PError *err;
    int send_now = p_socket_send (s->sock_client, data, sizeofdata, &err);
    if (send_now == -1) printf("error: %d\n", p_error_get_code(err));
}

void net_server_close (net_server *s) {
    if (s->socket != NULL) p_socket_close (s->socket, NULL);
    if (s->sock_client != NULL) p_socket_free (s->sock_client);
	if (s->socket != NULL) p_socket_free(s->socket);
}