socket.c 11 KB
Newer Older
1
2
3
4
5
6
#include <arpa/inet.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
7
#include <sys/poll.h>
8
#include <assert.h>
9
10
11
12
#include "../defines.h"
#include "packet.h"
#include "../util/dbg.h"
#include "../util/common.h"
13
#include "processes/cleaner.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
14
15
#include "processes/dataTransmitter.h"
#include "processes/dataReceiver.h"
16
#include "processes/feedbackReceiver.h"
17
#include "socket.h"
18
#include "receiver.h"
19

20
PrrtSocket *PrrtSocket_create(const bool is_sender)
Andreas Schmidt's avatar
Andreas Schmidt committed
21
{
22
    assert(sizeof(float) == 4);
23
    PrrtSocket *sock_ptr = (PrrtSocket*) calloc(1, sizeof(PrrtSocket));
24
    check_mem(sock_ptr);
25

26

27
    sock_ptr->isSender = is_sender;
28

29
30
    sock_ptr->clock = PrrtClock_create();

Andreas Schmidt's avatar
Andreas Schmidt committed
31
    sock_ptr->isBound = false;
32

33
34
    sock_ptr->sequenceNumberSource = 1;
    sock_ptr->sequenceNumberRedundancy = 1;
35

36
    sock_ptr->csi = PrrtChannelStateInformation_create();
37
    sock_ptr->applicationConstraints = PrrtApplicationConstraints_create();
38

39
40
    sock_ptr->packetTimeoutTable = PrrtPacketTimeoutTable_create();

41
    sock_ptr->dataPacketStore = PrrtDataPacketStore_create();
42

43
44
    check(sock_ptr->dataSocketFd = socket(AF_INET, SOCK_DGRAM, 0), "Cannot create data socket.")
    check(sock_ptr->feedbackSocketFd = socket(AF_INET, SOCK_DGRAM, 0), "Cannot create feedback socket.");
45

46
    pthread_mutexattr_t attr;
47
48
49
    check(pthread_mutexattr_init(&attr) == 0, "Mutex attr init failed.");
    check(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) == 0, "Setting type failed.");
    check(pthread_mutex_init(&sock_ptr->closingMutex, &attr) == 0, "Mutex init failed.");
50

51
    if (is_sender) {
52
53
        check(pthread_mutex_init(&sock_ptr->outQueueFilledMutex, NULL) == 0, "Mutex init failed.");
        check(pthread_cond_init(&sock_ptr->outQueueFilledCv, NULL) == 0, "Cond init failed.");
54
        sock_ptr->outQueue = List_create();
55

56
        sock_ptr->receivers = List_create();
57
    } else {
Andreas Schmidt's avatar
Andreas Schmidt committed
58
        sock_ptr->forwardPacketTable = PrrtForwardPacketTable_create();
59
        sock_ptr->repairBlockStore = PrrtRepairBlockStore_create();
60

61
62
        check(pthread_mutex_init(&sock_ptr->inQueueFilledMutex, NULL) == 0, "Mutex init failed.");
        check(pthread_cond_init(&sock_ptr->inQueueFilledCv, NULL) == 0, "Cond init failed.");
63
        sock_ptr->inQueue = List_create();
64
65
    }

66
    return sock_ptr;
67
68

    error:
69
        PrrtSocket_close(sock_ptr);
70
        return NULL;
71
72
}

Andreas Schmidt's avatar
Andreas Schmidt committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
bool PrrtSocket_bind(PrrtSocket *sock_ptr, const char* ipAddress, const uint16_t port) {
    check(port <= 65534, "Port %d cannot be bound to.", port);

    size_t size = sizeof(struct sockaddr_in);
    struct sockaddr_in* address = calloc(1, size);
    check_mem(address);

    address->sin_family = AF_INET;
    address->sin_addr.s_addr = inet_addr(ipAddress);
    address->sin_port = htons((uint16_t) (port + 1));
    sock_ptr->address = address;

    check(bind(sock_ptr->feedbackSocketFd, (struct sockaddr *) address, size) == EXIT_SUCCESS,
          "Cannot bind feedback socket.");

    address->sin_port = htons((uint16_t) (port));

    check(bind(sock_ptr->dataSocketFd, (struct sockaddr *) address, size) == EXIT_SUCCESS,
          "Cannot bind data socket.");

93
    if(sock_ptr->isSender) {
Andreas Schmidt's avatar
Andreas Schmidt committed
94
95
        check(pthread_create(&sock_ptr->receiveFeedbackThread, NULL, receive_feedback_loop, (void *) sock_ptr) ==
              EXIT_SUCCESS, "Cannot create receive feedback thread.");
Andreas Schmidt's avatar
Andreas Schmidt committed
96
        check(pthread_create(&sock_ptr->sendDataThread, NULL, send_data_loop, (void *) sock_ptr) == EXIT_SUCCESS,
Andreas Schmidt's avatar
Andreas Schmidt committed
97
98
99
100
101
102
              "Cannot create send thread.");
    } else {
        check(pthread_create(&sock_ptr->receiveDataThread, NULL, receive_data_loop, (void *) sock_ptr) == EXIT_SUCCESS,
              "Cannot create data receiving thread.");
    }

103
    check(pthread_create(&sock_ptr->cleanupThread, NULL, cleanup, (void *) sock_ptr) == EXIT_SUCCESS, "Cannot create cleanup thread.");
104

Andreas Schmidt's avatar
Andreas Schmidt committed
105
106
107
108
109
110
    return true;
    error:
    PrrtSocket_close(sock_ptr);
    return false;
}

111
int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t port) {
112
    PrrtReceiver *recv = PrrtReceiver_create(host, port);
113
    List_push(sock_ptr->receivers, recv);
114
115
116
    return 0;
}

117
int PrrtSocket_send(PrrtSocket *sock_ptr, const uint8_t *data, const size_t data_len) {
118
    check(sock_ptr->isSender, "Cannot send on receiver socket.")
119
    check(pthread_mutex_lock(&sock_ptr->outQueueFilledMutex) == 0, "Lock failed.");
120

121
    PrrtPacket *packet = PrrtPacket_create_data_packet(5, data, (prrtPacketLength_t) data_len, 0, PrrtApplicationConstraints_get_target_delay(sock_ptr->applicationConstraints));
122

123
    List_push(sock_ptr->outQueue, packet);
124
125
    check(pthread_cond_signal(&sock_ptr->outQueueFilledCv) == 0, "Signal failed.");
    check(pthread_mutex_unlock(&sock_ptr->outQueueFilledMutex) == 0, "Unlock failed");
126

127
    return 0;
128
    error:
129
        PERROR("There was a failure while sending from socket.%s", "");
130
        return -1;
131
132
}

133
int32_t PrrtSocket_recv(PrrtSocket *sock_ptr, void *buf_ptr) {
134
    check(sock_ptr->isSender == false, "Cannot receive on sender socket.")
135
    while (1) {
136
        check(pthread_mutex_lock(&sock_ptr->inQueueFilledMutex) == 0, "Lock failed.");
137
        while (List_count(sock_ptr->inQueue) == 0) {
138
            check(pthread_mutex_lock(&sock_ptr->closingMutex) == 0, "Lock failed.");
139
            if (sock_ptr->closing) {
140
141
                check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
                check(pthread_mutex_unlock(&sock_ptr->inQueueFilledMutex) == 0, "Unlock failed.");
142
143
                return -1;
            }
144
145
            check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
            check(pthread_cond_wait(&sock_ptr->inQueueFilledCv, &sock_ptr->inQueueFilledMutex) == 0, "Wait failed.");
146
        }
147
        PrrtPacket *packet = List_shift(sock_ptr->inQueue);
148

149
        prrtPacketLength_t len = (prrtPacketLength_t) (packet->payloadLength - PRRT_PACKET_DATA_HEADER_SIZE);
150
        PrrtPacket_copy_payload_to_buffer(buf_ptr, packet, PRRT_PACKET_DATA_HEADER_SIZE);
151
152
        PrrtPacket_destroy(packet);

153
        check(pthread_mutex_unlock(&sock_ptr->inQueueFilledMutex) == 0, "Unlock failed.");
154
        return len;
155
    }
156
    error:
157
        PERROR("There was a failure while receiving from socket.%s", "");
158
        return -1;
159
160
}

161
int PrrtSocket_interrupt(PrrtSocket *sock_ptr) {
162
    check(pthread_mutex_lock(&sock_ptr->closingMutex) == 0, "Lock failed.");
163
    sock_ptr->closing = true;
164
    check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
165

166
    void **res = NULL;
Andreas Schmidt's avatar
Andreas Schmidt committed
167
    if (sock_ptr->sendDataThread != 0) {
168
169
170
        check(pthread_mutex_lock(&sock_ptr->outQueueFilledMutex) == 0, "Lock failed.");
        check(pthread_cond_broadcast(&sock_ptr->outQueueFilledCv) == 0, "Broadcast failed.");
        check(pthread_mutex_unlock(&sock_ptr->outQueueFilledMutex) == 0, "Unlock failed.");
171

Andreas Schmidt's avatar
Andreas Schmidt committed
172
173
        check(pthread_join(sock_ptr->sendDataThread, res) == 0, "Join failed.");
        sock_ptr->sendDataThread = 0;
174
175
    }

176
    if (sock_ptr->receiveDataThread != 0) {
177
178
179
        check(pthread_mutex_lock(&sock_ptr->inQueueFilledMutex) == 0, "Lock failed.");
        check(pthread_cond_broadcast(&sock_ptr->inQueueFilledCv) == 0, "Broadcast failed");
        check(pthread_mutex_unlock(&sock_ptr->inQueueFilledMutex) == 0, "Unlock failed");
180

181
182
        check(pthread_cancel(sock_ptr->receiveDataThread) == 0, "Cancel failed.");
        check(pthread_join(sock_ptr->receiveDataThread, res) == 0, "Join failed.");
183
184
185
186

        sock_ptr->receiveDataThread = 0;
    }

187
    if (sock_ptr->receiveFeedbackThread != 0) {
188
        check(pthread_join(sock_ptr->receiveFeedbackThread, res) == 0, "Join failed.");
189
190
191

        sock_ptr->receiveFeedbackThread = 0;
    }
192
193
194
195
196
197
198
199

    if(sock_ptr->cleanupThread != 0) {
        check(pthread_cancel(sock_ptr->cleanupThread) == 0, "Cancel failed.");
        check(pthread_join(sock_ptr->cleanupThread, res) == 0, "Join failed.");

        sock_ptr->cleanupThread = 0;
    }

Andreas Schmidt's avatar
Andreas Schmidt committed
200
    return EXIT_SUCCESS;
201
202
203

    error:
        return EXIT_FAILURE;
204
205
206
}

int PrrtSocket_close(PrrtSocket *sock_ptr) {
207
    debug("Closing socket.");
208
    check(pthread_mutex_lock(&sock_ptr->closingMutex) == 0, "Lock failed.");
209
    if (!sock_ptr->closing) {
210
211
        check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
        check(PrrtSocket_interrupt(sock_ptr) == EXIT_SUCCESS, "Interrupt failed.");
212
    } else {
213
        check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
214
215
    }

216
217
218
    if(sock_ptr->dataPacketStore != NULL) {
        check(PrrtDataPacketStore_destroy(sock_ptr->dataPacketStore), "Destroy failed.");
        sock_ptr->dataPacketStore = NULL;
219
220
    }

221
222
223
    if(sock_ptr->repairBlockStore != NULL) {
        PrrtRepairBlockStore_destroy(sock_ptr->repairBlockStore);
        sock_ptr->repairBlockStore = NULL;
224
225
    }

226
    if (sock_ptr->receivers != NULL) {
227
        while (List_count(sock_ptr->receivers) > 0) {
Andreas Schmidt's avatar
Andreas Schmidt committed
228
            PrrtReceiver *recv = List_shift(sock_ptr->receivers);
229
            PrrtReceiver_destroy(recv);
230
231
        }
        List_destroy(sock_ptr->receivers);
232
        sock_ptr->receivers = NULL;
233
234
    }

235
    if (sock_ptr->outQueue != NULL) {
236
237
        check(pthread_mutex_destroy(&sock_ptr->outQueueFilledMutex) == 0, "Mutex destroy failed.");
        check(pthread_cond_destroy(&sock_ptr->outQueueFilledCv) == 0, "Cond destroy failed.");
238
        List_destroy(sock_ptr->outQueue);
239
        sock_ptr->outQueue = NULL;
240
241
    }

242
    if (sock_ptr->inQueue != NULL) {
243
244
        check(pthread_mutex_destroy(&sock_ptr->inQueueFilledMutex) == 0, "Mutex destroy failed.");
        check(pthread_cond_destroy(&sock_ptr->inQueueFilledCv) == 0, "Cond destroy failed.");
245
        List_destroy(sock_ptr->inQueue);
246
247
248
        sock_ptr->inQueue = NULL;
    }

249
    if (sock_ptr->forwardPacketTable != NULL) {
250
        check(PrrtForwardPacketTable_destroy(sock_ptr->forwardPacketTable), "Destroy failed.");
251
        sock_ptr->forwardPacketTable = NULL;
252
    }
253

254
255
256
257
258
    if(sock_ptr->packetTimeoutTable != NULL) {
        check(PrrtPacketTimeoutTable_destroy(sock_ptr->packetTimeoutTable), "Destroy failed.");
        sock_ptr->packetTimeoutTable = NULL;
    }

259
    check(pthread_mutex_destroy(&sock_ptr->closingMutex) == 0, "Mutex destroy failed.");
260

Andreas Schmidt's avatar
Andreas Schmidt committed
261
262
263
264
    if(sock_ptr->address != NULL) {
        free(sock_ptr->address);
    }

265
266
267
268
269
270
271
272
    if(sock_ptr->csi != NULL) {
        check(PrrtChannelStateInformation_destroy(sock_ptr->csi), "Could not destroy channel state information.")
    }

    if(sock_ptr->applicationConstraints) {
        check(PrrtApplicationConstraints_destroy(sock_ptr->applicationConstraints), "Could not destroy application constraints.")
    }

273
274
275
    if(sock_ptr->clock != NULL) {
        check(PrrtClock_destroy(sock_ptr->clock), "Destroy clock failed");
    }
276

277
278
    close(sock_ptr->dataSocketFd);
    close(sock_ptr->feedbackSocketFd);
279
    debug("Socket closed.");
280
    return 0;
281
282
283
284

    error:
        PERROR("Closing socket failed.%s", "");
        return -1;
285
286
}

287
288
289
bool PrrtSocket_set_sock_opt(PrrtSocket *sock_ptr, const char *name, const uint32_t value)
{
    if(strcmp(name, "targetdelay") == 0) {
290
        PrrtApplicationConstraints_set_target_delay(sock_ptr->applicationConstraints, value);
291
292
293
294
295
296
    } else {
        return false;
    }

    return true;
}