# HG changeset patch # User andrewm # Date 1437151854 -3600 # Node ID f3251851c71874225bfef92357332ff34a0be5b5 # Parent 272154649c466869b692f4884a42875476247e5e Brought UdpClient files over from ultra-staging branch (with include fix), and updated Makefile accordingly diff -r 272154649c46 -r f3251851c718 Makefile --- 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 diff -r 272154649c46 -r f3251851c718 core/UdpClient.cpp --- /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; + }; + diff -r 272154649c46 -r f3251851c718 core/client.cpp --- 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 #include -#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; jBUFF_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; kbuffers[!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 +#include +#include +#include +#include +#include +#include +#include +#include + +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_ */ diff -r 272154649c46 -r f3251851c718 include/client.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 #include #include +#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();