static PyObject * Stream_func_try_write(Stream *self, PyObject *args) { int err; uv_buf_t buf; Py_buffer view; RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); if (!PyArg_ParseTuple(args, PYUV_BYTES"*:try_write", &view)) { return NULL; } buf = uv_buf_init(view.buf, view.len); err = uv_try_write((uv_stream_t *)UV_HANDLE(self), &buf, 1); if (err < 0) { RAISE_STREAM_EXCEPTION(err, UV_HANDLE(self)); PyBuffer_Release(&view); return NULL; } PyBuffer_Release(&view); return PyInt_FromLong((long)err); }
static PyObject * Stream_func_start_read(Stream *self, PyObject *args) { int err; PyObject *tmp, *callback; tmp = NULL; RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); if (!PyArg_ParseTuple(args, "O:start_read", &callback)) { return NULL; } if (!PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "a callable is required"); return NULL; } err = uv_read_start((uv_stream_t *)UV_HANDLE(self), (uv_alloc_cb)pyuv__alloc_cb, (uv_read_cb)pyuv__stream_read_cb); if (err < 0) { RAISE_STREAM_EXCEPTION(err, UV_HANDLE(self)); return NULL; } tmp = self->on_read_cb; Py_INCREF(callback); self->on_read_cb = callback; Py_XDECREF(tmp); PYUV_HANDLE_INCREF(self); Py_RETURN_NONE; }
static PyObject * pyuv__stream_write_bytes(Stream *self, PyObject *data, PyObject *callback, PyObject *send_handle) { int err; uv_buf_t buf; stream_write_ctx *ctx; Py_buffer *view; ctx = PyMem_Malloc(sizeof *ctx); if (!ctx) { PyErr_NoMemory(); return NULL; } ctx->views = ctx->viewsml; view = &ctx->views[0]; if (PyObject_GetBuffer(data, view, PyBUF_SIMPLE) != 0) { PyMem_Free(ctx); return NULL; } ctx->view_count = 1; ctx->obj = self; ctx->callback = callback; ctx->send_handle = send_handle; Py_INCREF(callback); Py_XINCREF(send_handle); buf = uv_buf_init(view->buf, view->len); if (send_handle != NULL) { ASSERT(UV_HANDLE(self)->type == UV_NAMED_PIPE); err = uv_write2(&ctx->req, (uv_stream_t *)UV_HANDLE(self), &buf, 1, (uv_stream_t *)UV_HANDLE(send_handle), pyuv__stream_write_cb); } else { err = uv_write(&ctx->req, (uv_stream_t *)UV_HANDLE(self), &buf, 1, pyuv__stream_write_cb); } if (err < 0) { RAISE_STREAM_EXCEPTION(err, UV_HANDLE(self)); Py_DECREF(callback); Py_XDECREF(send_handle); PyBuffer_Release(view); PyMem_Free(ctx); return NULL; } /* Increase refcount so that object is not removed before the callback is called */ Py_INCREF(self); Py_RETURN_NONE; }
static PyObject * Stream_func_shutdown(Stream *self, PyObject *args) { int err; stream_shutdown_ctx *ctx; PyObject *callback; ctx = NULL; callback = Py_None; RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); if (!PyArg_ParseTuple(args, "|O:shutdown", &callback)) { return NULL; } ctx = PyMem_Malloc(sizeof *ctx); if (!ctx) { PyErr_NoMemory(); return NULL; } Py_INCREF(callback); ctx->obj = self; ctx->callback = callback; err = uv_shutdown(&ctx->req, (uv_stream_t *)UV_HANDLE(self), pyuv__stream_shutdown_cb); if (err < 0) { RAISE_STREAM_EXCEPTION(err, UV_HANDLE(self)); 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); PyMem_Free(ctx); return NULL; }
static INLINE PyObject * pyuv_stream_write(Stream *self, stream_write_ctx *ctx, Py_buffer *views, uv_buf_t *bufs, int buf_count, PyObject *callback, PyObject *send_handle) { int i, err; Py_INCREF(callback); Py_XINCREF(send_handle); ctx->obj = self; ctx->callback = callback; ctx->send_handle = send_handle; ctx->views = views; ctx->view_count = buf_count; if (send_handle) { err = uv_write2(&ctx->req, (uv_stream_t *)UV_HANDLE(self), bufs, buf_count, (uv_stream_t *)UV_HANDLE(send_handle), on_stream_write); } else { err = uv_write(&ctx->req, (uv_stream_t *)UV_HANDLE(self), bufs, buf_count, on_stream_write); } if (err < 0) { RAISE_STREAM_EXCEPTION(err, UV_HANDLE(self)); 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); Py_XDECREF(send_handle); for (i = 0; i < buf_count; i++) { PyBuffer_Release(&views[i]); } if (ctx->views != ctx->view) { PyMem_Free(views); } PyMem_Free(ctx); return NULL; }
static PyObject * Stream_func_fileno(Stream *self) { int err; uv_os_fd_t fd; RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); err = uv_fileno(UV_HANDLE(self), &fd); if (err < 0) { RAISE_STREAM_EXCEPTION(err, UV_HANDLE(self)); return NULL; } /* us_os_fd_t is a HANDLE on Windows which is a 64-bit data type but which * is guaranteed to contain only values < 2^24. * For more information, see: http://www.viva64.com/en/k/0005/ */ return PyInt_FromLong((long) fd); }
static PyObject * Stream_func_stop_read(Stream *self) { int err; RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL); RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL); err = uv_read_stop((uv_stream_t *)UV_HANDLE(self)); if (err < 0) { RAISE_STREAM_EXCEPTION(err, UV_HANDLE(self)); return NULL; } Py_XDECREF(self->on_read_cb); self->on_read_cb = NULL; PYUV_HANDLE_DECREF(self); Py_RETURN_NONE; }
static PyObject * pyuv__stream_write_sequence(Stream *self, PyObject *data, PyObject *callback, PyObject *send_handle) { int err; stream_write_ctx *ctx; PyObject *data_fast, *item; Py_ssize_t i, j, buf_count; data_fast = PySequence_Fast(data, "data must be an iterable"); if (data_fast == NULL) return NULL; buf_count = PySequence_Fast_GET_SIZE(data_fast); if (buf_count > INT_MAX) { PyErr_SetString(PyExc_ValueError, "iterable is too long"); Py_DECREF(data_fast); return NULL; } if (buf_count == 0) { PyErr_SetString(PyExc_ValueError, "iterable is empty"); Py_DECREF(data_fast); return NULL; } ctx = PyMem_Malloc(sizeof *ctx); if (!ctx) { PyErr_NoMemory(); Py_DECREF(data_fast); return NULL; } ctx->views = ctx->viewsml; if (buf_count > ARRAY_SIZE(ctx->viewsml)) ctx->views = PyMem_Malloc(sizeof(Py_buffer) * buf_count); if (!ctx->views) { PyErr_NoMemory(); PyMem_Free(ctx); Py_DECREF(data_fast); return NULL; } ctx->view_count = buf_count; { STACK_ARRAY(uv_buf_t, bufs, buf_count); for (i = 0; i < buf_count; i++) { item = PySequence_Fast_GET_ITEM(data_fast, i); if (PyObject_GetBuffer(item, &ctx->views[i], PyBUF_SIMPLE) != 0) goto error; bufs[i].base = ctx->views[i].buf; bufs[i].len = ctx->views[i].len; } ctx->obj = self; ctx->callback = callback; ctx->send_handle = send_handle; Py_INCREF(callback); Py_XINCREF(send_handle); if (send_handle != NULL) { ASSERT(UV_HANDLE(self)->type == UV_NAMED_PIPE); err = uv_write2(&ctx->req, (uv_stream_t *)UV_HANDLE(self), bufs, buf_count, (uv_stream_t *)UV_HANDLE(send_handle), pyuv__stream_write_cb); } else { err = uv_write(&ctx->req, (uv_stream_t *)UV_HANDLE(self), bufs, buf_count, pyuv__stream_write_cb); } } if (err < 0) { RAISE_STREAM_EXCEPTION(err, UV_HANDLE(self)); Py_DECREF(callback); Py_XDECREF(send_handle); goto error; } /* Increase refcount so that object is not removed before the callback is called */ Py_INCREF(self); Py_RETURN_NONE; error: for (j = 0; j < i; j++) PyBuffer_Release(&ctx->views[j]); if (ctx->views != ctx->viewsml) PyMem_Free(ctx->views); PyMem_Free(ctx); Py_XDECREF(data_fast); return NULL; }