@@ -21,18 +21,15 @@ DWORD WINAPI sendfile(LPVOID);
21
21
22
22
int main(int argc, char* argv[])
23
23
{
24
-
//usage: sendfile [server_port]
25
24
if ((2 < argc) || ((2 == argc) && (0 == atoi(argv[1]))))
26
25
{
27
26
cout << "usage: sendfile [server_port]" << endl;
28
27
return 0;
29
28
}
30
29
31
-
// use this function to initialize the UDT library
30
+
// Initialize the SRT library.
32
31
srt_startup();
33
32
34
-
srt_setloglevel(srt_logging::LogLevel::debug);
35
-
36
33
addrinfo hints;
37
34
addrinfo* res;
38
35
@@ -66,9 +63,6 @@ int main(int argc, char* argv[])
66
63
srt_setsockopt(serv, 0, SRTO_MSS, &mss, sizeof(int));
67
64
#endif
68
65
69
-
//int64_t maxbw = 5000000;
70
-
//srt_setsockopt(serv, 0, SRTO_MAXBW, &maxbw, sizeof maxbw);
71
-
72
66
if (SRT_ERROR == srt_bind(serv, res->ai_addr, res->ai_addrlen))
73
67
{
74
68
cout << "bind: " << srt_getlasterror_str() << endl;
@@ -78,14 +72,14 @@ int main(int argc, char* argv[])
78
72
freeaddrinfo(res);
79
73
80
74
cout << "server is ready at port: " << service << endl;
81
-
82
75
srt_listen(serv, 10);
83
76
84
77
sockaddr_storage clientaddr;
85
78
int addrlen = sizeof(clientaddr);
86
79
87
80
SRTSOCKET fhandle;
88
81
82
+
// Accept multiple client connections.
89
83
while (true)
90
84
{
91
85
if (SRT_INVALID_SOCK == (fhandle = srt_accept(serv, (sockaddr*)&clientaddr, &addrlen)))
@@ -99,18 +93,18 @@ int main(int argc, char* argv[])
99
93
getnameinfo((sockaddr *)&clientaddr, addrlen, clienthost, sizeof(clienthost), clientservice, sizeof(clientservice), NI_NUMERICHOST|NI_NUMERICSERV);
100
94
cout << "new connection: " << clienthost << ":" << clientservice << endl;
101
95
102
-
#ifndef _WIN32
103
-
pthread_t filethread;
104
-
pthread_create(&filethread, NULL, sendfile, new SRTSOCKET(fhandle));
105
-
pthread_detach(filethread);
106
-
#else
107
-
CreateThread(NULL, 0, sendfile, new SRTSOCKET(fhandle), 0, NULL);
108
-
#endif
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
109
103
}
110
104
111
105
srt_close(serv);
112
106
113
-
// use this function to release the UDT library
107
+
// Signal to the SRT library to clean up all allocated sockets and resources.
114
108
srt_cleanup();
115
109
116
110
return 0;
@@ -125,7 +119,7 @@ DWORD WINAPI sendfile(LPVOID usocket)
125
119
SRTSOCKET fhandle = *(SRTSOCKET*)usocket;
126
120
delete (SRTSOCKET*)usocket;
127
121
128
-
// aquiring file name information from client
122
+
// Acquiring file name information from client.
129
123
char file[1024];
130
124
int len;
131
125
@@ -142,15 +136,13 @@ DWORD WINAPI sendfile(LPVOID usocket)
142
136
}
143
137
file[len] = '\0';
144
138
145
-
// open the file (only to check the size)
139
+
// Open the file only to know its size.
146
140
fstream ifs(file, ios::in | ios::binary);
147
-
148
141
ifs.seekg(0, ios::end);
149
-
int64_t size = ifs.tellg();
150
-
//ifs.seekg(0, ios::beg);
142
+
const int64_t size = ifs.tellg();
151
143
ifs.close();
152
144
153
-
// send file size information
145
+
// Send file size.
154
146
if (SRT_ERROR == srt_send(fhandle, (char*)&size, sizeof(int64_t)))
155
147
{
156
148
cout << "send: " << srt_getlasterror_str() << endl;
@@ -160,7 +152,7 @@ DWORD WINAPI sendfile(LPVOID usocket)
160
152
SRT_TRACEBSTATS trace;
161
153
srt_bstats(fhandle, &trace, true);
162
154
163
-
// send the file
155
+
// Send the file itself.
164
156
int64_t offset = 0;
165
157
if (SRT_ERROR == srt_sendfile(fhandle, file, &offset, size, SRT_DEFAULT_SENDFILE_BLOCK))
166
158
{
@@ -170,13 +162,11 @@ DWORD WINAPI sendfile(LPVOID usocket)
170
162
171
163
srt_bstats(fhandle, &trace, true);
172
164
cout << "speed = " << trace.mbpsSendRate << "Mbits/sec" << endl;
173
-
int losspercent = 100*trace.pktSndLossTotal/trace.pktSent;
174
-
cout << "loss = " << trace.pktSndLossTotal << "pkt (" << losspercent << "%)\n";
165
+
const int64_t losspercent = 100 * trace.pktSndLossTotal / trace.pktSent;
166
+
cout << "network loss = " << trace.pktSndLossTotal << "pkts (" << losspercent << "%)\n";
175
167
176
168
srt_close(fhandle);
177
169
178
-
//ifs.close();
179
-
180
170
#ifndef _WIN32
181
171
return NULL;
182
172
#else
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4