2013-01-10 19:05:47 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 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.
|
|
|
|
*/
|
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
#include "SocketEvent.hxx"
|
2013-01-10 19:05:47 +01:00
|
|
|
#include "Loop.hxx"
|
2020-10-13 16:11:57 +02:00
|
|
|
#include "event/Features.h"
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2019-07-05 09:59:00 +02:00
|
|
|
#include <utility>
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2020-04-23 16:48:10 +02:00
|
|
|
#ifdef USE_EPOLL
|
|
|
|
#include <cerrno>
|
|
|
|
#endif
|
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
void
|
2020-10-14 14:24:16 +02:00
|
|
|
SocketEvent::Dispatch() noexcept
|
2013-08-07 22:16:59 +02:00
|
|
|
{
|
2020-10-08 20:53:43 +02:00
|
|
|
const unsigned flags = std::exchange(ready_flags, 0) &
|
|
|
|
(GetScheduledFlags() | IMPLICIT_FLAGS);
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2019-04-03 23:23:56 +02:00
|
|
|
if (flags != 0)
|
2020-10-14 14:24:16 +02:00
|
|
|
callback(flags);
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
|
2013-01-15 22:48:38 +01:00
|
|
|
void
|
2020-10-14 14:24:16 +02:00
|
|
|
SocketEvent::Open(SocketDescriptor _fd) noexcept
|
2013-01-15 22:48:38 +01:00
|
|
|
{
|
2017-08-10 18:25:22 +02:00
|
|
|
assert(!fd.IsDefined());
|
|
|
|
assert(_fd.IsDefined());
|
2013-01-15 22:48:38 +01:00
|
|
|
|
|
|
|
fd = _fd;
|
|
|
|
}
|
|
|
|
|
2017-08-10 18:25:22 +02:00
|
|
|
SocketDescriptor
|
2020-10-14 14:24:16 +02:00
|
|
|
SocketEvent::Steal() noexcept
|
2013-01-10 19:05:47 +01:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
|
|
|
Cancel();
|
|
|
|
|
2017-08-10 18:25:22 +02:00
|
|
|
return std::exchange(fd, SocketDescriptor::Undefined());
|
2013-01-27 22:37:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-10-14 14:24:16 +02:00
|
|
|
SocketEvent::Close() noexcept
|
2013-01-27 22:37:01 +01:00
|
|
|
{
|
2017-08-10 18:25:22 +02:00
|
|
|
Steal().Close();
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
2013-01-30 10:39:17 +01:00
|
|
|
|
2019-12-18 17:46:33 +01:00
|
|
|
bool
|
2020-10-14 14:24:16 +02:00
|
|
|
SocketEvent::Schedule(unsigned flags) noexcept
|
2013-08-07 23:57:30 +02:00
|
|
|
{
|
|
|
|
if (flags == GetScheduledFlags())
|
2019-12-18 17:46:33 +01:00
|
|
|
return true;
|
2013-08-07 23:57:30 +02:00
|
|
|
|
2020-10-14 16:19:08 +02:00
|
|
|
assert(IsDefined());
|
|
|
|
|
2019-12-18 17:46:33 +01:00
|
|
|
bool success;
|
2013-08-07 22:16:59 +02:00
|
|
|
if (scheduled_flags == 0)
|
2019-12-18 17:46:33 +01:00
|
|
|
success = loop.AddFD(fd.Get(), flags, *this);
|
2013-08-07 22:16:59 +02:00
|
|
|
else if (flags == 0)
|
2020-10-08 20:53:43 +02:00
|
|
|
success = loop.RemoveFD(fd.Get());
|
2013-08-07 22:16:59 +02:00
|
|
|
else
|
2019-12-18 17:46:33 +01:00
|
|
|
success = loop.ModifyFD(fd.Get(), flags, *this);
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
scheduled_flags = flags;
|
2020-04-23 16:48:10 +02:00
|
|
|
#ifdef USE_EPOLL
|
|
|
|
else if (errno == EBADF || errno == ENOENT)
|
|
|
|
/* the socket was probably closed by somebody else
|
|
|
|
(EBADF) or a new file descriptor with the same
|
|
|
|
number was created but not registered already
|
|
|
|
(ENOENT) - we can assume that there are no
|
|
|
|
scheduled events */
|
|
|
|
/* note that when this happens, we're actually lucky
|
|
|
|
that it has failed - imagine another thread may
|
|
|
|
meanwhile have created something on the same file
|
|
|
|
descriptor number, and has registered it; the
|
|
|
|
epoll_ctl() call above would then have succeeded,
|
|
|
|
but broke the other thread's epoll registration */
|
|
|
|
scheduled_flags = 0;
|
|
|
|
#endif
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2019-12-18 17:46:33 +01:00
|
|
|
return success;
|
2013-08-07 23:57:30 +02:00
|
|
|
}
|