Commit 2f8dc9c3 authored by Denis Bilenko's avatar Denis Bilenko

remove unused libevent-related .pxi and C sources

parent ab5b3850
__all__ += ['buffer']
cdef extern from "string.h":
void *memchr(void *s, int c, size_t n)
cdef extern from "libevent.h":
struct evbuffer:
char *buf "buffer"
int off
evbuffer *evbuffer_new()
int evbuffer_add(evbuffer *buf, char *p, int len)
char *evbuffer_readline(evbuffer *buf)
void evbuffer_free(evbuffer *buf)
size_t evbuffer_get_length(evbuffer *buffer)
unsigned char *EVBUFFER_PULLUP(evbuffer *buf, size_t size)
int EVBUFFER_DRAIN(evbuffer *buf, size_t len)
cdef class buffer:
"""file-like wrapper for libevent's :class:`evbuffer` structure.
Note, that the wrapper does not own the structure, libevent does.
"""
cdef evbuffer* __obj
def __init__(self, size_t _obj):
self.__obj = <evbuffer*>_obj
property _obj:
def __get__(self):
return <size_t>(self.__obj)
def __len__(self):
if self.__obj:
return evbuffer_get_length(self.__obj)
else:
return 0
def __nonzero__(self):
if self.__obj:
return evbuffer_get_length(self.__obj)
def detach(self):
self.__obj = NULL
def __iter__(self):
return self
def __next__(self):
line = self.readline()
if not line:
raise StopIteration
return line
def read(self, long size=-1):
"""Drain the first *size* bytes from the buffer (or what's left if there are less than *size* bytes).
If *size* is negative, drain the whole buffer.
"""
if not self.__obj:
return ''
cdef long length = evbuffer_get_length(self.__obj)
if size < 0:
size = length
else:
size = min(size, length)
if size <= 0:
return ''
cdef char* data = <char*>EVBUFFER_PULLUP(self.__obj, size)
if not data:
return ''
cdef object result = PyString_FromStringAndSize(data, size)
cdef int res = EVBUFFER_DRAIN(self.__obj, size)
if res:
try:
sys.stderr.write('evbuffer_drain(0x%x, %s) returned %s\n' % (self._obj, size, res))
except:
traceback.print_exc()
return result
def readline(self, size=None):
if not self.__obj:
return ''
cdef char* data = <char*>EVBUFFER_PULLUP(self.__obj, -1)
if not data:
return ''
cdef long length = evbuffer_get_length(self.__obj)
cdef char *nl = <char*> memchr(<void*>data, 10, length) # search for "\n"
if nl:
length = nl - data + 1
cdef object result = PyString_FromStringAndSize(data, length)
cdef int res = EVBUFFER_DRAIN(self.__obj, length)
if res:
try:
sys.stderr.write('evbuffer_drain(0x%x, %s) returned %s\n' % (self._obj, length, res))
except:
traceback.print_exc()
return result
def readlines(self, hint=-1):
return list(self)
def write(self, bytes data):
cdef int result = evbuffer_add(self.__obj, data, len(data))
if result < 0:
raise IOError("evbuffer_add() returned %s" % result)
return result
__all__ += ['dns_init', 'dns_shutdown', 'dns_err_to_string',
'dns_resolve_ipv4', 'dns_resolve_ipv6',
'dns_resolve_reverse', 'dns_resolve_reverse_ipv6']
cdef extern from *:
ctypedef char* const_char_ptr "const char*"
cdef extern from "libevent.h":
ctypedef void (*evdns_handler)(int result, char t, int count, int ttl, void *addrs, void *arg)
int evdns_init()
const_char_ptr evdns_err_to_string(int err)
int evdns_resolve_ipv4(char *name, int flags, evdns_handler callback, void *arg)
int evdns_resolve_ipv6(char *name, int flags, evdns_handler callback, void *arg)
int evdns_resolve_reverse(void *ip, int flags, evdns_handler callback, void *arg)
int evdns_resolve_reverse_ipv6(void *ip, int flags, evdns_handler callback, void *arg)
void evdns_shutdown(int fail_requests)
# Result codes
DNS_ERR_NONE = 0
DNS_ERR_FORMAT = 1
DNS_ERR_SERVERFAILED = 2
DNS_ERR_NOTEXIST = 3
DNS_ERR_NOTIMPL = 4
DNS_ERR_REFUSED = 5
DNS_ERR_TRUNCATED = 65
DNS_ERR_UNKNOWN = 66
DNS_ERR_TIMEOUT = 67
DNS_ERR_SHUTDOWN = 68
# Types
DNS_IPv4_A = 1
DNS_PTR = 2
DNS_IPv6_AAAA = 3
# Flags
DNS_QUERY_NO_SEARCH = 1
def dns_init():
"""Initialize async DNS resolver."""
evdns_init()
def dns_shutdown(int fail_requests=0):
"""Shutdown the async DNS resolver and terminate all active requests."""
evdns_shutdown(fail_requests)
def dns_err_to_string(int err):
cdef const_char_ptr result = evdns_err_to_string(err)
if result:
return result
cdef void __evdns_callback(int code, char type, int count, int ttl, void *addrs, void *arg) with gil:
cdef int i
cdef object callback = <object>arg
Py_DECREF(callback)
cdef object addr
cdef object result
if type == DNS_IPv4_A:
result = []
for i from 0 <= i < count:
addr = PyString_FromStringAndSize(&(<char *>addrs)[i*4], 4)
result.append(addr)
elif type == DNS_IPv6_AAAA:
result = []
for i from 0 <= i < count:
addr = PyString_FromStringAndSize(&(<char *>addrs)[i*16], 16)
result.append(addr)
elif type == DNS_PTR and count == 1: # only 1 PTR possible
result = PyString_FromString((<char **>addrs)[0])
else:
result = None
try:
callback(code, type, ttl, result)
except:
traceback.print_exc()
sys.exc_clear()
def dns_resolve_ipv4(char *name, int flags, object callback):
"""Lookup an A record for a given name.
- *name* -- DNS hostname
- *flags* -- either 0 or DNS_QUERY_NO_SEARCH
- *callback* -- callback with ``(result, type, ttl, addrs)`` prototype
"""
cdef int result = evdns_resolve_ipv4(name, flags, __evdns_callback, <void *>callback)
if result:
raise IOError('evdns_resolve_ipv4(%r, %r) returned %s' % (name, flags, result, ))
Py_INCREF(callback)
def dns_resolve_ipv6(char *name, int flags, object callback):
"""Lookup an AAAA record for a given name.
- *name* -- DNS hostname
- *flags* -- either 0 or DNS_QUERY_NO_SEARCH
- *callback* -- callback with ``(result, type, ttl, addrs)`` prototype
"""
cdef int result = evdns_resolve_ipv6(name, flags, __evdns_callback, <void *>callback)
if result:
raise IOError('evdns_resolve_ip6(%r, %r) returned %s' % (name, flags, result, ))
Py_INCREF(callback)
def dns_resolve_reverse(char* packed_ip, int flags, object callback):
"""Lookup a PTR record for a given IPv4 address.
- *packed_ip* -- IPv4 address (as 4-byte binary string)
- *flags* -- either 0 or DNS_QUERY_NO_SEARCH
- *callback* -- callback with ``(result, type, ttl, addrs)`` prototype
"""
cdef int result = evdns_resolve_reverse(<void *>packed_ip, flags, __evdns_callback, <void *>callback)
if result:
raise IOError('evdns_resolve_reverse(%r, %r) returned %s' % (packed_ip, flags, result, ))
Py_INCREF(callback)
def dns_resolve_reverse_ipv6(char* packed_ip, int flags, object callback):
"""Lookup a PTR record for a given IPv6 address.
- *packed_ip* -- IPv6 address (as 16-byte binary string)
- *flags* -- either 0 or DNS_QUERY_NO_SEARCH
- *callback* -- callback with ``(result, type, ttl, addrs)`` prototype
"""
cdef int result = evdns_resolve_reverse_ipv6(<void *>packed_ip, flags, __evdns_callback, <void *>callback)
if result:
raise IOError('evdns_resolve_reverse_ipv6(%r, %r) returned %s' % (packed_ip, flags, result, ))
Py_INCREF(callback)
This diff is collapsed.
/* Copyright (c) 2009-2010 Denis Bilenko. See LICENSE for details. */
#ifdef WIN32
#include "winsock2.h" // for timeval
#endif
#include "sys/queue.h"
#include "event.h"
#if defined(_EVENT_NUMERIC_VERSION) && _EVENT_NUMERIC_VERSION >= 0x2000000
#if _EVENT_NUMERIC_VERSION >= 0x02000900
#define LIBEVENT_HTTP_MODERN
#endif
#include "event2/event.h"
#include "event2/event_struct.h"
#include "event2/event_compat.h"
#include "event2/http.h"
#include "event2/http_compat.h"
#include "event2/http_struct.h"
#include "event2/buffer.h"
#include "event2/buffer_compat.h"
#include "event2/dns.h"
#include "event2/dns_compat.h"
#include "event2/util.h"
#define EVBUFFER_DRAIN evbuffer_drain
#define EVHTTP_SET_CB evhttp_set_cb
#define EVBUFFER_PULLUP(BUF, SIZE) evbuffer_pullup(BUF, SIZE)
#if _EVENT_NUMERIC_VERSION >= 0x02000500
#define current_base event_global_current_base_
#endif
#else
#include "evhttp.h"
#include "evdns.h"
/* compatibility */
#define evbuffer_get_length EVBUFFER_LENGTH
#define EVBUFFER_PULLUP(BUF, SIZE) EVBUFFER_DATA(BUF)
#define TAILQ_FIRST(head) ((head)->tqh_first)
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
/* functions that return int in libeven2 but void in libevent1 */
#define EVBUFFER_DRAIN(A, B) (evbuffer_drain((A), (B)), 0)
#define EVHTTP_SET_CB(A, B, C, D) (evhttp_set_cb((A), (B), (C), (D)), 0)
#endif
#define TAILQ_GET_NEXT(X) TAILQ_NEXT((X), next)
extern void *current_base;
This diff is collapsed.
char* get_gaierror(int);
PyObject* makesockaddr(int sockfd, void* addr, int addrlen, int proto);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment