Mercurial > hg > beaglert
changeset 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 | 272154649c46 |
children | 53e57276ac1a |
files | Makefile core/UdpClient.cpp core/client.cpp include/UdpClient.h include/client.h |
diffstat | 5 files changed, 128 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Fri Jul 17 17:41:35 2015 +0100 +++ b/Makefile Fri Jul 17 17:50:54 2015 +0100 @@ -33,6 +33,7 @@ ./core/RTAudio.cpp \ ./core/RTAudioCommandLine.cpp \ ./core/Utilities.cpp \ +./core/UdpClient.cpp \ ./core/client.cpp CORE_OBJS := \ @@ -42,6 +43,7 @@ ./build/core/RTAudio.o \ ./build/core/RTAudioCommandLine.o \ ./build/core/Utilities.o \ +./build/core/UdpClient.o \ ./build/core/client.o CORE_CPP_DEPS := \ @@ -51,6 +53,7 @@ ./build/core/RTAudio.d \ ./build/core/RTAudioCommandLine.d \ ./build/core/Utilities.d \ +./build/core/UdpClient.d \ ./build/core/client.d # Objects for a system-supplied default main() file, if the user
--- /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; + }; +
--- a/core/client.cpp Fri Jul 17 17:41:35 2015 +0100 +++ b/core/client.cpp Fri Jul 17 17:50:54 2015 +0100 @@ -7,7 +7,7 @@ #include <unistd.h> #include <rtdk.h> -#define BUFF_LEN 1024 +#define MESSAGE_BUFF_LEN 1024 #define MAX_VAR_STRING 20 struct sockaddr_in outServer, inServer; @@ -47,13 +47,13 @@ int sendMessage(networkData message) { unsigned int length; - char buffer[BUFF_LEN]; + char buffer[MESSAGE_BUFF_LEN]; length=sizeof(struct sockaddr_in); int k=0; k=sprintf(buffer+k, "%8d;",*message.counter); for(int j=0; j<message.numVariables; j++){ k+=sprintf(buffer+k, "%.3f;",*message.variables[j]); - if(k>BUFF_LEN - 20) //safety margin + if(k>MESSAGE_BUFF_LEN - 20) //safety margin continue; } sprintf(buffer+k,"\n"); @@ -63,16 +63,18 @@ if (n < 0) error("Sendto"); return 0; } -/* -int receiveMessage() + +int sendAudio(networkAudio *audio) { - int n = recvfrom(inSock,inBuffer,1024,0,(struct sockaddr *)&from,&fromlen); - if (n < 0) error("recvfrom"); - printf("Received a datagram: "); - printf(inBuffer); - } -*/ - + unsigned int length; + length=sizeof(struct sockaddr_in); +// for(int k=0; k<NETWORK_AU DIO_BUFFER_SIZE; k++) +// printf("%f\n",audio.buffers[!audio.currentBuffer][k]); + n=sendto(outSock,audio->buffers[!audio->currentBuffer],NETWORK_AUDIO_BUFFER_SIZE*sizeof(float),0,(const struct sockaddr *)&outServer,length); + if (n < 0) error("Sendto"); + audio->doneOnTime=1; + return 0; +} int receiveMessage(networkData message){ struct timeval stTimeOut; @@ -97,11 +99,11 @@ return -1; } printf("Received a datagram: "); - printf("%s", inBuffer); + printf(inBuffer); //the worst parser ever int previousN=0; int currentVariable=0; - for(int n=0; inBuffer[n]!=0 && currentVariable<message.numVariables && n-previousN<MAX_VAR_STRING && n<BUFF_LEN; n++){ //scan the string + for(int n=0; inBuffer[n]!=0 && currentVariable<message.numVariables && n-previousN<MAX_VAR_STRING && n<MESSAGE_BUFF_LEN; n++){ //scan the string if(inBuffer[n]==';'||inBuffer[n]==0||inBuffer[n]=='\n'){ // if you find a separator or you are at the end of the string, parse the variable int j=0; inBuffer[n]=0; //set the semicolon to 0 ...
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/UdpClient.h Fri Jul 17 17:50:54 2015 +0100 @@ -0,0 +1,40 @@ +/* + * udpClient.h + * + * Created on: 19 May 2015 + * Author: giulio moro + */ + +#ifndef UDPCLIENT_H_ +#define UDPCLIENT_H_ + +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +class UdpClient{ + private: + int port; + int enabled; + int outSocket; + bool isSetPort; + bool isSetServer; + struct sockaddr_in destinationServer; + public: + UdpClient(); + UdpClient(int aPort, const char* aServerName); + ~UdpClient(); + void setPort(int aPort); + void setServer(const char* aServerName); + int send(void* message, int size); +}; + + + +#endif /* UDPCLIENT_H_ */
--- a/include/client.h Fri Jul 17 17:41:35 2015 +0100 +++ b/include/client.h Fri Jul 17 17:50:54 2015 +0100 @@ -8,15 +8,27 @@ #include <stdlib.h> #include <unistd.h> #include <string.h> +#include "UdpClient.h" struct networkData{ int *counter; float *variables[16]; int numVariables; }; +#define NETWORK_AUDIO_BUFFER_SIZE 100 //1400/4 //maximum payload for a UDP datagram over ethernet is 1472 bytes, I leave some headroom and divide by 4 to get the number of floats +struct networkAudio{ + int timestamp; + int currentBuffer; + int index; + float buffers[2][NETWORK_AUDIO_BUFFER_SIZE]; + int doneOnTime; + bool toBeSent; + UdpClient udpClient; +}; void error(const char *); int setupSockets(int receivePort, int transmitPort, char const*serverName); int sendMessage(networkData message); +int sendAudio(networkAudio *audio); int receiveMessage(networkData message); void closeSockets();