aboutsummaryrefslogtreecommitdiff
path: root/protocol.c
blob: 9c8c65b9f9a9616deadacec31e51d890748c8d91 (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
#include <sys/types.h>
#include <sys/socket.h>

#include "protocol.h"

#define TCP_TIMEOUT 100000 // 100ms in microseconds
#define MAX_PACKET_SIZE 512

int send_all(int sock, char *buf, int *len)
{
    struct timeval timeout;      
    timeout.tv_sec = 0;
    timeout.tv_usec = TCP_TIMEOUT;
    // if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
    //             sizeof timeout) < 0)
    //     perror("setsockopt failed\n");

    int total = 0;        // how many bytes we've sent
    int bytesleft = *len; // how many we have left to send
    int n;

    // ensure we don't send more than MAX_PACKET_SIZE bytes
    size_t max_send = bytesleft >= MAX_PACKET_SIZE ? MAX_PACKET_SIZE : bytesleft;
    while(total < *len) {
        n = send(sock, buf+total, max_send, 0);
        if (n == -1) { break; }
        total += n;
        bytesleft -= n;
    }

    *len = total; // return number actually sent here

    return n==-1?-1:0; // return -1 on failure, 0 on success
} 

int recv_all(int sock, char *buf, int *len)
{
    // setup the timeout on the socket
    struct timeval timeout;      
    timeout.tv_sec = 0;
    timeout.tv_usec = TCP_TIMEOUT;
    if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
                sizeof timeout) < 0)
        perror("setsockopt failed\n");

    // printf("start: %ld\n", start);
    int total = 0;        // how many bytes we've sent
    int bytesleft = *len; // how many we have left to send
    int n;

    while(total < *len) {
        n = recv(sock, buf+total, bytesleft, 0);
        // start = time(NULL);
        if (n == -1) { break; }
        total += n;
        bytesleft -= n;
    }

    timeout.tv_sec = 0;
    timeout.tv_usec = 0;
    if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
                sizeof timeout) < 0)
        perror("setsockopt failed\n");


    *len = total; // return number actually sent here

    return n==-1?-1:0; // return -1 on failure, 0 on success
}

int apply_timeout(int fd) {
    // handle handshake
    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = TCP_TIMEOUT;
    if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
        perror("setsockopt (in apply_timeout)");
        return -1;
    }

    return 1;
}

int remove_timeout(int fd)
{
    // handle handshake
    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
        perror("setsockopt (in remove_timeout)");
        return -1;
    }

    return 1;
}