6 #include <sys/socket.h>
12 #include "mutexlock.h"
13 #include "serverpool.h"
21 extern ServerPool *servers;
23 UDPInput::UDPInput(const string &url)
27 // Should be verified by the caller.
29 bool ok = parse_url(url, &protocol, &host, &port, &path);
34 pthread_mutex_init(&stats_mutex, NULL);
36 stats.bytes_received = 0;
37 stats.data_bytes_received = 0;
40 UDPInput::UDPInput(const InputProto &serialized)
41 : url(serialized.url()),
42 sock(serialized.sock())
44 // Should be verified by the caller.
46 bool ok = parse_url(url, &protocol, &host, &port, &path);
51 pthread_mutex_init(&stats_mutex, NULL);
53 stats.bytes_received = serialized.bytes_received();
54 stats.data_bytes_received = serialized.data_bytes_received();
57 InputProto UDPInput::serialize() const
59 InputProto serialized;
60 serialized.set_url(url);
61 serialized.set_sock(sock);
62 serialized.set_bytes_received(stats.bytes_received);
63 serialized.set_data_bytes_received(stats.data_bytes_received);
67 void UDPInput::close_socket()
73 void UDPInput::construct_header()
77 "Content-type: application/octet-stream\r\n"
78 "Cache-control: no-cache\r\n"
79 "Server: " SERVER_IDENTIFICATION "\r\n"
80 "Connection: close\r\n";
83 void UDPInput::add_destination(int stream_index)
85 stream_indices.push_back(stream_index);
86 servers->set_header(stream_index, http_header, "");
89 void UDPInput::do_work()
91 while (!should_stop()) {
93 int port_num = atoi(port.c_str());
94 sock = create_server_socket(port_num, UDP_SOCKET);
96 log(WARNING, "[%s] UDP socket creation failed. Waiting 0.2 seconds and trying again...",
103 // Wait for a packet, or a wakeup.
104 bool activity = wait_for_activity(sock, POLLIN, NULL);
106 // Most likely, should_stop was set.
112 ret = recv(sock, packet_buf, sizeof(packet_buf), 0);
113 } while (ret == -1 && errno == EINTR);
122 MutexLock lock(&stats_mutex);
123 stats.bytes_received += ret;
124 stats.data_bytes_received += ret;
127 for (size_t i = 0; i < stream_indices.size(); ++i) {
128 servers->add_data(stream_indices[i], packet_buf, ret, SUITABLE_FOR_STREAM_START);
133 InputStats UDPInput::get_stats() const
135 MutexLock lock(&stats_mutex);