You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
4.7 KiB
158 lines
4.7 KiB
#include "servletWebRTC.h"
|
|
#include "../../components/base/basicFunctionality.h"
|
|
#include <ctype.h>
|
|
#include <err.h>
|
|
#include <errno.h>
|
|
#include <getopt.h>
|
|
#include <ifaddrs.h>
|
|
#include <netdb.h>
|
|
#include <paths.h>
|
|
#include <poll.h>
|
|
#include <stdio.h>
|
|
#include <signal.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <arpa/inet.h>
|
|
#include <netinet/ip.h>
|
|
#include <netinet/udp.h>
|
|
#include <sys/param.h> /* MIN() */
|
|
#include <sys/socket.h>
|
|
|
|
#define LOG( X ) m_pLog->Log( "SWRTC", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "SWRTC", this, _LOG_ERROR, X )
|
|
|
|
#define ENABLE_SOCKOPT(sd, level, opt) \
|
|
do { \
|
|
int val = 1; \
|
|
if (setsockopt(sd, level, opt, &val, sizeof(val)) < 0) \
|
|
warn("Failed enabling %s for web service", #opt); \
|
|
} while (0);
|
|
|
|
servletWebRTC::servletWebRTC( uint16_t ListenPort ):basicDualThread(){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_nListenPort = ListenPort;
|
|
m_nSocketDescriptor = -1;
|
|
LOG("Construct");
|
|
};
|
|
|
|
servletWebRTC::~servletWebRTC(){
|
|
LOG("Destruct");
|
|
};
|
|
|
|
bool servletWebRTC::create( uint16_t ListenPort ){
|
|
LOG("Create");
|
|
if( m_nListenPort == 0 && ListenPort == 0 ) return false;
|
|
m_nListenPort = ListenPort;
|
|
struct sockaddr sa;
|
|
struct sockaddr_in *sin;
|
|
|
|
memset(&sa, 0, sizeof(struct sockaddr));
|
|
sa.sa_family = AF_INET;
|
|
sin = (struct sockaddr_in *)&sa;
|
|
sin->sin_addr.s_addr = htonl(INADDR_ANY);
|
|
sin->sin_port = htons( m_nListenPort );
|
|
|
|
m_nSocketDescriptor = socket( sa.sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
|
|
if( m_nSocketDescriptor == -1 ){
|
|
LOG_ERROR("Failed creating web socket");
|
|
return false;
|
|
};
|
|
ENABLE_SOCKOPT( m_nSocketDescriptor, SOL_SOCKET, SO_REUSEADDR);
|
|
#ifdef SO_REUSEPORT
|
|
ENABLE_SOCKOPT( m_nSocketDescriptor, SOL_SOCKET, SO_REUSEPORT);
|
|
#endif
|
|
if( bind( m_nSocketDescriptor, &sa, sizeof(sa)) < 0 ){
|
|
LOG_ERROR("Failed binding Socket");
|
|
close( m_nSocketDescriptor );
|
|
return false;
|
|
} else {
|
|
LOG("Bind Socessfull");
|
|
}
|
|
if( listen( m_nSocketDescriptor, 10) != 0 ){
|
|
LOG_ERROR("Failed setting web listen backlog");
|
|
close( m_nSocketDescriptor );
|
|
return false;
|
|
} else {
|
|
LOG("Listen OK");
|
|
};
|
|
return basicDualThread::init();
|
|
};
|
|
|
|
// Listener
|
|
void servletWebRTC::m_fThreadAProc( void ){
|
|
struct pollfd pfd[1];
|
|
pid_t pid = gettid();
|
|
char pidBuf[16];
|
|
sprintf(pidBuf,"pid:%d", pid);
|
|
LOG(pidBuf);
|
|
pfd->fd = m_nSocketDescriptor;
|
|
pfd->events = POLLIN | POLLHUP;
|
|
m_bThreadAStatus = TH_WORKING;
|
|
while( m_bThreadAStatus < TH_ENDED ) {
|
|
int RetVal = 0;//, timeout;
|
|
RetVal = poll( pfd, 1, 1000 );
|
|
if( RetVal < 0 ){
|
|
if (EINTR == errno) continue;
|
|
LOG_ERROR("Unrecoverable error");
|
|
m_bThreadAStatus = TH_ENDED;
|
|
continue;
|
|
};
|
|
// Handle Connections
|
|
if( RetVal != 0 ){
|
|
if ( pfd[0].revents & POLLIN ) {
|
|
servletClientHandlerWebRTC * pClient;
|
|
LOG("Handle New Client");
|
|
pClient = new servletClientHandlerWebRTC( this, pfd[0].fd );
|
|
if( pClient != NULL ) {
|
|
m_vWebClients.push_back( pClient );
|
|
};
|
|
};
|
|
};
|
|
// Handle Dead Clients
|
|
if( m_vWebClients.size() ) {
|
|
for( vectorClientHandlerWebRTC::iterator index = m_vWebClients.begin(); index < m_vWebClients.end(); index++ ) {
|
|
servletClientHandlerWebRTC * pClient;
|
|
pClient = (*index);
|
|
if( !( pClient->getStatus()< TH_ENDED ) ) {
|
|
// Remove Dead Client
|
|
m_vWebClients.erase(index);
|
|
LOG("Client Record Erased!");
|
|
delete( pClient );
|
|
};
|
|
};
|
|
};
|
|
SLEEP(50);
|
|
};
|
|
};
|
|
|
|
// Worker
|
|
void servletWebRTC::m_fThreadBProc( void ){
|
|
pid_t pid = gettid();
|
|
char pidBuf[16];
|
|
sprintf(pidBuf,"pid:%d", pid);
|
|
LOG(pidBuf);
|
|
m_bThreadBStatus = TH_WORKING;
|
|
while( m_bThreadBStatus < TH_ENDED ) {
|
|
SLEEP(50);
|
|
};
|
|
};
|
|
|
|
bool servletWebRTC::attachChannel( CSTR MediaURI, bool LocalDevice ){
|
|
if( MediaURI == NULL ) return false;
|
|
servletChannelWebRTC * tPtr;
|
|
tPtr = new servletChannelWebRTC();
|
|
if( tPtr == NULL ) return false;
|
|
if( tPtr->create(MediaURI) ){
|
|
m_vChannels.push_back( tPtr );
|
|
} else {
|
|
delete( tPtr );
|
|
};
|
|
return true;
|
|
};
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
|