cosmetics
[strongswan.git] / src / charon / threads / sender.c
1 /**
2 * @file sender.c
3 *
4 * @brief Implementation of sender_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <stdlib.h>
25 #include <pthread.h>
26
27 #include "sender.h"
28
29 #include <daemon.h>
30 #include <network/socket.h>
31 #include <network/packet.h>
32 #include <queues/send_queue.h>
33
34
35 typedef struct private_sender_t private_sender_t;
36
37 /**
38 * Private data of a sender_t object.
39 */
40 struct private_sender_t {
41 /**
42 * Public part of a sender_t object.
43 */
44 sender_t public;
45
46 /**
47 * Assigned thread.
48 */
49 pthread_t assigned_thread;
50
51 };
52
53 /**
54 * Implementation of private_sender_t.send_packets.
55 */
56 static void send_packets(private_sender_t * this)
57 {
58 packet_t *current_packet;
59 status_t status;
60
61 /* cancellation disabled by default */
62 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
63
64 DBG1(DBG_NET, "sender thread running, thread_ID: %06u",
65 (int)pthread_self());
66
67 while (TRUE)
68 {
69 current_packet = charon->send_queue->get(charon->send_queue);
70 DBG2(DBG_NET, "got a packet, sending it");
71 status = charon->socket->send(charon->socket, current_packet);
72 if (status != SUCCESS)
73 {
74 DBG1(DBG_NET, "sending packet failed");
75 }
76 current_packet->destroy(current_packet);
77 }
78 }
79
80 /**
81 * Implementation of sender_t.destroy.
82 */
83 static void destroy(private_sender_t *this)
84 {
85 pthread_cancel(this->assigned_thread);
86 pthread_join(this->assigned_thread, NULL);
87 free(this);
88 }
89
90 /*
91 * Described in header.
92 */
93 sender_t * sender_create()
94 {
95 private_sender_t *this = malloc_thing(private_sender_t);
96
97 this->public.destroy = (void(*)(sender_t*)) destroy;
98
99 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))send_packets, this) != 0)
100 {
101 free(this);
102 charon->kill(charon, "unable to create sender thread");
103 }
104
105 return &(this->public);
106 }