annotate 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 |
rev |
line source |
andrewm@70
|
1 /*
|
andrewm@70
|
2 * udpClient.cpp
|
andrewm@70
|
3 *
|
andrewm@70
|
4 * Created on: 19 May 2015
|
andrewm@70
|
5 * Author: giulio moro
|
andrewm@70
|
6 */
|
andrewm@70
|
7 #include "../include/UdpClient.h"
|
andrewm@70
|
8
|
andrewm@70
|
9 UdpClient::UdpClient(){
|
andrewm@70
|
10 outSocket=socket(AF_INET, SOCK_DGRAM, 0);
|
andrewm@70
|
11 isSetPort=false;
|
andrewm@70
|
12 isSetServer=false;
|
andrewm@70
|
13 enabled=false;
|
andrewm@70
|
14 }
|
andrewm@70
|
15 UdpClient::UdpClient(int aPort, const char* aServerName){
|
andrewm@70
|
16 outSocket=socket(AF_INET, SOCK_DGRAM, 0);
|
andrewm@70
|
17 if(outSocket<0){
|
andrewm@70
|
18 enabled=false;
|
andrewm@70
|
19 return;
|
andrewm@70
|
20 }
|
andrewm@70
|
21 setPort(aPort);
|
andrewm@70
|
22 setServer(aServerName);
|
andrewm@70
|
23 isSetPort=true;
|
andrewm@70
|
24 isSetServer=true;
|
andrewm@70
|
25 enabled=true;
|
andrewm@70
|
26 }
|
andrewm@70
|
27 UdpClient::~UdpClient(){
|
andrewm@70
|
28 close(outSocket);
|
andrewm@70
|
29 }
|
andrewm@70
|
30 void UdpClient::setPort(int aPort){
|
andrewm@70
|
31 port=aPort;
|
andrewm@70
|
32 destinationServer.sin_port = htons(port);
|
andrewm@70
|
33 destinationServer.sin_family = AF_INET;
|
andrewm@70
|
34 isSetPort=true;
|
andrewm@70
|
35 if(isSetServer){
|
andrewm@70
|
36 enabled=true;
|
andrewm@70
|
37 }
|
andrewm@70
|
38 };
|
andrewm@70
|
39 void UdpClient::setServer(const char* aServerName){
|
andrewm@70
|
40 inet_pton(AF_INET,aServerName,&destinationServer.sin_addr);
|
andrewm@70
|
41 isSetServer=true;
|
andrewm@70
|
42 if(isSetPort){
|
andrewm@70
|
43 enabled=true;
|
andrewm@70
|
44 }
|
andrewm@70
|
45 };
|
andrewm@70
|
46 int UdpClient::send(void * message, int size){
|
andrewm@70
|
47 if(!enabled)
|
andrewm@70
|
48 return -1;
|
andrewm@70
|
49 unsigned int length;
|
andrewm@70
|
50 length=sizeof(struct sockaddr_in);
|
andrewm@70
|
51 int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length);
|
andrewm@70
|
52 if (n < 0){
|
andrewm@70
|
53 return n;
|
andrewm@70
|
54 }
|
andrewm@70
|
55 return 1;
|
andrewm@70
|
56 };
|
andrewm@70
|
57
|