| 1 |
#ifndef _WIN32 |
| 2 |
#include <cstdlib> |
| 3 |
#include <netdb.h> |
| 4 |
#else |
| 5 |
#include <winsock2.h> |
| 6 |
#include <ws2tcpip.h> |
| 7 |
#endif |
| 8 |
#include <fstream> |
| 9 |
#include <iostream> |
| 10 |
#include <string> |
| 11 |
#include <cstring> |
| 12 |
#include <srt.h> |
| 13 |
|
| 14 |
using namespace std; |
| 15 |
|
| 16 |
#ifndef _WIN32 |
| 17 |
void* sendfile(void*); |
| 18 |
#else |
| 19 |
DWORD WINAPI sendfile(LPVOID); |
| 20 |
#endif |
| 21 |
|
| 22 |
int main(int argc, char* argv[]) |
| 23 |
{ |
| 24 |
if ((2 < argc) || ((2 == argc) && (0 == atoi(argv[1])))) |
| 25 |
{ |
| 26 |
cout << "usage: sendfile [server_port]" << endl; |
| 27 |
return 0; |
| 28 |
} |
| 29 |
|
| 30 |
// Initialize the SRT library. |
| 31 |
srt_startup(); |
| 32 |
|
| 33 |
addrinfo hints; |
| 34 |
addrinfo* res; |
| 35 |
|
| 36 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 37 |
hints.ai_flags = AI_PASSIVE; |
| 38 |
hints.ai_family = AF_INET; |
| 39 |
hints.ai_socktype = SOCK_DGRAM; |
| 40 |
|
| 41 |
string service("9000"); |
| 42 |
if (2 == argc) |
| 43 |
service = argv[1]; |
| 44 |
|
| 45 |
if (0 != getaddrinfo(NULL, service.c_str(), &hints, &res)) |
| 46 |
{ |
| 47 |
cout << "illegal port number or port is busy.\n" << endl; |
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
SRTSOCKET serv = srt_create_socket(); |
| 52 |
|
| 53 |
// SRT requires that third argument is always SOCK_DGRAM. The Stream API is set by an option, |
| 54 |
// although there's also lots of other options to be set, for which there's a convenience option, |
| 55 |
// SRTO_TRANSTYPE. |
| 56 |
SRT_TRANSTYPE tt = SRTT_FILE; |
| 57 |
srt_setsockopt(serv, 0, SRTO_TRANSTYPE, &tt, sizeof tt); |
| 58 |
|
| 59 |
// Windows UDP issue |
| 60 |
// For better performance, modify HKLM\System\CurrentControlSet\Services\Afd\Parameters\FastSendDatagramThreshold |
| 61 |
#ifdef _WIN32 |
| 62 |
int mss = 1052; |
| 63 |
srt_setsockopt(serv, 0, SRTO_MSS, &mss, sizeof(int)); |
| 64 |
#endif |
| 65 |
|
| 66 |
if (SRT_ERROR == srt_bind(serv, res->ai_addr, res->ai_addrlen)) |
| 67 |
{ |
| 68 |
cout << "bind: " << srt_getlasterror_str() << endl; |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
freeaddrinfo(res); |
| 73 |
|
| 74 |
cout << "server is ready at port: " << service << endl; |
| 75 |
srt_listen(serv, 10); |
| 76 |
|
| 77 |
sockaddr_storage clientaddr; |
| 78 |
int addrlen = sizeof(clientaddr); |
| 79 |
|
| 80 |
SRTSOCKET fhandle; |
| 81 |
|
| 82 |
// Accept multiple client connections. |
| 83 |
while (true) |
| 84 |
{ |
| 85 |
if (SRT_INVALID_SOCK == (fhandle = srt_accept(serv, (sockaddr*)&clientaddr, &addrlen))) |
| 86 |
{ |
| 87 |
cout << "accept: " << srt_getlasterror_str() << endl; |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
char clienthost[NI_MAXHOST]; |
| 92 |
char clientservice[NI_MAXSERV]; |
| 93 |
getnameinfo((sockaddr *)&clientaddr, addrlen, clienthost, sizeof(clienthost), clientservice, sizeof(clientservice), NI_NUMERICHOST|NI_NUMERICSERV); |
| 94 |
cout << "new connection: " << clienthost << ":" << clientservice << endl; |
| 95 |
|
| 96 |
#ifndef _WIN32 |
| 97 |
pthread_t filethread; |
| 98 |
pthread_create(&filethread, NULL, sendfile, new SRTSOCKET(fhandle)); |
| 99 |
pthread_detach(filethread); |
| 100 |
#else |
| 101 |
CreateThread(NULL, 0, sendfile, new SRTSOCKET(fhandle), 0, NULL); |
| 102 |
#endif |
| 103 |
} |
| 104 |
|
| 105 |
srt_close(serv); |
| 106 |
|
| 107 |
// Signal to the SRT library to clean up all allocated sockets and resources. |
| 108 |
srt_cleanup(); |
| 109 |
|
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
#ifndef _WIN32 |
| 114 |
void* sendfile(void* usocket) |
| 115 |
#else |
| 116 |
DWORD WINAPI sendfile(LPVOID usocket) |
| 117 |
#endif |
| 118 |
{ |
| 119 |
SRTSOCKET fhandle = *(SRTSOCKET*)usocket; |
| 120 |
delete (SRTSOCKET*)usocket; |
| 121 |
|
| 122 |
// Acquiring file name information from client. |
| 123 |
char file[1024]; |
| 124 |
int len; |
| 125 |
|
| 126 |
if (SRT_ERROR == srt_recv(fhandle, (char*)&len, sizeof(int))) |
| 127 |
{ |
| 128 |
cout << "recv: " << srt_getlasterror_str() << endl; |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
if (SRT_ERROR == srt_recv(fhandle, file, len)) |
| 133 |
{ |
| 134 |
cout << "recv: " << srt_getlasterror_str() << endl; |
| 135 |
return 0; |
| 136 |
} |
| 137 |
file[len] = '\0'; |
| 138 |
|
| 139 |
// Open the file only to know its size. |
| 140 |
fstream ifs(file, ios::in | ios::binary); |
| 141 |
ifs.seekg(0, ios::end); |
| 142 |
const int64_t size = ifs.tellg(); |
| 143 |
ifs.close(); |
| 144 |
|
| 145 |
// Send file size. |
| 146 |
if (SRT_ERROR == srt_send(fhandle, (char*)&size, sizeof(int64_t))) |
| 147 |
{ |
| 148 |
cout << "send: " << srt_getlasterror_str() << endl; |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
SRT_TRACEBSTATS trace; |
| 153 |
srt_bstats(fhandle, &trace, true); |
| 154 |
|
| 155 |
// Send the file itself. |
| 156 |
int64_t offset = 0; |
| 157 |
if (SRT_ERROR == srt_sendfile(fhandle, file, &offset, size, SRT_DEFAULT_SENDFILE_BLOCK)) |
| 158 |
{ |
| 159 |
cout << "sendfile: " << srt_getlasterror_str() << endl; |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
srt_bstats(fhandle, &trace, true); |
| 164 |
cout << "speed = " << trace.mbpsSendRate << "Mbits/sec" << endl; |
| 165 |
const int64_t losspercent = 100 * trace.pktSndLossTotal / trace.pktSent; |
| 166 |
cout << "network loss = " << trace.pktSndLossTotal << "pkts (" << losspercent << "%)\n"; |
| 167 |
|
| 168 |
srt_close(fhandle); |
| 169 |
|
| 170 |
#ifndef _WIN32 |
| 171 |
return NULL; |
| 172 |
#else |
| 173 |
return 0; |
| 174 |
#endif |
| 175 |
} |