void Socket::send(py::object py_buf) const throw() { // pointer to buffer char* buf = nullptr; // true buffer length int buf_len = 0; // passed in length size int pref_len = 0; // TODO: find a way to use Boost.Python instead if (!PyArg_ParseTuple(py_buf.ptr(), "s#ii", &buf, &buf_len, &pref_len)) { Exception e("Wrong arguments: Socket::send((char*)buf, (int)buf_len)", ""); translateException(e); throw e; } else { if ((pref_len - buf_len) > buf_len) { Exception e("Buffer length must not double real buffer length", ""); translateException(e); throw e; return; } } if (buf == nullptr) { Exception e("Null buffer provided during Socket::send", ""); translateException(e); throw e; } int res; // FIXME: useful python macros? Py_BEGIN_ALLOW_THREADS; res = UDT::send(descriptor_, buf, buf_len, 0); Py_END_ALLOW_THREADS; if (res == UDT::ERROR) { PYUDT_LOG_ERROR("Could not send data through socket " << descriptor_); translateUDTError(); return; } PYUDT_LOG_TRACE("Sent " << buf_len << " byte(s) through socket " << descriptor_ << " that were stored in " << static_cast<void *>(&buf)); }
void Epoll::add_ssock(py::object py_socket, py::object py_flags) throw() { // File descriptor of a system socket SYSSOCKET socket; int flags; try { // Extract the flags flags = py::extract<int>(py_flags); // First, try to extract a file descriptor py::extract<SYSSOCKET> get_syssocket(py_socket); if (get_syssocket.check()) socket = get_syssocket(); else { // Second, try to treat this as a Python TCP socket try { socket = py::call_method<SYSSOCKET>(py_socket.ptr(), "fileno"); } catch (...) { Exception e("Wrong arguments: Epoll::add_ssock((SYSSOCKET)s, "\ "(int)flags)", ""); translateException(e); throw e; } } if (UDT::ERROR == UDT::epoll_add_ssock(id_, socket, &flags)) { PYUDT_LOG_ERROR("Could not add system socket " << socket << " to epoll " << id_); translateUDTError(); return; } } catch (py::error_already_set& err) { std::cerr << parse_python_exception() << std::endl; throw err; } PYUDT_LOG_TRACE("Added system socket " << socket << " to epoll " << id_); }
void Epoll::add_usock(py::object py_socket, py::object py_flags) throw() { Socket* socket; int flags; try { try { socket = py::extract<Socket*>(py_socket); flags = py::extract<int>(py_flags); } catch (...) { Exception e("Wrong arguments: Epoll::add_usock((Socket)s, (int)flags)", ""); translateException(e); throw e; } objmap_[socket->getDescriptor()] = socket; if (UDT::ERROR == UDT::epoll_add_usock(id_, socket->getDescriptor(), &flags)) { PYUDT_LOG_ERROR("Could not add UDT socket " << socket->getDescriptor() << " to epoll " << id_); translateUDTError(); return; } } catch (py::error_already_set& err) { std::cerr << parse_python_exception() << std::endl; throw err; } PYUDT_LOG_TRACE("Added UDT socket " << socket->getDescriptor() << " to epoll " << id_ << " with flag " << flags); }
void Socket::connect(py::object py_address) throw() { char* ip = 0x0; uint16_t port = 0; try { py::tuple addr_tuple = py::extract<py::tuple>(py_address); ip = py::extract<char*>(addr_tuple[0]); port = py::extract<uint16_t>(addr_tuple[1]); } catch (...) { Exception e("Wrong arguments: Socket::connect( (addr, port) )", ""); translateException(e); throw e; } connect(ip, port); }
void Epoll::remove_ssock(py::object py_socket) throw() { SYSSOCKET socket; try { py::extract<SYSSOCKET> get_syssocket(py_socket); if (get_syssocket.check()) socket = get_syssocket(); else { try { socket = py::call_method<SYSSOCKET>(py_socket.ptr(), "fileno"); } catch (...) { Exception e("Wrong arguments: Epoll::remove_ssock((SYSSOCKET)s)", ""); translateException(e); throw e; } } if (UDT::ERROR == UDT::epoll_remove_ssock(id_, socket)) { PYUDT_LOG_ERROR("Could not remove system socket " << socket << " from epoll " << id_); translateUDTError(); return; } } catch (py::error_already_set& err) { std::cerr << parse_python_exception() << std::endl; throw err; } PYUDT_LOG_TRACE("Removed system socket " << socket << " from epoll " << id_); }
void Epoll::add_ssock(py::object py_socket, py::object py_flags) throw() { // file descriptor of a system socket SYSSOCKET socket; int flags; try { try { socket = py::extract<SYSSOCKET>(py_socket); flags = py::extract<int>(py_flags); } catch (...) { Exception e("Wrong arguments: Epoll::add_ssock((SYSSOCKET)s, (int)flags)", ""); translateException(e); throw e; } if (UDT::ERROR == UDT::epoll_add_ssock(id_, socket, &flags)) { PYUDT_LOG_ERROR("Could not add system socket " << socket << " to epoll " << id_); translateUDTError(); return; } } catch (py::error_already_set& err) { std::cerr << parse_python_exception() << std::endl; throw err; } PYUDT_LOG_TRACE("Added system socket " << socket << " to epoll " << id_); }
void Epoll::remove_usock(py::object py_socket) throw() { Socket* socket; try { try { socket = py::extract<Socket*>(py_socket); } catch (...) { Exception e("Wrong arguments: Epoll::remove_usock((Socket)s)", ""); translateException(e); throw e; } objmap_.erase(socket->getDescriptor()); if (UDT::ERROR == UDT::epoll_remove_usock(id_, socket->getDescriptor())) { PYUDT_LOG_ERROR("Could not remove UDT socket " << socket->getDescriptor() << " from epoll " << id_); translateUDTError(); return; } } catch (py::error_already_set& err) { std::cerr << parse_python_exception() << std::endl; throw err; } PYUDT_LOG_TRACE("Removed UDT socket " << socket->getDescriptor() << " from epoll " << id_); }