Skip to content

Commit

Permalink
[apps] Clean up sendfile and recvfile examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsharabayko committed May 3, 2022
1 parent 886843e commit d618c30
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 37 deletions.
16 changes: 6 additions & 10 deletions examples/recvfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int main(int argc, char* argv[])
return -1;
}

// use this function to initialize the UDT library
// Use this function to initialize the UDT library
srt_startup();

srt_setloglevel(srt_logging::LogLevel::debug);
Expand All @@ -46,7 +46,7 @@ int main(int argc, char* argv[])
return -1;
}

// connect to the server, implicit bind
// Connect to the server, implicit bind.
if (SRT_ERROR == srt_connect(fhandle, peer->ai_addr, peer->ai_addrlen))
{
cout << "connect: " << srt_getlasterror_str() << endl;
Expand All @@ -55,8 +55,7 @@ int main(int argc, char* argv[])

freeaddrinfo(peer);


// send name information of the requested file
// Send name information of the requested file.
int len = strlen(argv[3]);

if (SRT_ERROR == srt_send(fhandle, (char*)&len, sizeof(int)))
Expand All @@ -71,7 +70,7 @@ int main(int argc, char* argv[])
return -1;
}

// get size information
// Get size information.
int64_t size;

if (SRT_ERROR == srt_recv(fhandle, (char*)&size, sizeof(int64_t)))
Expand All @@ -86,8 +85,7 @@ int main(int argc, char* argv[])
return -1;
}

// receive the file
//fstream ofs(argv[4], ios::out | ios::binary | ios::trunc);
// Receive the file.
int64_t recvsize;
int64_t offset = 0;

Expand All @@ -108,9 +106,7 @@ int main(int argc, char* argv[])

srt_close(fhandle);

//ofs.close();

// use this function to release the UDT library
// Signal to the SRT library to clean up all allocated sockets and resources.
srt_cleanup();

return 0;
Expand Down
44 changes: 17 additions & 27 deletions examples/sendfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@ DWORD WINAPI sendfile(LPVOID);

int main(int argc, char* argv[])
{
//usage: sendfile [server_port]
if ((2 < argc) || ((2 == argc) && (0 == atoi(argv[1]))))
{
cout << "usage: sendfile [server_port]" << endl;
return 0;
}

// use this function to initialize the UDT library
// Initialize the SRT library.
srt_startup();

srt_setloglevel(srt_logging::LogLevel::debug);

addrinfo hints;
addrinfo* res;

Expand Down Expand Up @@ -66,9 +63,6 @@ int main(int argc, char* argv[])
srt_setsockopt(serv, 0, SRTO_MSS, &mss, sizeof(int));
#endif

//int64_t maxbw = 5000000;
//srt_setsockopt(serv, 0, SRTO_MAXBW, &maxbw, sizeof maxbw);

if (SRT_ERROR == srt_bind(serv, res->ai_addr, res->ai_addrlen))
{
cout << "bind: " << srt_getlasterror_str() << endl;
Expand All @@ -78,14 +72,14 @@ int main(int argc, char* argv[])
freeaddrinfo(res);

cout << "server is ready at port: " << service << endl;

srt_listen(serv, 10);

sockaddr_storage clientaddr;
int addrlen = sizeof(clientaddr);

SRTSOCKET fhandle;

// Accept multiple client connections.
while (true)
{
if (SRT_INVALID_SOCK == (fhandle = srt_accept(serv, (sockaddr*)&clientaddr, &addrlen)))
Expand All @@ -99,18 +93,18 @@ int main(int argc, char* argv[])
getnameinfo((sockaddr *)&clientaddr, addrlen, clienthost, sizeof(clienthost), clientservice, sizeof(clientservice), NI_NUMERICHOST|NI_NUMERICSERV);
cout << "new connection: " << clienthost << ":" << clientservice << endl;

#ifndef _WIN32
pthread_t filethread;
pthread_create(&filethread, NULL, sendfile, new SRTSOCKET(fhandle));
pthread_detach(filethread);
#else
CreateThread(NULL, 0, sendfile, new SRTSOCKET(fhandle), 0, NULL);
#endif
#ifndef _WIN32
pthread_t filethread;
pthread_create(&filethread, NULL, sendfile, new SRTSOCKET(fhandle));
pthread_detach(filethread);
#else
CreateThread(NULL, 0, sendfile, new SRTSOCKET(fhandle), 0, NULL);
#endif
}

srt_close(serv);

// use this function to release the UDT library
// Signal to the SRT library to clean up all allocated sockets and resources.
srt_cleanup();

return 0;
Expand All @@ -125,7 +119,7 @@ DWORD WINAPI sendfile(LPVOID usocket)
SRTSOCKET fhandle = *(SRTSOCKET*)usocket;
delete (SRTSOCKET*)usocket;

// aquiring file name information from client
// Acquiring file name information from client.
char file[1024];
int len;

Expand All @@ -142,15 +136,13 @@ DWORD WINAPI sendfile(LPVOID usocket)
}
file[len] = '\0';

// open the file (only to check the size)
// Open the file only to know its size.
fstream ifs(file, ios::in | ios::binary);

ifs.seekg(0, ios::end);
int64_t size = ifs.tellg();
//ifs.seekg(0, ios::beg);
const int64_t size = ifs.tellg();
ifs.close();

// send file size information
// Send file size.
if (SRT_ERROR == srt_send(fhandle, (char*)&size, sizeof(int64_t)))
{
cout << "send: " << srt_getlasterror_str() << endl;
Expand All @@ -160,7 +152,7 @@ DWORD WINAPI sendfile(LPVOID usocket)
SRT_TRACEBSTATS trace;
srt_bstats(fhandle, &trace, true);

// send the file
// Send the file itself.
int64_t offset = 0;
if (SRT_ERROR == srt_sendfile(fhandle, file, &offset, size, SRT_DEFAULT_SENDFILE_BLOCK))
{
Expand All @@ -170,13 +162,11 @@ DWORD WINAPI sendfile(LPVOID usocket)

srt_bstats(fhandle, &trace, true);
cout << "speed = " << trace.mbpsSendRate << "Mbits/sec" << endl;
int losspercent = 100*trace.pktSndLossTotal/trace.pktSent;
cout << "loss = " << trace.pktSndLossTotal << "pkt (" << losspercent << "%)\n";
const int64_t losspercent = 100 * trace.pktSndLossTotal / trace.pktSent;
cout << "network loss = " << trace.pktSndLossTotal << "pkts (" << losspercent << "%)\n";

srt_close(fhandle);

//ifs.close();

#ifndef _WIN32
return NULL;
#else
Expand Down

0 comments on commit d618c30

Please sign in to comment.