Skip to content

Commit

Permalink
Merge pull request #12 from Mu2e/rrivera/improvedUDPexample
Browse files Browse the repository at this point in the history
Better point management in UDP example
  • Loading branch information
rrivera747 authored Sep 10, 2024
2 parents 402769c + 0ed1622 commit 34f43ab
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 45 deletions.
56 changes: 32 additions & 24 deletions test/ots_udp_hw_emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,33 @@ void* get_in_addr(struct sockaddr* sa)
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int makeSocket(const char* ip, int port, struct addrinfo*& p)
//==============================================================================
int makeSocket(const char* ip, int port, struct sockaddr_in &ai_addr)
{
int sockfd;
struct addrinfo hints, *servinfo;
int rv;
int numberOfBytes;
struct sockaddr_storage their_addr;
socklen_t addr_len;
char s[INET6_ADDRSTRLEN];
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
// int numbytes;
// struct sockaddr_storage their_addr;
// socklen_t addr_len;
// char s[INET6_ADDRSTRLEN];

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
char portStr[10];
sprintf(portStr, "%d", port);
if((rv = getaddrinfo(ip, portStr, &hints, &servinfo)) != 0)

if((rv = getaddrinfo(ip, std::to_string(port).c_str(), &hints, &servinfo)) != 0)
{
__PRINTF__("getaddrinfo: %s\n", gai_strerror(rv));
return -1;
__SS__ << "getaddrinfo: " << gai_strerror(rv) << __E__;
__SS_THROW__;
}

// loop through all the results and make a socket
for(p = servinfo; p != NULL; p = p->ai_next)
{
if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
{
__PRINTF__("sw: socket\n");
__COUT_WARN__ << "sw: socket failed, trying other address..." << __E__;
continue;
}

Expand All @@ -90,14 +90,16 @@ int makeSocket(const char* ip, int port, struct addrinfo*& p)

if(p == NULL)
{
__PRINTF__("sw: failed to create socket\n");
return -1;
__SS__ << "sw: failed to create socket" << __E__;
__SS_THROW__;
}

freeaddrinfo(servinfo);
//copy ai_addr, which is needed for sendto
memcpy(&ai_addr, p->ai_addr, sizeof(ai_addr));

freeaddrinfo(servinfo);
return sockfd;
}
} //end makeSocket()

int main(int argc, char** argv)
{
Expand All @@ -113,7 +115,7 @@ int main(int argc, char** argv)
unsigned short streamToPort;

int sockfd;
int sendSockfd = 0;
int sendSockfd = -1;
struct addrinfo hints, *servinfo, *p;
int rv;
int numberOfBytes;
Expand Down Expand Up @@ -163,6 +165,9 @@ int main(int argc, char** argv)

freeaddrinfo(servinfo);


struct sockaddr_in ai_addr;

//////////////////////////////////////////////////////////////////////
////////////// ready to go //////////////
//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -419,9 +424,10 @@ int main(int argc, char** argv)
<< "Stream destination port: 0x" << streamToPort << dec
<< " " << streamToPort << endl;

close(sendSockfd);
sendSockfd = 0;
sendSockfd = makeSocket(streamToIP.c_str(), streamToPort, p);
if(sendSockfd != -1)
close(sendSockfd);
sendSockfd = -1;
sendSockfd = makeSocket(streamToIP.c_str(), streamToPort, ai_addr);//p);
if(sendSockfd != -1)
{
__COUT__ << "************************************************"
Expand Down Expand Up @@ -515,7 +521,7 @@ int main(int argc, char** argv)
// __COUT__ << value << std::endl;

if((numberOfBytes = sendto(
sendSockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
sendSockfd, buff, packetSz, 0, (struct sockaddr*)&ai_addr, sizeof(ai_addr)) == //p->ai_addr, p->ai_addrlen)) ==
-1)
{
perror("Hw: sendto");
Expand Down Expand Up @@ -548,7 +554,7 @@ int main(int argc, char** argv)
if(sendSockfd != -1)
{
if((numberOfBytes = sendto(
sendSockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
sendSockfd, buff, packetSz, 0, (struct sockaddr*)&ai_addr, sizeof(ai_addr)) == //p->ai_addr, p->ai_addrlen)) ==
-1)
{
perror("hw: sendto");
Expand All @@ -568,6 +574,8 @@ int main(int argc, char** argv)
} // end main loop

close(sockfd);
if(sendSockfd != -1)
close(sendSockfd);

return 0;
}
47 changes: 26 additions & 21 deletions test/ots_udp_sw_emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,33 @@ void* get_in_addr(struct sockaddr* sa)
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int makeSocket(char* ip, int port)
//==============================================================================
int makeSocket(const char* ip, int port, struct sockaddr_in &ai_addr)
{
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
int numbytes;
struct sockaddr_storage their_addr;
socklen_t addr_len;
char s[INET6_ADDRSTRLEN];
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
// int numbytes;
// struct sockaddr_storage their_addr;
// socklen_t addr_len;
// char s[INET6_ADDRSTRLEN];

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;

if((rv = getaddrinfo(ip, HWPORT, &hints, &servinfo)) != 0)
if((rv = getaddrinfo(ip, std::to_string(port).c_str(), &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
__SS__ << "getaddrinfo: " << gai_strerror(rv) << __E__;
__SS_THROW__;
}

// loop through all the results and make a socket
for(p = servinfo; p != NULL; p = p->ai_next)
{
if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
{
perror("sw: socket");
__COUT_WARN__ << "sw: socket failed, trying other address..." << __E__;
continue;
}

Expand All @@ -85,14 +86,16 @@ int makeSocket(char* ip, int port)

if(p == NULL)
{
fprintf(stderr, "sw: failed to create socket\n");
return 2;
__SS__ << "sw: failed to create socket" << __E__;
__SS_THROW__;
}

freeaddrinfo(servinfo);
//copy ai_addr, which is needed for sendto
memcpy(&ai_addr, p->ai_addr, sizeof(ai_addr));

freeaddrinfo(servinfo);
return sockfd;
}
} //end makeSocket()

int main(int argc, char* argv[])
{
Expand Down Expand Up @@ -142,6 +145,8 @@ int main(int argc, char* argv[])
return 2;
}

struct sockaddr_in ai_addr;
memcpy(&ai_addr, p->ai_addr, sizeof(ai_addr));
freeaddrinfo(servinfo);

//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -184,7 +189,7 @@ int main(int argc, char* argv[])
8); // increment each time
packetSz = RX_DATA_OFFSET + buff[1] * 8;

if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
if((numbytes = sendto(sockfd, buff, packetSz, 0, (struct sockaddr*)&ai_addr, sizeof(ai_addr))) == //p->ai_addr, p->ai_addrlen)) ==
-1)
{
perror("sw: sendto");
Expand All @@ -196,7 +201,7 @@ int main(int argc, char* argv[])
// ///////////////////////////////////////////////////////////
packetSz = RX_DATA_OFFSET;

if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
if((numbytes = sendto(sockfd, buff, packetSz, 0, (struct sockaddr*)&ai_addr, sizeof(ai_addr))) == //p->ai_addr, p->ai_addrlen)) ==
-1)
{
perror("sw: sendto");
Expand Down Expand Up @@ -259,7 +264,7 @@ int main(int argc, char* argv[])
memset((void*)&buff[RX_DATA_OFFSET + 7], 1, 1); // enable data
packetSz = RX_DATA_OFFSET + buff[1] * 8;

if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
if((numbytes = sendto(sockfd, buff, packetSz, 0, (struct sockaddr*)&ai_addr, sizeof(ai_addr))) == //p->ai_addr, p->ai_addrlen)) ==
-1)
{
perror("sw: sendto");
Expand All @@ -278,7 +283,7 @@ int main(int argc, char* argv[])
memcpy((void*)&buff[RX_DATA_OFFSET], (void*)&val, 8);
packetSz = RX_DATA_OFFSET + buff[1] * 8;

if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
if((numbytes = sendto(sockfd, buff, packetSz, 0, (struct sockaddr*)&ai_addr, sizeof(ai_addr))) == //p->ai_addr, p->ai_addrlen)) ==
-1)
{
perror("sw: sendto");
Expand Down Expand Up @@ -342,7 +347,7 @@ int main(int argc, char* argv[])
memset((void*)&buff[RX_DATA_OFFSET + 7], 0, 1); // disable data
packetSz = RX_DATA_OFFSET + buff[1] * 8;

if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
if((numbytes = sendto(sockfd, buff, packetSz, 0, (struct sockaddr*)&ai_addr, sizeof(ai_addr))) == //p->ai_addr, p->ai_addrlen)) ==
-1)
{
perror("sw: sendto");
Expand Down

0 comments on commit 34f43ab

Please sign in to comment.