summaryrefslogtreecommitdiff
path: root/src/multicast.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/multicast.cc')
-rw-r--r--src/multicast.cc62
1 files changed, 60 insertions, 2 deletions
diff --git a/src/multicast.cc b/src/multicast.cc
index 34c7241..ff3f557 100644
--- a/src/multicast.cc
+++ b/src/multicast.cc
@@ -27,25 +27,83 @@
#include "config.h"
#include "multicast.h"
+#include "miav_config.h"
+
+#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
-#include <sys/socket.h>
#include <sys/param.h>
#include <arpa/inet.h>
+#include <sys/types.h>
+
+// For IP_MTU
+//#include <linux/in.h>
+//#ifndef IP_MTU
+//#define IP_MTU 14
+//#endif
+
+#include <errno.h>
Multicast::Multicast(Info *i, char *addr, int port)
{
info = i;
+ udp_buffer = NULL;
+
+ // Open connection socket
if(!UDPOpen(addr, port)) info->error("Error creating socket %s:%d", addr, port);
+
+ int mtu = config->readInt("udp_packet_size");
+
+ // Create buffer with the size of MTU
+ // socklen_t mtu_sz;
+ // if(getsockopt(sock, SOL_IP, IP_MTU, &mtu, &mtu_sz) != -1) {
+
+ udp_buffer_size = mtu - 28;
+ if(udp_buffer_size < 1) udp_buffer_size = 1;
+ udp_buffer = new char[udp_buffer_size];
+ udp_buffer_pointer = udp_buffer;
+ info->info("UDP packet buffer size %db", udp_buffer_size);
+
+ //} else {
+ // info->error("Error getting MTU size from socket: %s", strerror(errno));
+ // return;
+ //}
}
Multicast::~Multicast()
{
+ if(udp_buffer) delete udp_buffer;
}
void Multicast::Write(void* buf, int size)
{
- if(write(sock, buf, size) != size) info->error("Error Writing to socket.");
+ if(!udp_buffer) return; // no buffer to write in... better break out!
+
+ // info->info("To send: %d", size);
+
+ char *p = (char*)buf;
+ int left = udp_buffer_size - (udp_buffer_pointer - udp_buffer);
+
+ while(size) {
+ int to_copy = size > left ? left : size;
+
+ memcpy(udp_buffer_pointer, p, to_copy);
+
+ left-=to_copy;
+ udp_buffer_pointer += to_copy;
+
+ p+=to_copy;
+ size-=to_copy;
+
+ // info->info("Copied %d - %d to go", to_copy, size);
+
+ if(left == 0) {
+ // info->info("Sending full packet");
+ write(sock, udp_buffer, udp_buffer_size);
+ left = udp_buffer_size;
+ udp_buffer_pointer = udp_buffer;
+ }
+ }
}
bool Multicast::is_address_multicast(unsigned long address)