提交 6b8c8747 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Worker: Remove Socket utils.

上级 4d3e285a
......@@ -73,26 +73,6 @@ namespace Utils
return copied_addr;
}
class Socket
{
public:
/**
* Builds a pair of connected sockets. The sockets are created in non-blocking
* mode and with close-exec (so they are closed after fork()).
*
* @param family Socket family (AF_UNIX...).
* @param type Socket type (SOCK_DGRAM...) + flags.
* @param fds Array with two integers.
*
* In case of error throws a MediaSoupError exception.
*/
static void BuildSocketPair(int family, int type, int* fds);
private:
static bool SetNonBlock(int fd);
static bool SetCloExec(int fd);
};
class File
{
public:
......
#define MS_CLASS "Utils::Socket"
#include "Utils.h"
#include "MediaSoupError.h"
#include "Logger.h"
#include <cerrno>
#include <fcntl.h>
namespace Utils
{
void Socket::BuildSocketPair(int family, int type, int* fds)
{
MS_TRACE();
// Old Linux/BSD kernels and OSX don't support setting SOCK_NONBLOCK and/or
// SOCK_CLOEXEC during socket creation. In those cases use fcntl().
#if defined(SOCK_NONBLOCK)
type |= SOCK_NONBLOCK;
#endif
#if defined(SOCK_CLOEXEC)
type |= SOCK_CLOEXEC;
#endif
// Create a pair of connected sockets.
if (socketpair(family, type, 0, fds))
MS_THROW_ERROR("socketpair() failed: %s", std::strerror(errno));
#if !defined(SOCK_NONBLOCK)
if (!(SetNonBlock(fds[0]) && SetNonBlock(fds[1])))
MS_THROW_ERROR("error setting sockets as non-blocking");
#endif
#if !defined(SOCK_CLOEXEC)
if (!(SetCloExec(fds[0]) && SetCloExec(fds[1])))
MS_THROW_ERROR("error setting sockets as close-exec");
#endif
}
bool Socket::SetNonBlock(int fd)
{
MS_TRACE();
int flags;
int ret;
flags = fcntl(fd, F_GETFL);
if (flags == -1)
{
MS_ERROR("fcntl() failed when getting flags");
return false;
}
ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
if (ret == -1)
{
MS_ERROR("fcntl() failed when setting O_NONBLOCK: %s", std::strerror(errno));
return false;
}
return true;
}
bool Socket::SetCloExec(int fd)
{
MS_TRACE();
int flags;
int ret;
flags = fcntl(fd, F_GETFD);
if (flags == -1)
{
MS_ERROR("fcntl() failed when getting flags");
return false;
}
ret = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
if (ret == -1)
{
MS_ERROR("fcntl() failed when setting FD_CLOEXEC: %s", std::strerror(errno));
return false;
}
return true;
}
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论