static PyObject * Stream_func_writelines(Stream *self, PyObject *args) { int r, buf_count; Py_buffer *views; PyObject *callback, *seq, *ret; uv_buf_t *bufs; stream_write_ctx *ctx; callback = Py_None; RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); if (!PyArg_ParseTuple(args, "O|O:writelines", &seq, &callback)) { return NULL; } if (callback != Py_None && !PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "a callable or None is required"); return NULL; } ctx = PyMem_Malloc(sizeof *ctx); if (!ctx) { PyErr_NoMemory(); return NULL; } r = pyseq2uvbuf(seq, &views, &bufs, &buf_count); if (r != 0) { /* error is already set */ PyMem_Free(ctx); return NULL; } ret = pyuv_stream_write(self, ctx, views, bufs, buf_count, callback, NULL); /* uv_write copies the uv_buf_t structures, so we can free them now */ PyMem_Free(bufs); return ret; }
static PyObject * UDP_func_send(UDP *self, PyObject *args) { int r, dest_port, address_type; char *dest_ip; uv_buf_t buf; Py_buffer *view; PyObject *callback = Py_None; uv_udp_send_t *wr = NULL; udp_send_data_t *req_data = NULL; RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); view = PyMem_Malloc(sizeof *view); if (!view) { PyErr_NoMemory(); return NULL; } #ifdef PYUV_PYTHON3 if (!PyArg_ParseTuple(args, "(si)y*|O:send", &dest_ip, &dest_port, view, &callback)) { #else if (!PyArg_ParseTuple(args, "(si)s*|O:send", &dest_ip, &dest_port, view, &callback)) { #endif return NULL; } if (callback != Py_None && !PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "a callable or None is required"); goto error1; } if (dest_port < 0 || dest_port > 65535) { PyErr_SetString(PyExc_ValueError, "port must be between 0 and 65535"); goto error1; } if (pyuv_guess_ip_family(dest_ip, &address_type)) { PyErr_SetString(PyExc_ValueError, "invalid IP address"); goto error1; } Py_INCREF(callback); wr = PyMem_Malloc(sizeof *wr); if (!wr) { PyErr_NoMemory(); goto error2; } req_data = PyMem_Malloc(sizeof *req_data); if (!req_data) { PyErr_NoMemory(); goto error2; } buf = uv_buf_init(view->buf, view->len); req_data->callback = callback; req_data->view_count = 1; req_data->views = view; wr->data = (void *)req_data; if (address_type == AF_INET) { r = uv_udp_send(wr, (uv_udp_t *)UV_HANDLE(self), &buf, 1, uv_ip4_addr(dest_ip, dest_port), (uv_udp_send_cb)on_udp_send); } else { r = uv_udp_send6(wr, (uv_udp_t *)UV_HANDLE(self), &buf, 1, uv_ip6_addr(dest_ip, dest_port), (uv_udp_send_cb)on_udp_send); } if (r != 0) { RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_UDPError); goto error2; } /* Increase refcount so that object is not removed before the callback is called */ Py_INCREF(self); Py_RETURN_NONE; error2: Py_DECREF(callback); PyMem_Free(req_data); PyMem_Free(wr); error1: PyBuffer_Release(view); PyMem_Free(view); return NULL; } static PyObject * UDP_func_sendlines(UDP *self, PyObject *args) { int i, r, buf_count, dest_port, address_type; char *dest_ip; PyObject *callback, *seq; Py_buffer *views; uv_buf_t *bufs; uv_udp_send_t *wr = NULL; udp_send_data_t *req_data = NULL; callback = Py_None; RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); if (!PyArg_ParseTuple(args, "(si)O|O:sendlines", &dest_ip, &dest_port, &seq, &callback)) { return NULL; } if (callback != Py_None && !PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "a callable or None is required"); return NULL; } if (dest_port < 0 || dest_port > 65535) { PyErr_SetString(PyExc_ValueError, "port must be between 0 and 65535"); return NULL; } if (pyuv_guess_ip_family(dest_ip, &address_type)) { PyErr_SetString(PyExc_ValueError, "invalid IP address"); return NULL; } r = pyseq2uvbuf(seq, &views, &bufs, &buf_count); if (r != 0) { /* error is already set */ return NULL; } Py_INCREF(callback); wr = PyMem_Malloc(sizeof *wr); if (!wr) { PyErr_NoMemory(); goto error; } req_data = PyMem_Malloc(sizeof *req_data); if (!req_data) { PyErr_NoMemory(); goto error; } req_data->callback = callback; req_data->view_count = buf_count; req_data->views = views; wr->data = (void *)req_data; if (address_type == AF_INET) { r = uv_udp_send(wr, (uv_udp_t *)UV_HANDLE(self), bufs, buf_count, uv_ip4_addr(dest_ip, dest_port), (uv_udp_send_cb)on_udp_send); } else { r = uv_udp_send6(wr, (uv_udp_t *)UV_HANDLE(self), bufs, buf_count, uv_ip6_addr(dest_ip, dest_port), (uv_udp_send_cb)on_udp_send); } /* uv_write copies the uv_buf_t structures, so we can free them now */ PyMem_Free(bufs); bufs = NULL; if (r != 0) { RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_UDPError); goto error; } /* Increase refcount so that object is not removed before the callback is called */ Py_INCREF(self); Py_RETURN_NONE; error: Py_DECREF(callback); for (i = 0; i < buf_count; i++) { PyBuffer_Release(&views[i]); } PyMem_Free(views); PyMem_Free(bufs); PyMem_Free(req_data); PyMem_Free(wr); return NULL; }
static PyObject * UDP_func_sendlines(UDP *self, PyObject *args) { int i, err, buf_count; struct sockaddr_storage ss; PyObject *addr, *callback, *seq; Py_buffer *views; uv_buf_t *bufs; udp_send_ctx *ctx; callback = Py_None; ctx = NULL; RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); if (!PyArg_ParseTuple(args, "OO|O:sendlines", &addr, &seq, &callback)) { return NULL; } if (callback != Py_None && !PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "a callable or None is required"); return NULL; } if (pyuv_parse_addr_tuple(addr, &ss) < 0) { /* Error is set by the function itself */ return NULL; } err = pyseq2uvbuf(seq, &views, &bufs, &buf_count); if (err != 0) { /* error is already set */ return NULL; } Py_INCREF(callback); ctx = PyMem_Malloc(sizeof *ctx); if (!ctx) { PyErr_NoMemory(); goto error; } ctx->callback = callback; ctx->view_count = buf_count; ctx->views = views; err = uv_udp_send(&ctx->req, &self->udp_h, bufs, buf_count, (struct sockaddr *)&ss, (uv_udp_send_cb)on_udp_send); /* uv_write copies the uv_buf_t structures, so we can free them now */ PyMem_Free(bufs); bufs = NULL; if (err < 0) { RAISE_UV_EXCEPTION(err, PyExc_UDPError); goto error; } /* Increase refcount so that object is not removed before the callback is called */ Py_INCREF(self); Py_RETURN_NONE; error: Py_DECREF(callback); for (i = 0; i < buf_count; i++) { PyBuffer_Release(&views[i]); } PyMem_Free(views); PyMem_Free(bufs); PyMem_Free(ctx); return NULL; }
static PyObject * UDP_func_sendlines(UDP *self, PyObject *args) { int i, r, buf_count, dest_port, address_type; char *dest_ip; PyObject *callback, *seq; uv_buf_t *bufs; uv_udp_send_t *wr = NULL; udp_send_data_t *req_data = NULL; callback = Py_None; RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); if (!PyArg_ParseTuple(args, "(si)O|O:sendlines", &dest_ip, &dest_port, &seq, &callback)) { return NULL; } if (callback != Py_None && !PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "a callable or None is required"); return NULL; } if (dest_port < 0 || dest_port > 65535) { PyErr_SetString(PyExc_ValueError, "port must be between 0 and 65535"); return NULL; } if (pyuv_guess_ip_family(dest_ip, &address_type)) { PyErr_SetString(PyExc_ValueError, "invalid IP address"); return NULL; } Py_INCREF(callback); r = pyseq2uvbuf(seq, &bufs, &buf_count); if (r != 0) { /* error is already set */ goto error; } if (buf_count == 0) { PyErr_SetString(PyExc_ValueError, "Sequence is empty"); goto error; } wr = (uv_udp_send_t *)PyMem_Malloc(sizeof(uv_udp_send_t)); if (!wr) { PyErr_NoMemory(); goto error; } req_data = (udp_send_data_t*) PyMem_Malloc(sizeof(udp_send_data_t)); if (!req_data) { PyErr_NoMemory(); goto error; } req_data->callback = callback; req_data->buf_count = buf_count; req_data->data.bufs = bufs; wr->data = (void *)req_data; if (address_type == AF_INET) { r = uv_udp_send(wr, (uv_udp_t *)UV_HANDLE(self), bufs, buf_count, uv_ip4_addr(dest_ip, dest_port), (uv_udp_send_cb)on_udp_send); } else { r = uv_udp_send6(wr, (uv_udp_t *)UV_HANDLE(self), bufs, buf_count, uv_ip6_addr(dest_ip, dest_port), (uv_udp_send_cb)on_udp_send); } if (r != 0) { RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_UDPError); goto error; } Py_RETURN_NONE; error: Py_DECREF(callback); if (bufs) { for (i = 0; i < buf_count; i++) { PyMem_Free(bufs[i].base); } PyMem_Free(bufs); } if (req_data) { PyMem_Free(req_data); } if (wr) { PyMem_Free(wr); } return NULL; }