diff core/UdpClient.cpp @ 70:f3251851c718

Brought UdpClient files over from ultra-staging branch (with include fix), and updated Makefile accordingly
author andrewm
date Fri, 17 Jul 2015 17:50:54 +0100
parents
children 3068421c0737 c42a6b4dc2d4
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/UdpClient.cpp	Fri Jul 17 17:50:54 2015 +0100
@@ -0,0 +1,57 @@
+/*
+ * udpClient.cpp
+ *
+ *  Created on: 19 May 2015
+ *      Author: giulio moro
+ */
+#include "../include/UdpClient.h"
+
+	UdpClient::UdpClient(){
+		outSocket=socket(AF_INET, SOCK_DGRAM, 0);
+		isSetPort=false;
+		isSetServer=false;
+		enabled=false;
+	}
+	UdpClient::UdpClient(int aPort, const char* aServerName){
+		outSocket=socket(AF_INET, SOCK_DGRAM, 0);
+		if(outSocket<0){
+			enabled=false;
+			return;
+		}
+		setPort(aPort);
+		setServer(aServerName);
+		isSetPort=true;
+		isSetServer=true;
+		enabled=true;
+	}
+	UdpClient::~UdpClient(){
+	   close(outSocket);
+	}
+	void UdpClient::setPort(int aPort){
+		port=aPort;
+		destinationServer.sin_port = htons(port);
+		destinationServer.sin_family = AF_INET;
+		isSetPort=true;
+		if(isSetServer){
+			enabled=true;
+		}
+	};
+	void UdpClient::setServer(const char* aServerName){
+		inet_pton(AF_INET,aServerName,&destinationServer.sin_addr);
+		isSetServer=true;
+		if(isSetPort){
+			enabled=true;
+		}
+	};
+	int UdpClient::send(void * message, int size){
+		if(!enabled)
+			return -1;
+		unsigned int length;
+		length=sizeof(struct sockaddr_in);
+		int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length);
+		if (n < 0){
+			return n;
+		}
+		return 1;
+	};
+