Skip to content

Commit

Permalink
Applied changes in the API to make it universal (#121)
Browse files Browse the repository at this point in the history
* Applied changes in the API to make it universal
  • Loading branch information
ethouris authored and rndi committed Oct 10, 2017
1 parent 94f8db0 commit 4b897ba
Show file tree
Hide file tree
Showing 13 changed files with 926 additions and 269 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,16 @@ if ( ENABLE_CXX11 )
)
endif()

add_executable(sendfile
${CMAKE_SOURCE_DIR}/apps/legacy/sendfile.cpp
)

add_executable(recvfile
${CMAKE_SOURCE_DIR}/apps/legacy/recvfile.cpp
)
target_link_libraries(sendfile ${TARGET_srt} ${DEPENDS_srt})
target_link_libraries(recvfile ${TARGET_srt} ${DEPENDS_srt})

# This is recommended by cmake, but it doesn't work anyway.
# What is needed is that this below CMAKE_INSTALL_RPATH (yes, relative)
# is added as is.
Expand Down
117 changes: 117 additions & 0 deletions apps/legacy/recvfile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#ifndef WIN32
#include <arpa/inet.h>
#include <netdb.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <srt.h>

using namespace std;

int main(int argc, char* argv[])
{
if ((argc != 5) || (0 == atoi(argv[2])))
{
cout << "usage: recvfile server_ip server_port remote_filename local_filename" << endl;
return -1;
}

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

srt_setloglevel(logging::LogLevel::debug);

struct addrinfo hints, *peer;

memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;

SRTSOCKET fhandle = srt_socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol);
// SRT requires that third argument is always SOCK_DGRAM. The Stream API is set by an option,
// although there's also lots of other options to be set, for which there's a convenience option,
// SRTO_TRANSTYPE.
SRT_TRANSTYPE tt = SRTT_FILE;
srt_setsockopt(fhandle, 0, SRTO_TRANSTYPE, &tt, sizeof tt);

if (0 != getaddrinfo(argv[1], argv[2], &hints, &peer))
{
cout << "incorrect server/peer address. " << argv[1] << ":" << argv[2] << endl;
return -1;
}

// connect to the server, implict bind
if (SRT_ERROR == srt_connect(fhandle, peer->ai_addr, peer->ai_addrlen))
{
cout << "connect: " << srt_getlasterror_str() << endl;
return -1;
}

freeaddrinfo(peer);


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

if (SRT_ERROR == srt_send(fhandle, (char*)&len, sizeof(int)))
{
cout << "send: " << srt_getlasterror_str() << endl;
return -1;
}

if (SRT_ERROR == srt_send(fhandle, argv[3], len))
{
cout << "send: " << srt_getlasterror_str() << endl;
return -1;
}

// get size information
int64_t size;

if (SRT_ERROR == srt_recv(fhandle, (char*)&size, sizeof(int64_t)))
{
cout << "send: " << srt_getlasterror_str() << endl;
return -1;
}

if (size < 0)
{
cout << "no such file " << argv[3] << " on the server\n";
return -1;
}

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

SRT_TRACEBSTATS trace;
srt_bstats(fhandle, &trace, true);

if (SRT_ERROR == (recvsize = srt_recvfile(fhandle, argv[4], &offset, size, SRT_DEFAULT_RECVFILE_BLOCK)))
{
cout << "recvfile: " << srt_getlasterror_str() << endl;
return -1;
}

srt_bstats(fhandle, &trace, true);

cout << "speed = " << trace.mbpsRecvRate << "Mbits/sec" << endl;
int losspercent = 100*trace.pktRcvLossTotal/trace.pktRecv;
cout << "loss = " << trace.pktRcvLossTotal << "pkt (" << losspercent << "%)\n";

srt_close(fhandle);

//ofs.close();

// use this function to release the UDT library
srt_cleanup();

return 0;
}
185 changes: 185 additions & 0 deletions apps/legacy/sendfile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#ifndef WIN32
#include <cstdlib>
#include <netdb.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <srt.h>

using namespace std;

#ifndef WIN32
void* sendfile(void*);
#else
DWORD WINAPI sendfile(LPVOID);
#endif

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
srt_startup();

srt_setloglevel(logging::LogLevel::debug);

addrinfo hints;
addrinfo* res;

memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;

string service("9000");
if (2 == argc)
service = argv[1];

if (0 != getaddrinfo(NULL, service.c_str(), &hints, &res))
{
cout << "illegal port number or port is busy.\n" << endl;
return 0;
}

SRTSOCKET serv = srt_socket(res->ai_family, res->ai_socktype, res->ai_protocol);

// SRT requires that third argument is always SOCK_DGRAM. The Stream API is set by an option,
// although there's also lots of other options to be set, for which there's a convenience option,
// SRTO_TRANSTYPE.
SRT_TRANSTYPE tt = SRTT_FILE;
srt_setsockopt(serv, 0, SRTO_TRANSTYPE, &tt, sizeof tt);

// Windows UDP issue
// For better performance, modify HKLM\System\CurrentControlSet\Services\Afd\Parameters\FastSendDatagramThreshold
#ifdef WIN32
int mss = 1052;
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;
return 0;
}

freeaddrinfo(res);

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

srt_listen(serv, 10);

sockaddr_storage clientaddr;
int addrlen = sizeof(clientaddr);

SRTSOCKET fhandle;

while (true)
{
if (SRT_INVALID_SOCK == (fhandle = srt_accept(serv, (sockaddr*)&clientaddr, &addrlen)))
{
cout << "accept: " << srt_getlasterror_str() << endl;
return 0;
}

char clienthost[NI_MAXHOST];
char clientservice[NI_MAXSERV];
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
}

srt_close(serv);

// use this function to release the UDT library
srt_cleanup();

return 0;
}

#ifndef WIN32
void* sendfile(void* usocket)
#else
DWORD WINAPI sendfile(LPVOID usocket)
#endif
{
SRTSOCKET fhandle = *(SRTSOCKET*)usocket;
delete (SRTSOCKET*)usocket;

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

if (SRT_ERROR == srt_recv(fhandle, (char*)&len, sizeof(int)))
{
cout << "recv: " << srt_getlasterror_str() << endl;
return 0;
}

if (SRT_ERROR == srt_recv(fhandle, file, len))
{
cout << "recv: " << srt_getlasterror_str() << endl;
return 0;
}
file[len] = '\0';

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

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

// send file size information
if (SRT_ERROR == srt_send(fhandle, (char*)&size, sizeof(int64_t)))
{
cout << "send: " << srt_getlasterror_str() << endl;
return 0;
}

SRT_TRACEBSTATS trace;
srt_bstats(fhandle, &trace, true);

// send the file
int64_t offset = 0;
if (SRT_ERROR == srt_sendfile(fhandle, file, &offset, size, SRT_DEFAULT_SENDFILE_BLOCK))
{
cout << "sendfile: " << srt_getlasterror_str() << endl;
return 0;
}

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";

srt_close(fhandle);

//ifs.close();

#ifndef WIN32
return NULL;
#else
return 0;
#endif
}
4 changes: 2 additions & 2 deletions apps/srt-file-transmit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ bool DoUpload(UriParser& ut, string path, string filename)
size_t shift = 0;
while (n > 0)
{
int st = srt_send(ss, buf.data()+shift, n, 0);
int st = srt_send(ss, buf.data()+shift, n);
Verb() << "Upload: " << n << " --> " << st << (!shift ? string() : "+" + Sprint(shift));
if (st == SRT_ERROR)
{
Expand Down Expand Up @@ -311,7 +311,7 @@ bool DoDownload(UriParser& us, string directory, string filename)

for (;;)
{
int n = srt_recv(ss, buf.data(), ::g_buffer_size, 0);
int n = srt_recv(ss, buf.data(), ::g_buffer_size);
if (n == SRT_ERROR)
{
cerr << "Download: SRT error: " << srt_getlasterror_str() << endl;
Expand Down
Loading

0 comments on commit 4b897ba

Please sign in to comment.