Repository navigation
Expand file tree
/
Copy pathIngestServer.cpp
More file actions
196 lines (178 loc) · 5.64 KB
/
Copy pathIngestServer.cpp
File metadata and controls
196 lines (178 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* @file IngestServer.cpp
* @author Hayden McAfee (hayden@outlook.com)
* @version 0.1
* @date 2020-08-09
*
* @copyright Copyright (c) 2020 Hayden McAfee
*
*/
#include "IngestServer.h"
extern "C"
{
#include <debug.h>
}
#include <netinet/in.h>
#include <cerrno>
#include <stdexcept>
#include <memory>
#include <unistd.h>
#pragma region Constructor/Destructor
IngestServer::IngestServer(
std::shared_ptr<ServiceConnection> serviceConnection,
int listenPort,
int socketQueueLimit) :
serviceConnection(serviceConnection),
listenPort(listenPort),
socketQueueLimit(socketQueueLimit)
{ }
#pragma endregion
#pragma region Public methods
void IngestServer::Start()
{
sockaddr_in socketAddress = { 0 };
socketAddress.sin_family = AF_INET;
socketAddress.sin_addr.s_addr = htonl(INADDR_ANY);
socketAddress.sin_port = htons(listenPort);
listenSocketHandle = socket(AF_INET, SOCK_STREAM, 0);
int bindResult = bind(
listenSocketHandle,
(const sockaddr*)&socketAddress,
sizeof(socketAddress));
switch (bindResult)
{
case 0:
break;
case EADDRINUSE:
throw std::runtime_error("FTL ingest could not bind to socket, "
"this address is already in use.");
case EACCES:
throw std::runtime_error("FTL ingest could not bind to socket, "
"access was denied.");
default:
throw std::runtime_error("FTL ingest could not bind to socket.");
}
int listenResult = listen(listenSocketHandle, socketQueueLimit);
switch (listenResult)
{
case 0:
break;
case EADDRINUSE:
throw std::runtime_error("FTL ingest could not listen on socket, "
"this port is already in use.");
default:
throw std::runtime_error("FTL ingest could not listen on socket.");
}
listenThread = std::thread(&IngestServer::startListenThread, this);
}
void IngestServer::Stop()
{
for (const auto& ingestConnection : pendingConnections)
{
ingestConnection->Stop();
}
pendingConnections.clear();
shutdown(listenSocketHandle, SHUT_RDWR);
listenThread.join();
close(listenSocketHandle);
}
void IngestServer::SetOnRequestMediaConnection(
std::function<uint16_t (std::shared_ptr<IngestConnection>)> callback)
{
onRequestMediaConnection = callback;
}
#pragma endregion
#pragma region Private methods
void IngestServer::startListenThread()
{
JANUS_LOG(LOG_INFO, "FTL: Ingest server is listening on port %d\n", listenPort);
while (true)
{
// Accept incoming connections, manage them as "pending" until the
// FTL handshake is completed.
int connectionHandle = accept(listenSocketHandle, nullptr, nullptr);
if (connectionHandle == -1)
{
if (errno == EINVAL)
{
JANUS_LOG(LOG_INFO, "FTL: Ingest server is being shut down.\n");
break;
}
}
else
{
sockaddr_in acceptAddress = { 0 };
socklen_t acceptLen = sizeof(acceptAddress);
getpeername(connectionHandle, reinterpret_cast<sockaddr*>(&acceptAddress), &acceptLen);
JANUS_LOG(LOG_INFO, "FTL: Ingest server accepted connection...\n");
std::shared_ptr<IngestConnection> connection =
std::make_shared<IngestConnection>(
connectionHandle,
acceptAddress,
serviceConnection);
pendingConnections.push_back(connection);
connection->SetOnClosed(std::bind(
&IngestServer::connectionClosed,
this,
std::placeholders::_1));
connection->SetOnRequestMediaConnection(std::bind(
&IngestServer::mediaConnectionRequested,
this,
std::placeholders::_1));
connection->Start();
}
}
}
void IngestServer::removeConnection(IngestConnection& connection)
{
for (auto it = pendingConnections.begin(); it != pendingConnections.end(); ++it)
{
const auto& pendingConnection = *it;
if (pendingConnection.get() == &connection)
{
pendingConnections.erase(it);
break;
}
}
}
void IngestServer::connectionClosed(IngestConnection& connection)
{
// Stop and remove this connection
connection.Stop();
removeConnection(connection);
uint32_t connectionCount = pendingConnections.size();
JANUS_LOG(LOG_INFO, "FTL: Pending ingest connection closed. %d pending ingest connections.\n",
connectionCount);
}
uint16_t IngestServer::mediaConnectionRequested(IngestConnection& connection)
{
// Find our reference to this connection and remove it
std::shared_ptr<IngestConnection> connectionReference;
for (auto it = pendingConnections.begin(); it != pendingConnections.end(); ++it)
{
std::shared_ptr<IngestConnection>& c = *it;
if (c.get() == &connection)
{
connectionReference = *it;
pendingConnections.erase(it);
break;
}
}
if (connectionReference == nullptr)
{
throw std::runtime_error("Could not find reference to connection.");
}
// Pass this connection out to callback
if (onRequestMediaConnection != nullptr)
{
// Relinquish callbacks
connectionReference->SetOnClosed(nullptr);
connectionReference->SetOnRequestMediaConnection(nullptr);
return onRequestMediaConnection(connectionReference);
}
else
{
throw std::runtime_error("Callback from IngestServer to request new media connection failed.");
}
}
#pragma endregion