2013-01-10 19:05:47 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 The Music Player Daemon Project
|
2013-01-10 19:05:47 +01:00
|
|
|
* http://www.musicpd.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, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "SocketMonitor.hxx"
|
|
|
|
#include "Loop.hxx"
|
2013-08-07 10:31:31 +02:00
|
|
|
#include "system/fd_util.h"
|
2013-01-10 19:05:47 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2013-01-30 10:39:17 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <winsock2.h>
|
|
|
|
#else
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
void
|
|
|
|
SocketMonitor::Dispatch(unsigned flags)
|
|
|
|
{
|
|
|
|
flags &= GetScheduledFlags();
|
|
|
|
|
|
|
|
if (flags != 0 && !OnSocketReady(flags) && IsDefined())
|
|
|
|
Cancel();
|
|
|
|
}
|
|
|
|
|
2013-01-10 19:05:47 +01:00
|
|
|
SocketMonitor::~SocketMonitor()
|
|
|
|
{
|
|
|
|
if (IsDefined())
|
2014-01-06 18:02:57 +01:00
|
|
|
Cancel();
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
2013-01-15 22:48:38 +01:00
|
|
|
void
|
|
|
|
SocketMonitor::Open(int _fd)
|
|
|
|
{
|
|
|
|
assert(fd < 0);
|
|
|
|
assert(_fd >= 0);
|
|
|
|
|
|
|
|
fd = _fd;
|
|
|
|
}
|
|
|
|
|
2013-01-27 22:37:01 +01:00
|
|
|
int
|
|
|
|
SocketMonitor::Steal()
|
2013-01-10 19:05:47 +01:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
|
|
|
Cancel();
|
|
|
|
|
2013-01-27 22:37:01 +01:00
|
|
|
int result = fd;
|
2013-01-10 19:05:47 +01:00
|
|
|
fd = -1;
|
|
|
|
|
2013-01-27 22:37:01 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-11-06 18:20:51 +01:00
|
|
|
void
|
|
|
|
SocketMonitor::Abandon()
|
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
2013-11-28 11:37:23 +01:00
|
|
|
int old_fd = fd;
|
2013-11-06 18:20:51 +01:00
|
|
|
fd = -1;
|
2013-11-28 11:37:23 +01:00
|
|
|
loop.Abandon(old_fd, *this);
|
2013-11-06 18:20:51 +01:00
|
|
|
}
|
|
|
|
|
2013-01-27 22:37:01 +01:00
|
|
|
void
|
|
|
|
SocketMonitor::Close()
|
|
|
|
{
|
|
|
|
close_socket(Steal());
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
2013-01-30 10:39:17 +01:00
|
|
|
|
2013-08-07 23:57:30 +02:00
|
|
|
void
|
|
|
|
SocketMonitor::Schedule(unsigned flags)
|
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
|
|
|
if (flags == GetScheduledFlags())
|
|
|
|
return;
|
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
if (scheduled_flags == 0)
|
|
|
|
loop.AddFD(fd, flags, *this);
|
|
|
|
else if (flags == 0)
|
|
|
|
loop.RemoveFD(fd, *this);
|
|
|
|
else
|
|
|
|
loop.ModifyFD(fd, flags, *this);
|
|
|
|
|
|
|
|
scheduled_flags = flags;
|
2013-08-07 23:57:30 +02:00
|
|
|
}
|
|
|
|
|
2013-01-30 10:39:17 +01:00
|
|
|
SocketMonitor::ssize_t
|
|
|
|
SocketMonitor::Read(void *data, size_t length)
|
|
|
|
{
|
2013-08-07 23:56:49 +02:00
|
|
|
assert(IsDefined());
|
|
|
|
|
2013-01-30 10:39:17 +01:00
|
|
|
int flags = 0;
|
|
|
|
#ifdef MSG_DONTWAIT
|
|
|
|
flags |= MSG_DONTWAIT;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return recv(Get(), (char *)data, length, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketMonitor::ssize_t
|
|
|
|
SocketMonitor::Write(const void *data, size_t length)
|
|
|
|
{
|
2013-08-07 23:56:49 +02:00
|
|
|
assert(IsDefined());
|
|
|
|
|
2013-01-30 10:39:17 +01:00
|
|
|
int flags = 0;
|
|
|
|
#ifdef MSG_NOSIGNAL
|
|
|
|
flags |= MSG_NOSIGNAL;
|
|
|
|
#endif
|
|
|
|
#ifdef MSG_DONTWAIT
|
|
|
|
flags |= MSG_DONTWAIT;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return send(Get(), (const char *)data, length, flags);
|
|
|
|
}
|