icinga2/lib/base/socket.cpp

714 lines
15 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
2012-05-11 13:33:57 +02:00
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2013-03-16 21:18:53 +01:00
#include "base/socket.h"
#include "base/objectlock.h"
#include "base/utility.h"
2013-03-18 22:40:40 +01:00
#include "base/exception.h"
2013-03-16 21:18:53 +01:00
#include <sstream>
2013-03-15 18:21:29 +01:00
#include <boost/bind.hpp>
2013-03-16 21:18:53 +01:00
#include <boost/make_shared.hpp>
2013-03-18 17:04:22 +01:00
#include <boost/exception/errinfo_api_function.hpp>
#include <boost/exception/errinfo_errno.hpp>
#include <boost/exception/errinfo_file_name.hpp>
2012-03-28 13:24:49 +02:00
using namespace icinga;
/**
* Constructor for the Socket class.
*/
2012-03-28 13:24:49 +02:00
Socket::Socket(void)
2012-11-22 12:04:32 +01:00
: m_FD(INVALID_SOCKET), m_Connected(false), m_Listening(false),
m_SendQueue(boost::make_shared<FIFO>()), m_RecvQueue(boost::make_shared<FIFO>())
{
m_SendQueue->Start();
m_RecvQueue->Start();
}
2012-03-28 13:24:49 +02:00
/**
* Destructor for the Socket class.
*/
2012-03-28 13:24:49 +02:00
Socket::~Socket(void)
{
m_SendQueue->Close();
m_RecvQueue->Close();
2013-03-10 05:10:51 +01:00
CloseInternal();
2012-03-28 13:24:49 +02:00
}
2012-09-14 14:41:17 +02:00
/**
* Starts I/O processing for this socket.
*/
2012-03-28 13:24:49 +02:00
void Socket::Start(void)
{
ASSERT(!m_ReadThread.joinable() && !m_WriteThread.joinable());
ASSERT(GetFD() != INVALID_SOCKET);
2012-04-18 15:22:25 +02:00
2013-03-15 18:21:29 +01:00
// TODO: figure out why we're not using "this" here (hint: to keep the object alive until the threads are done)
m_ReadThread = boost::thread(boost::bind(&Socket::ReadThreadProc, static_cast<Socket::Ptr>(GetSelf())));
2012-06-24 02:56:48 +02:00
m_ReadThread.detach();
2013-03-15 18:21:29 +01:00
m_WriteThread = boost::thread(boost::bind(&Socket::WriteThreadProc, static_cast<Socket::Ptr>(GetSelf())));
2012-06-24 02:56:48 +02:00
m_WriteThread.detach();
2012-11-22 12:04:32 +01:00
Stream::Start();
2012-03-28 13:24:49 +02:00
}
/**
* Sets the file descriptor for this socket object.
*
* @param fd The file descriptor.
*/
2012-03-28 13:24:49 +02:00
void Socket::SetFD(SOCKET fd)
{
2013-03-02 09:07:47 +01:00
ObjectLock olock(this);
2013-02-13 12:47:51 +01:00
/* mark the socket as non-blocking and close-on-exec */
2012-05-11 12:44:04 +02:00
if (fd != INVALID_SOCKET) {
Utility::SetNonBlockingSocket(fd);
2013-02-13 12:47:51 +01:00
#ifndef _WIN32
Utility::SetCloExec(fd);
2013-02-13 12:47:51 +01:00
#endif /* _WIN32 */
2012-05-11 12:44:04 +02:00
}
2012-03-28 13:24:49 +02:00
m_FD = fd;
}
/**
* Retrieves the file descriptor for this socket object.
*
* @returns The file descriptor.
*/
2012-03-28 13:24:49 +02:00
SOCKET Socket::GetFD(void) const
{
2013-03-02 09:07:47 +01:00
ObjectLock olock(this);
2012-03-28 13:24:49 +02:00
return m_FD;
}
2013-03-02 09:07:47 +01:00
/**
* Closes the socket.
*/
2013-03-10 05:10:51 +01:00
void Socket::CloseInternal(void)
2012-03-28 13:24:49 +02:00
{
{
ObjectLock olock(this);
2013-03-02 09:07:47 +01:00
2013-03-10 05:10:51 +01:00
if (m_FD != INVALID_SOCKET) {
closesocket(m_FD);
m_FD = INVALID_SOCKET;
}
}
2012-06-24 02:56:48 +02:00
2012-11-22 12:04:32 +01:00
Stream::Close();
2012-03-28 13:24:49 +02:00
}
2013-03-10 05:10:51 +01:00
/**
* Shuts down the socket.
*/
void Socket::Close(void)
{
SetWriteEOF(true);
}
/**
2012-05-26 20:00:03 +02:00
* Retrieves the last error that occured for the socket.
*
* @returns An error code.
*/
2012-05-26 20:00:03 +02:00
int Socket::GetError(void) const
{
int opt;
socklen_t optlen = sizeof(opt);
int rc = getsockopt(GetFD(), SOL_SOCKET, SO_ERROR, (char *)&opt, &optlen);
2012-05-26 20:00:03 +02:00
if (rc >= 0)
return opt;
return 0;
}
/**
* Processes errors that have occured for the socket.
*/
2012-06-24 02:56:48 +02:00
void Socket::HandleException(void)
2012-03-28 13:24:49 +02:00
{
2013-03-02 09:07:47 +01:00
ObjectLock olock(this);
2013-03-11 14:03:01 +01:00
#ifndef _WIN32
2013-03-11 13:45:08 +01:00
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("select")
<< boost::errinfo_errno(GetError()));
2013-03-11 13:45:08 +01:00
#else /* _WIN32 */
2013-03-11 14:03:01 +01:00
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("select")
2013-03-18 22:40:40 +01:00
<< errinfo_win32_error(GetError()));
2013-03-11 13:45:08 +01:00
#endif /* _WIN32 */
2012-03-28 13:24:49 +02:00
}
/**
* Formats a sockaddr in a human-readable way.
*
* @returns A String describing the sockaddr.
*/
String Socket::GetAddressFromSockaddr(sockaddr *address, socklen_t len)
{
char host[NI_MAXHOST];
char service[NI_MAXSERV];
2012-07-17 20:41:06 +02:00
if (getnameinfo(address, len, host, sizeof(host), service,
2013-03-11 13:45:08 +01:00
sizeof(service), NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
2013-03-11 14:03:01 +01:00
#ifndef _WIN32
2013-03-11 13:45:08 +01:00
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("getnameinfo")
<< boost::errinfo_errno(errno));
2013-03-11 14:03:01 +01:00
#else /* _WIN32 */
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("getnameinfo")
2013-03-18 22:40:40 +01:00
<< errinfo_win32_error(WSAGetLastError()));
2013-03-11 14:03:01 +01:00
#endif /* _WIN32 */
2013-03-11 13:45:08 +01:00
}
2013-03-16 21:18:53 +01:00
std::ostringstream s;
s << "[" << host << "]:" << service;
return s.str();
}
/**
* Returns a String describing the local address of the socket.
*
* @returns A String describing the local address.
*/
String Socket::GetClientAddress(void)
{
2012-08-13 13:06:43 +02:00
boost::mutex::scoped_lock lock(m_SocketMutex);
2012-06-24 02:56:48 +02:00
sockaddr_storage sin;
socklen_t len = sizeof(sin);
2013-03-11 13:45:08 +01:00
if (getsockname(GetFD(), (sockaddr *)&sin, &len) < 0) {
2013-03-11 14:03:01 +01:00
#ifndef _WIN32
2013-03-11 13:45:08 +01:00
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("getsockname")
<< boost::errinfo_errno(errno));
2013-03-11 14:03:01 +01:00
#else /* _WIN32 */
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("getsockname")
2013-03-18 22:40:40 +01:00
<< errinfo_win32_error(WSAGetLastError()));
2013-03-11 14:03:01 +01:00
#endif /* _WIN32 */
2013-03-11 13:45:08 +01:00
}
return GetAddressFromSockaddr((sockaddr *)&sin, len);
}
/**
* Returns a String describing the peer address of the socket.
*
* @returns A String describing the peer address.
*/
String Socket::GetPeerAddress(void)
{
2012-08-13 13:06:43 +02:00
boost::mutex::scoped_lock lock(m_SocketMutex);
2012-06-24 02:56:48 +02:00
sockaddr_storage sin;
socklen_t len = sizeof(sin);
2013-03-11 13:45:08 +01:00
if (getpeername(GetFD(), (sockaddr *)&sin, &len) < 0) {
2013-03-11 14:03:01 +01:00
#ifndef _WIN32
2013-03-11 13:45:08 +01:00
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("getpeername")
<< boost::errinfo_errno(errno));
2013-03-11 14:03:01 +01:00
#else /* _WIN32 */
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("getpeername")
2013-03-18 22:40:40 +01:00
<< errinfo_win32_error(WSAGetLastError()));
2013-03-11 14:03:01 +01:00
#endif /* _WIN32 */
2013-03-11 13:45:08 +01:00
}
return GetAddressFromSockaddr((sockaddr *)&sin, len);
}
2012-05-26 20:00:03 +02:00
2012-09-14 14:41:17 +02:00
/**
* Read thread procedure for sockets. This function waits until the
* socket is readable and processes inbound data.
*/
2012-06-24 02:56:48 +02:00
void Socket::ReadThreadProc(void)
{
2012-08-13 13:06:43 +02:00
boost::mutex::scoped_lock lock(m_SocketMutex);
2012-06-24 02:56:48 +02:00
for (;;) {
fd_set readfds, exceptfds;
FD_ZERO(&readfds);
FD_ZERO(&exceptfds);
int fd = GetFD();
if (fd == INVALID_SOCKET)
return;
if (WantsToRead())
FD_SET(fd, &readfds);
FD_SET(fd, &exceptfds);
lock.unlock();
timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
int rc = select(fd + 1, &readfds, NULL, &exceptfds, &tv);
lock.lock();
if (GetFD() == INVALID_SOCKET)
return;
2012-07-17 20:41:06 +02:00
try {
2013-03-11 13:45:08 +01:00
if (rc < 0) {
2013-03-11 14:03:01 +01:00
#ifndef _WIN32
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("select")
<< boost::errinfo_errno(errno));
2013-03-11 14:03:01 +01:00
#else /* _WIN32 */
2013-03-11 13:45:08 +01:00
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("select")
2013-03-18 22:40:40 +01:00
<< errinfo_win32_error(WSAGetLastError()));
2013-03-11 14:03:01 +01:00
#endif /* _WIN32 */
2013-03-11 13:45:08 +01:00
}
2012-06-24 02:56:48 +02:00
2012-07-17 20:41:06 +02:00
if (FD_ISSET(fd, &readfds))
HandleReadable();
2012-06-24 02:56:48 +02:00
2012-07-17 20:41:06 +02:00
if (FD_ISSET(fd, &exceptfds))
HandleException();
} catch (...) {
2012-11-22 12:04:32 +01:00
SetException(boost::current_exception());
2012-07-17 20:41:06 +02:00
2013-03-10 05:10:51 +01:00
CloseInternal();
2012-07-17 20:41:06 +02:00
break;
}
2012-06-24 02:56:48 +02:00
if (WantsToWrite())
m_WriteCV.notify_all(); /* notify Write thread */
2012-06-24 02:56:48 +02:00
}
}
2012-09-14 14:41:17 +02:00
/**
* Write thread procedure for sockets. This function waits until the socket
* is writable and processes outbound data.
*/
2012-06-24 02:56:48 +02:00
void Socket::WriteThreadProc(void)
{
2012-08-13 13:06:43 +02:00
boost::mutex::scoped_lock lock(m_SocketMutex);
2012-06-24 02:56:48 +02:00
for (;;) {
fd_set writefds;
FD_ZERO(&writefds);
2012-07-16 11:44:11 +02:00
while (!WantsToWrite()) {
m_WriteCV.timed_wait(lock, boost::posix_time::seconds(1));
2012-06-24 02:56:48 +02:00
if (GetFD() == INVALID_SOCKET)
return;
}
int fd = GetFD();
if (fd == INVALID_SOCKET)
return;
2012-06-24 02:56:48 +02:00
FD_SET(fd, &writefds);
lock.unlock();
int rc = select(fd + 1, NULL, &writefds, NULL, NULL);
lock.lock();
if (GetFD() == INVALID_SOCKET)
return;
2012-07-17 20:41:06 +02:00
try {
2013-03-11 13:45:08 +01:00
if (rc < 0) {
2013-03-11 14:03:01 +01:00
#ifndef _WIN32
2013-03-11 13:45:08 +01:00
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("select")
<< boost::errinfo_errno(errno));
2013-03-11 14:03:01 +01:00
#else /* _WIN32 */
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("select")
2013-03-18 22:40:40 +01:00
<< errinfo_win32_error(WSAGetLastError()));
2013-03-11 14:03:01 +01:00
#endif /* _WIN32 */
2013-03-11 13:45:08 +01:00
}
2012-06-24 02:56:48 +02:00
2012-07-17 20:41:06 +02:00
if (FD_ISSET(fd, &writefds))
HandleWritable();
} catch (...) {
2012-11-22 12:04:32 +01:00
SetException(boost::current_exception());
2012-07-17 20:41:06 +02:00
2013-03-10 05:10:51 +01:00
CloseInternal();
2012-07-17 20:41:06 +02:00
break;
}
2012-06-24 02:56:48 +02:00
}
}
2012-09-14 14:41:17 +02:00
/**
* Sets whether the socket is fully connected.
*
* @param connected Whether the socket is connected
*/
2012-07-16 11:44:11 +02:00
void Socket::SetConnected(bool connected)
{
2013-03-02 09:07:47 +01:00
ObjectLock olock(this);
2012-07-16 11:44:11 +02:00
m_Connected = connected;
}
2012-09-14 14:41:17 +02:00
/**
* Checks whether the socket is fully connected.
*
* @returns true if the socket is connected, false otherwise
*/
2012-07-16 11:44:11 +02:00
bool Socket::IsConnected(void) const
{
2013-03-02 09:07:47 +01:00
ObjectLock olock(this);
2012-07-16 11:44:11 +02:00
return m_Connected;
}
2012-07-17 20:41:06 +02:00
2012-09-14 14:41:17 +02:00
/**
2012-11-22 12:04:32 +01:00
* Returns how much data is available for reading.
*
* @returns The number of bytes available.
*/
size_t Socket::GetAvailableBytes(void) const
{
2013-03-02 09:07:47 +01:00
ObjectLock olock(this);
2012-11-22 12:04:32 +01:00
if (m_Listening)
2013-03-16 21:18:53 +01:00
throw new std::logic_error("Socket does not support GetAvailableBytes().");
2012-11-22 12:04:32 +01:00
2013-03-02 09:07:47 +01:00
return m_RecvQueue->GetAvailableBytes();
2012-11-22 12:04:32 +01:00
}
/**
* Reads data from the socket.
*
* @param buffer The buffer where the data should be stored.
* @param size The size of the buffer.
* @returns The number of bytes read.
*/
size_t Socket::Read(void *buffer, size_t size)
{
{
ObjectLock olock(this);
2012-11-22 12:04:32 +01:00
if (m_Listening)
2013-03-16 21:18:53 +01:00
throw new std::logic_error("Socket does not support Read().");
2012-11-22 12:04:32 +01:00
}
2013-03-10 05:10:51 +01:00
if (m_RecvQueue->GetAvailableBytes() == 0)
CheckException();
return m_RecvQueue->Read(buffer, size);
2012-11-22 12:04:32 +01:00
}
/**
* Peeks at data for the socket.
*
* @param buffer The buffer where the data should be stored.
* @param size The size of the buffer.
* @returns The number of bytes read.
*/
size_t Socket::Peek(void *buffer, size_t size)
{
2013-03-02 09:07:47 +01:00
{
ObjectLock olock(this);
if (m_Listening)
2013-03-16 21:18:53 +01:00
throw new std::logic_error("Socket does not support Peek().");
2013-03-02 09:07:47 +01:00
}
2012-11-22 12:04:32 +01:00
if (m_RecvQueue->GetAvailableBytes() == 0)
CheckException();
2012-11-22 12:04:32 +01:00
return m_RecvQueue->Peek(buffer, size);
2012-11-22 12:04:32 +01:00
}
/**
* Writes data to the socket.
*
* @param buffer The buffer that should be sent.
* @param size The size of the buffer.
*/
void Socket::Write(const void *buffer, size_t size)
{
{
2013-03-02 09:07:47 +01:00
ObjectLock olock(this);
2012-11-22 12:04:32 +01:00
2013-03-02 09:07:47 +01:00
if (m_Listening)
2013-03-16 21:18:53 +01:00
throw new std::logic_error("Socket does not support Write().");
2012-11-22 12:04:32 +01:00
}
2013-03-02 09:07:47 +01:00
m_SendQueue->Write(buffer, size);
2012-11-22 12:04:32 +01:00
}
/**
* Starts listening for incoming client connections.
*/
void Socket::Listen(void)
{
2013-03-11 13:45:08 +01:00
if (listen(GetFD(), SOMAXCONN) < 0) {
2013-03-11 14:03:01 +01:00
#ifndef _WIN32
2013-03-11 13:45:08 +01:00
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("listen")
<< boost::errinfo_errno(errno));
2013-03-11 14:03:01 +01:00
#else /* _WIN32 */
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("listen")
2013-03-18 22:40:40 +01:00
<< errinfo_win32_error(WSAGetLastError()));
2013-03-11 14:03:01 +01:00
#endif /* _WIN32 */
2013-03-11 13:45:08 +01:00
}
2012-11-22 12:04:32 +01:00
{
ObjectLock olock(this);
m_Listening = true;
}
2012-11-22 12:04:32 +01:00
}
void Socket::HandleWritable(void)
{
if (m_Listening)
HandleWritableServer();
else
HandleWritableClient();
}
void Socket::HandleReadable(void)
{
if (m_Listening)
HandleReadableServer();
else
HandleReadableClient();
}
/**
* Processes data that is available for this socket.
*/
void Socket::HandleWritableClient(void)
{
int rc;
char data[1024];
size_t count;
if (!IsConnected())
SetConnected(true);
for (;;) {
2013-03-02 09:07:47 +01:00
count = m_SendQueue->Peek(data, sizeof(data));
2012-11-22 12:04:32 +01:00
2013-03-10 05:10:51 +01:00
if (count == 0) {
if (IsWriteEOF())
CloseInternal();
2013-03-02 09:07:47 +01:00
break;
2013-03-10 05:10:51 +01:00
}
2012-11-22 12:04:32 +01:00
rc = send(GetFD(), data, count, 0);
2013-04-02 11:08:08 +02:00
#ifdef _WIN32
if (rc < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
#else /* _WIN32 */
if (rc < 0 && errno == EAGAIN)
#endif /* _WIN32 */
break;
2013-03-11 13:45:08 +01:00
if (rc <= 0) {
2013-03-11 14:03:01 +01:00
#ifndef _WIN32
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("send")
<< boost::errinfo_errno(errno));
2013-03-11 14:03:01 +01:00
#else /* _WIN32 */
2013-03-11 13:45:08 +01:00
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("send")
2013-03-18 22:40:40 +01:00
<< errinfo_win32_error(WSAGetLastError()));
2013-03-11 14:03:01 +01:00
#endif /* _WIN32 */
2013-03-11 13:45:08 +01:00
}
2012-11-22 12:04:32 +01:00
2013-03-02 09:07:47 +01:00
m_SendQueue->Read(NULL, rc);
2012-11-22 12:04:32 +01:00
}
}
/**
* Processes data that can be written for this socket.
*/
void Socket::HandleReadableClient(void)
{
if (!IsConnected())
SetConnected(true);
bool new_data = false;
for (;;) {
char data[1024];
int rc = recv(GetFD(), data, sizeof(data), 0);
#ifdef _WIN32
if (rc < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
#else /* _WIN32 */
if (rc < 0 && errno == EAGAIN)
#endif /* _WIN32 */
break;
2013-03-11 13:45:08 +01:00
if (rc < 0) {
2013-03-11 14:03:01 +01:00
#ifndef _WIN32
2013-03-11 13:45:08 +01:00
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("recv")
<< boost::errinfo_errno(errno));
2013-03-11 14:03:01 +01:00
#else /* _WIN32 */
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("recv")
2013-03-18 22:40:40 +01:00
<< errinfo_win32_error(WSAGetLastError()));
2013-03-11 14:03:01 +01:00
#endif /* _WIN32 */
2013-03-11 13:45:08 +01:00
}
2012-11-22 12:04:32 +01:00
2013-03-10 05:10:51 +01:00
new_data = true;
2013-03-10 03:09:01 +01:00
2013-03-10 05:10:51 +01:00
if (rc == 0) {
SetReadEOF(true);
2012-11-22 12:04:32 +01:00
2013-03-10 05:10:51 +01:00
break;
}
m_RecvQueue->Write(data, rc);
2012-11-22 12:04:32 +01:00
}
if (new_data)
2013-02-17 19:14:34 +01:00
OnDataAvailable(GetSelf());
2012-11-22 12:04:32 +01:00
}
void Socket::HandleWritableServer(void)
{
2013-03-16 21:18:53 +01:00
throw std::logic_error("This should never happen.");
2012-11-22 12:04:32 +01:00
}
/**
* Accepts a new client and creates a new client object for it
* using the client factory function.
*/
void Socket::HandleReadableServer(void)
{
int fd;
sockaddr_storage addr;
socklen_t addrlen = sizeof(addr);
fd = accept(GetFD(), (sockaddr *)&addr, &addrlen);
2013-03-11 13:45:08 +01:00
if (fd < 0) {
2013-03-11 14:03:01 +01:00
#ifndef _WIN32
2013-03-11 13:45:08 +01:00
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("accept")
<< boost::errinfo_errno(errno));
2013-03-11 13:45:08 +01:00
#else /* _WIN32 */
2013-03-11 14:03:01 +01:00
BOOST_THROW_EXCEPTION(socket_error()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("accept")
2013-03-18 22:40:40 +01:00
<< errinfo_win32_error(WSAGetLastError()));
2013-03-11 13:45:08 +01:00
#endif /* _WIN32 */
}
2012-11-22 12:04:32 +01:00
Socket::Ptr client = boost::make_shared<Socket>();
2012-11-22 12:04:32 +01:00
client->SetFD(fd);
2013-02-17 19:14:34 +01:00
OnNewClient(GetSelf(), client);
2012-11-22 12:04:32 +01:00
}
/**
* Checks whether data should be written for this socket object.
*
* @returns true if the socket should be registered for writing, false otherwise.
*/
bool Socket::WantsToWrite(void) const
{
if (m_Listening)
return WantsToWriteServer();
else
return WantsToWriteClient();
}
/**
* Checks whether data should be read for this socket object.
*
* @returns true if the socket should be registered for reading, false otherwise.
*/
bool Socket::WantsToRead(void) const
{
if (m_Listening)
return WantsToReadServer();
else
return WantsToReadClient();
}
/**
* Checks whether data should be read for this socket.
*
* @returns true
*/
bool Socket::WantsToReadClient(void) const
{
2013-03-10 05:10:51 +01:00
return !IsReadEOF();
2012-11-22 12:04:32 +01:00
}
/**
* Checks whether data should be written for this socket.
*
* @returns true if data should be written, false otherwise.
*/
bool Socket::WantsToWriteClient(void) const
{
2013-03-02 09:07:47 +01:00
if (m_SendQueue->GetAvailableBytes() > 0)
return true;
2012-11-22 12:04:32 +01:00
return (!IsConnected());
}
/**
* Checks whether the TCP server wants to write.
*
* @returns false
*/
bool Socket::WantsToWriteServer(void) const
{
return false;
}
/**
* Checks whether the TCP server wants to read (i.e. accept new clients).
*
* @returns true
2012-09-14 14:41:17 +02:00
*/
2012-11-22 12:04:32 +01:00
bool Socket::WantsToReadServer(void) const
2012-07-17 20:41:06 +02:00
{
2012-11-22 12:04:32 +01:00
return true;
2012-07-17 20:41:06 +02:00
}