From 854d119ff2d5c48e83836247b0c093436fdf29b7 Mon Sep 17 00:00:00 2001 From: rakshasa Date: Sun, 31 Aug 2008 19:01:19 +0000 Subject: [PATCH] * Added missing files. * Inserted more error-checking in kqueue and fixed some potential bugs. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1066 e378c898-3ddf-0310-93e7-cc216c733640 --- src/core/curl_socket.cc | 127 ++++++++++++++++++++++++++++++++++++++++ src/core/curl_socket.h | 70 ++++++++++++++++++++++ src/core/curl_stack.cc | 2 + 3 files changed, 199 insertions(+) create mode 100644 src/core/curl_socket.cc create mode 100644 src/core/curl_socket.h diff --git a/src/core/curl_socket.cc b/src/core/curl_socket.cc new file mode 100644 index 00000000..17a22393 --- /dev/null +++ b/src/core/curl_socket.cc @@ -0,0 +1,127 @@ +// rTorrent - BitTorrent client +// Copyright (C) 2005-2008, Jari Sundell +// +// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// In addition, as a special exception, the copyright holders give +// permission to link the code of portions of this program with the +// OpenSSL library under certain conditions as described in each +// individual source file, and distribute linked combinations +// including the two. +// +// You must obey the GNU General Public License in all respects for +// all of the code used other than OpenSSL. If you modify file(s) +// with this exception, you may extend this exception to your version +// of the file(s), but you are not obligated to do so. If you do not +// wish to do so, delete this exception statement from your version. +// If you delete this exception statement from all source files in the +// program, then also delete it here. +// +// Contact: Jari Sundell +// +// Skomakerveien 33 +// 3185 Skoppum, NORWAY + +#include "config.h" + +#include +#include + +#include +#include + +#include "control.h" + +#include "curl_socket.h" +#include "curl_stack.h" + +namespace core { + +int +CurlSocket::receive_socket(void* easy_handle, curl_socket_t fd, int what, void* userp, void* socketp) { + CurlStack* stack = (CurlStack*)userp; + CurlSocket* socket = (CurlSocket*)socketp; + + if (what == CURL_POLL_REMOVE) { + // We also probably need the special code here as we're not + // guaranteed that the fd will be closed, afaik. + if (socket != NULL) + socket->close(); + + // TODO: Consider the possibility that we'll need to set the + // fd-associated pointer curl holds to NULL. + + delete socket; + return 0; + } + + if (socket == NULL) { + socket = stack->new_socket(fd); + control->poll()->open(socket); + + // No interface for libcurl to signal when it's interested in error events. + // Assume that hence it must always be interested in them. + control->poll()->insert_error(socket); + } + + if (what == CURL_POLL_NONE || what == CURL_POLL_OUT) + control->poll()->remove_read(socket); + else + control->poll()->insert_read(socket); + + if (what == CURL_POLL_NONE || what == CURL_POLL_IN) + control->poll()->remove_write(socket); + else + control->poll()->insert_write(socket); + + return 0; +} + +CurlSocket::~CurlSocket() { + if (m_fileDesc != -1) + throw torrent::internal_error("CurlSocket::~CurlSocket() m_fileDesc != -1."); +} + +void +CurlSocket::close() { + if (m_fileDesc == -1) + throw torrent::internal_error("CurlSocket::close() m_fileDesc == -1."); + + // Extra cleanup to avoid leaving dangling polling events in case + // libcurl decides to leave the fd open for reuse. + control->poll()->remove_read(this); + control->poll()->remove_write(this); + control->poll()->remove_error(this); + + control->poll()->closed(this); + m_fileDesc = -1; +} + +void +CurlSocket::event_read() { + return m_stack->receive_action(this, CURL_CSELECT_IN); +} + +void +CurlSocket::event_write() { + return m_stack->receive_action(this, CURL_CSELECT_OUT); +} + +void +CurlSocket::event_error() { + return m_stack->receive_action(this, CURL_CSELECT_ERR); +} + +} diff --git a/src/core/curl_socket.h b/src/core/curl_socket.h new file mode 100644 index 00000000..eef06972 --- /dev/null +++ b/src/core/curl_socket.h @@ -0,0 +1,70 @@ +// rTorrent - BitTorrent client +// Copyright (C) 2005-2008, Jari Sundell +// +// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// In addition, as a special exception, the copyright holders give +// permission to link the code of portions of this program with the +// OpenSSL library under certain conditions as described in each +// individual source file, and distribute linked combinations +// including the two. +// +// You must obey the GNU General Public License in all respects for +// all of the code used other than OpenSSL. If you modify file(s) +// with this exception, you may extend this exception to your version +// of the file(s), but you are not obligated to do so. If you do not +// wish to do so, delete this exception statement from your version. +// If you delete this exception statement from all source files in the +// program, then also delete it here. +// +// Contact: Jari Sundell +// +// Skomakerveien 33 +// 3185 Skoppum, NORWAY + +#ifndef RTORRENT_CORE_CURL_SOCKET_H +#define RTORRENT_CORE_CURL_SOCKET_H + +#include + +#include "globals.h" + +namespace core { + +class CurlStack; + +class CurlSocket : public torrent::Event { +public: + CurlSocket(int fd, CurlStack* stack) : m_stack(stack) { m_fileDesc = fd; } + ~CurlSocket(); + + void close(); + + static int receive_socket(void* easy_handle, curl_socket_t fd, int what, void* userp, void* socketp); + +private: + CurlSocket(const CurlSocket&); + void operator = (const CurlSocket&); + + virtual void event_read(); + virtual void event_write(); + virtual void event_error(); + + CurlStack* m_stack; +}; + +} + +#endif diff --git a/src/core/curl_stack.cc b/src/core/curl_stack.cc index 021a3383..51ea8076 100644 --- a/src/core/curl_stack.cc +++ b/src/core/curl_stack.cc @@ -203,6 +203,8 @@ CurlStack::global_cleanup() { curl_global_cleanup(); } +// TODO: Is this function supposed to set a per-handle timeout, or is +// it the shortest timeout amongst all handles? int CurlStack::set_timeout(void* handle, long timeout_ms, void* userp) { CurlStack* stack = (CurlStack*)userp;