2012-03-28 13:24:49 +02:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
list<Socket::WeakPtr> Socket::Sockets;
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
Socket::Socket(void)
|
|
|
|
{
|
|
|
|
m_FD = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket::~Socket(void)
|
|
|
|
{
|
|
|
|
Close(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Socket::Start(void)
|
|
|
|
{
|
2012-04-04 12:22:46 +02:00
|
|
|
OnException += bind_weak(&Socket::ExceptionEventHandler, shared_from_this());
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
Sockets.push_front(static_pointer_cast<Socket>(shared_from_this()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Socket::Stop(void)
|
|
|
|
{
|
|
|
|
Sockets.remove_if(weak_ptr_eq_raw<Socket>(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Socket::SetFD(SOCKET fd)
|
|
|
|
{
|
2012-03-29 13:15:54 +02:00
|
|
|
unsigned long lTrue = 1;
|
|
|
|
|
|
|
|
if (fd != INVALID_SOCKET)
|
|
|
|
ioctlsocket(fd, FIONBIO, &lTrue);
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
m_FD = fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
SOCKET Socket::GetFD(void) const
|
|
|
|
{
|
|
|
|
return m_FD;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Socket::Close(void)
|
|
|
|
{
|
|
|
|
Close(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Socket::Close(bool from_dtor)
|
|
|
|
{
|
|
|
|
if (m_FD != INVALID_SOCKET) {
|
|
|
|
closesocket(m_FD);
|
|
|
|
m_FD = INVALID_SOCKET;
|
|
|
|
|
|
|
|
/* nobody can possibly have a valid event subscription when the destructor has been called */
|
|
|
|
if (!from_dtor) {
|
2012-04-03 15:16:11 +02:00
|
|
|
EventArgs::Ptr ea = make_shared<EventArgs>();
|
2012-03-28 13:24:49 +02:00
|
|
|
ea->Source = shared_from_this();
|
|
|
|
OnClosed(ea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!from_dtor)
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
|
2012-04-04 12:22:46 +02:00
|
|
|
string Socket::FormatErrorCode(int code)
|
|
|
|
{
|
|
|
|
char *message;
|
|
|
|
string result = "Unknown socket error.";
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, 0, (char *)&message, 0, NULL) != 0) {
|
|
|
|
result = string(message);
|
|
|
|
LocalFree(message);
|
|
|
|
}
|
|
|
|
#else /* _WIN32 */
|
|
|
|
if (code != 0)
|
|
|
|
message = strerror(code);
|
|
|
|
|
|
|
|
result = string(message);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Socket::ExceptionEventHandler(EventArgs::Ptr ea)
|
|
|
|
{
|
|
|
|
int opt;
|
|
|
|
socklen_t optlen = sizeof(opt);
|
|
|
|
|
|
|
|
int rc = getsockopt(GetFD(), SOL_SOCKET, SO_ERROR, (char *)&opt, &optlen);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
Close();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opt != 0) {
|
|
|
|
SocketErrorEventArgs::Ptr ea = make_shared<SocketErrorEventArgs>();
|
|
|
|
ea->Code = opt;
|
|
|
|
ea->Message = FormatErrorCode(opt);
|
|
|
|
Close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
void Socket::CloseAllSockets(void)
|
|
|
|
{
|
2012-04-02 20:50:35 +02:00
|
|
|
for (list<Socket::WeakPtr>::iterator i = Sockets.begin(); i != Sockets.end(); ) {
|
|
|
|
Socket::Ptr socket = i->lock();
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (socket == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
socket->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::WantsToRead(void) const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::WantsToWrite(void) const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|