From 1099eeae24b28306022ae3663a2766261b3455e4 Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 18 Oct 2005 12:07:40 +0000 Subject: *** empty log message *** --- src/multicast.cc | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) (limited to 'src/multicast.cc') 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 #include #include -#include #include #include +#include + +// For IP_MTU +//#include +//#ifndef IP_MTU +//#define IP_MTU 14 +//#endif + +#include 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) -- cgit v1.2.3