Example #1
0
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "Please provide output file.\n");
    fprintf(stderr, "Example: echo \"hello world\" | ./main test.txt\n");
    exit(1);
  }

  const char *filename = argv[1];

  uv_loop_t *loop = uv_default_loop();

  uv_pipe_init(loop, &stdin_pipe, NOIPC);
  uv_pipe_open(&stdin_pipe, STDIN);

  uv_pipe_init(loop, &stdout_pipe, NOIPC);
  uv_pipe_open(&stdout_pipe, STDOUT);

  uv_fs_t file_req;
  int fd = uv_fs_open(loop, &file_req, filename, O_CREAT | O_RDWR, 0644, NULL);
  uv_pipe_init(loop, &file_pipe, 0);
  uv_pipe_open(&file_pipe, fd);

  // uv_pipe_t subclasses uv_stream_t
  uv_read_start((uv_stream_t*)&stdin_pipe, alloc_cb, read_cb);

  uv_run(loop, UV_RUN_DEFAULT);
  return 0;
}
Example #2
0
int main(int argc, char *argv[])
{
    if (argc < 2) return -1;

    int r = uv_pipe_init(uv_default_loop(), &stdin_pipe, 0);
    assert(r==0);
    uv_pipe_open(&stdin_pipe, 0);

    uv_pipe_init(uv_default_loop(), &stdout_pipe, 0);
    uv_pipe_open(&stdin_pipe, 1);

  printf("%d\n", uv_guess_handle(0));

    uv_fs_t file_req;
    int fd = uv_fs_open(uv_default_loop(), &file_req, argv[1], O_WRONLY | O_CREAT, 0644, NULL);

    uv_pipe_init(uv_default_loop(), &file_pipe, 0);
    uv_pipe_open(&file_pipe, fd);

    printf("hhhhhhhhhh\n");
    uv_read_start((uv_stream_t*) &stdin_pipe, buf_alloc, OnRead);

    uv_run(uv_default_loop(), UV_RUN_DEFAULT);

    return 0;
}
Example #3
0
int run_ipc_send_recv_helper(uv_loop_t* loop, int inprocess) {
  int r;

  is_in_process = inprocess;

  memset(&ctx2, 0, sizeof(ctx2));

  r = uv_pipe_init(loop, &ctx2.listen, 0);
  ASSERT(r == 0);

  r = uv_pipe_init(loop, &ctx2.channel, 1);
  ASSERT(r == 0);

  if (inprocess) {
    r = uv_pipe_bind(&ctx2.listen, TEST_PIPENAME_3);
    ASSERT(r == 0);

    r = uv_listen((uv_stream_t*)&ctx2.listen, SOMAXCONN, listen_cb);
    ASSERT(r == 0);
  } else {
    r = uv_pipe_open(&ctx2.channel, 0);
    ASSERT(r == 0);

    send_recv_start();
  }

  r = uv_run(loop, UV_RUN_DEFAULT);
  ASSERT(r == 0);

  return 0;
}
Example #4
0
void rstream_set_file(RStream *rstream, uv_file file)
{
    rstream->file_type = uv_guess_handle(file);

    if (rstream->free_handle) {
        // If this is the second time we're calling this function, free the
        // previously allocated memory
        if (rstream->fread_idle != NULL) {
            uv_close((uv_handle_t *)rstream->fread_idle, close_cb);
        } else {
            uv_close((uv_handle_t *)rstream->stream, close_cb);
        }
    }

    if (rstream->file_type == UV_FILE) {
        // Non-blocking file reads are simulated with a idle handle that reads
        // in chunks of rstream->buffer_size, giving time for other events to
        // be processed between reads.
        rstream->fread_idle = xmalloc(sizeof(uv_idle_t));
        uv_idle_init(uv_default_loop(), rstream->fread_idle);
        rstream->fread_idle->data = rstream;
    } else {
        // Only pipes are supported for now
        assert(rstream->file_type == UV_NAMED_PIPE
               || rstream->file_type == UV_TTY);
        rstream->stream = xmalloc(sizeof(uv_pipe_t));
        uv_pipe_init(uv_default_loop(), (uv_pipe_t *)rstream->stream, 0);
        uv_pipe_open((uv_pipe_t *)rstream->stream, file);
        rstream->stream->data = rstream;
    }

    rstream->fd = file;
    rstream->free_handle = true;
}
Example #5
0
File: init.c Project: RZEWa60/julia
void *init_stdio_handle(uv_file fd,int readable)
{
    void *handle;
    uv_handle_type type = uv_guess_handle(fd);
    switch(type)
    {
        case UV_TTY:
            handle = malloc(sizeof(uv_tty_t));
            uv_tty_init(jl_io_loop,(uv_tty_t*)handle,fd,readable);
            ((uv_tty_t*)handle)->data=0;
            uv_tty_set_mode((void*)handle,0); //cooked stdio
            break;
        case UV_NAMED_PIPE:
        case UV_FILE:
            handle = malloc(sizeof(uv_pipe_t));
            uv_pipe_init(jl_io_loop, (uv_pipe_t*)handle,(readable?UV_PIPE_READABLE:UV_PIPE_WRITEABLE));
            uv_pipe_open((uv_pipe_t*)handle,fd);
            ((uv_pipe_t*)handle)->data=0;
            break;
        case UV_TCP:
        case UV_UDP:
        default:
            handle=NULL;
            jl_errorf("This type of handle for stdio is not yet supported (%d)!\n",type);
            break;
    }
    return handle;
}
Example #6
0
static int run_worker(uv_loop_t *loop, struct engine *engine)
{
	/* Control sockets or TTY */
	auto_free char *sock_file = NULL;
	uv_pipe_t pipe;
	uv_pipe_init(loop, &pipe, 0);
	pipe.data = engine;
	if (g_interactive) {
		printf("[system] interactive mode\n> ");
		fflush(stdout);
		uv_pipe_open(&pipe, 0);
		uv_read_start((uv_stream_t*) &pipe, tty_alloc, tty_read);
	} else {
		(void) mkdir("tty", S_IRWXU|S_IRWXG);
		sock_file = afmt("tty/%ld", getpid());
		if (sock_file) {
			uv_pipe_bind(&pipe, sock_file);
			uv_listen((uv_stream_t *) &pipe, 16, tty_accept);
		}
	}
	/* Run event loop */
	uv_run(loop, UV_RUN_DEFAULT);
	if (sock_file) {
		unlink(sock_file);
	}
	return kr_ok();
}
Example #7
0
int main() {
    loop = uv_default_loop();

    uv_pipe_init(loop, &queue, 1);
    uv_pipe_open(&queue, 0);
    uv_read2_start((uv_stream_t*)&queue, alloc_buffer, on_new_connection);
    return uv_run(loop, UV_RUN_DEFAULT);
}
Example #8
0
int main(int argc, char **argv) {
    loop = uv_default_loop();

    uv_pipe_init(loop, &stdin_pipe, 0);
    uv_pipe_open(&stdin_pipe, 0);

    uv_pipe_init(loop, &stdout_pipe, 0);
    uv_pipe_open(&stdout_pipe, 1);
    
    uv_fs_t file_req;
    int fd = uv_fs_open(loop, &file_req, argv[1], O_CREAT | O_RDWR, 0644, NULL);
    uv_pipe_init(loop, &file_pipe, 0);
    uv_pipe_open(&file_pipe, fd);

    uv_read_start((uv_stream_t*)&stdin_pipe, alloc_buffer, read_stdin);

    uv_run(loop, UV_RUN_DEFAULT);
    return 0;
}
Example #9
0
/*
 * Class:     com_iwebpp_libuvpp_handles_PipeHandle
 * Method:    _open
 * Signature: (JI)I
 */
extern "C" JNIEXPORT  jint JNICALL Java_com_iwebpp_libuvpp_handles_PipeHandle__1open
  (JNIEnv *env, jobject that, jlong pipe, jint fd) {

  assert(pipe);
  uv_pipe_t* handle = reinterpret_cast<uv_pipe_t*>(pipe);
  int r = 0;uv_pipe_open(handle, fd);
  if (r) {
    ThrowException(env, handle->loop, "uv_pipe_open");
  }
  return r;
}
Example #10
0
/// Sets the underlying file descriptor that will be written to. Only pipes
/// are supported for now.
///
/// @param wstream The `WStream` instance
/// @param file The file descriptor
void wstream_set_file(WStream *wstream, uv_file file)
{
  uv_handle_type type = uv_guess_handle(file);

  assert(type == UV_NAMED_PIPE || type == UV_TTY);
  wstream->stream = xmalloc(sizeof(uv_pipe_t));
  uv_pipe_init(uv_default_loop(), (uv_pipe_t *)wstream->stream, 0);
  uv_pipe_open((uv_pipe_t *)wstream->stream, file);
  wstream->stream->data = NULL;
  handle_set_wstream((uv_handle_t *)wstream->stream, wstream);
  wstream->free_handle = true;
}
Example #11
0
File: test-ipc.c Project: clibs/uv
int ipc_helper_tcp_connection(void) {
  /*
   * This is launched from test-ipc.c. stdin is a duplex channel
   * over which a handle will be transmitted.
   */

  int r;
  struct sockaddr_in addr;
  uv_os_fd_t stdin_handle = uv_convert_fd_to_handle(0);

  r = uv_pipe_init(uv_default_loop(), &channel, 1);
  ASSERT(r == 0);

  uv_pipe_open(&channel, stdin_handle);

  ASSERT(1 == uv_is_readable((uv_stream_t*) &channel));
  ASSERT(1 == uv_is_writable((uv_stream_t*) &channel));
  ASSERT(0 == uv_is_closing((uv_handle_t*) &channel));

  r = uv_tcp_init(uv_default_loop(), &tcp_server);
  ASSERT(r == 0);

  ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));

  r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
  ASSERT(r == 0);

  r = uv_listen((uv_stream_t*)&tcp_server, BACKLOG, ipc_on_connection_tcp_conn);
  ASSERT(r == 0);

  /* Make a connection to the server */
  r = uv_tcp_init(uv_default_loop(), &conn.conn);
  ASSERT(r == 0);

  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));

  r = uv_tcp_connect(&conn.conn_req,
                     (uv_tcp_t*) &conn.conn,
                     (const struct sockaddr*) &addr,
                     connect_child_process_cb);
  ASSERT(r == 0);

  r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  ASSERT(r == 0);

  ASSERT(tcp_conn_read_cb_called == 1);
  ASSERT(tcp_conn_write_cb_called == 1);
  ASSERT(close_cb_called == 4);

  MAKE_VALGRIND_HAPPY();
  return 0;
}
Example #12
0
File: test-ipc.c Project: clibs/uv
int ipc_helper(int listen_after_write) {
  /*
   * This is launched from test-ipc.c. stdin is a duplex channel that we
   * over which a handle will be transmitted.
   */
  struct sockaddr_in addr;
  uv_write_t write_req;
  int r;
  uv_buf_t buf;
  uv_os_fd_t stdin_handle = uv_convert_fd_to_handle(0);

  ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));

  r = uv_pipe_init(uv_default_loop(), &channel, 1);
  ASSERT(r == 0);

  uv_pipe_open(&channel, stdin_handle);

  ASSERT(1 == uv_is_readable((uv_stream_t*) &channel));
  ASSERT(1 == uv_is_writable((uv_stream_t*) &channel));
  ASSERT(0 == uv_is_closing((uv_handle_t*) &channel));

  r = uv_tcp_init(uv_default_loop(), &tcp_server);
  ASSERT(r == 0);

  r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
  ASSERT(r == 0);

  if (!listen_after_write) {
    r = uv_listen((uv_stream_t*)&tcp_server, BACKLOG, ipc_on_connection);
    ASSERT(r == 0);
  }

  buf = uv_buf_init("hello\n", 6);
  r = uv_write2(&write_req, (uv_stream_t*)&channel, &buf, 1,
      (uv_stream_t*)&tcp_server, NULL);
  ASSERT(r == 0);

  if (listen_after_write) {
    r = uv_listen((uv_stream_t*)&tcp_server, BACKLOG, ipc_on_connection);
    ASSERT(r == 0);
  }

  r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  ASSERT(r == 0);

  ASSERT(connection_accepted == 1);
  ASSERT(close_cb_called == 3);

  MAKE_VALGRIND_HAPPY();
  return 0;
}
Example #13
0
int ipc_helper_closed_handle(void) {
  int r;
  struct sockaddr_in addr;
  uv_write_t write_req;
  uv_write_t write_req2;
  uv_buf_t buf;
  char buffer[LARGE_SIZE];

  r = uv_pipe_init(uv_default_loop(), &channel, 1);
  ASSERT(r == 0);

  uv_pipe_open(&channel, 0);

  ASSERT(1 == uv_is_readable((uv_stream_t*) &channel));
  ASSERT(1 == uv_is_writable((uv_stream_t*) &channel));
  ASSERT(0 == uv_is_closing((uv_handle_t*) &channel));

  memset(buffer, '.', LARGE_SIZE);
  buf = uv_buf_init(buffer, LARGE_SIZE);

  r = uv_tcp_init(uv_default_loop(), &tcp_server);
  ASSERT(r == 0);

  ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));

  r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
  ASSERT(r == 0);

  r = uv_write(&write_req,
               (uv_stream_t*)&channel,
               &buf,
               1,
               closed_handle_large_write_cb);
  ASSERT(r == 0);

  r = uv_write2(&write_req2,
                (uv_stream_t*)&channel,
                &buf,
                1,
                (uv_stream_t*)&tcp_server,
                closed_handle_write_cb);
  ASSERT(r == 0);

  uv_close((uv_handle_t*)&tcp_server, NULL);

  r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  ASSERT(r == 0);

  MAKE_VALGRIND_HAPPY();
  return 0;
}
Example #14
0
static int ipc_helper(int listen_after_write) {
  /*
   * This is launched from test-ipc.c. stdin is a duplex channel that we
   * over which a handle will be transmitted. In this initial version only
   * data is transfered over the channel. XXX edit this comment after handle
   * transfer is added.
   */
  
  uv_write_t write_req;
  int r;
  uv_buf_t buf;

  r = uv_pipe_init(uv_default_loop(), &channel, 1);
  ASSERT(r == 0);

  uv_pipe_open(&channel, 0);

  ASSERT(uv_is_readable(&channel));
  ASSERT(uv_is_writable(&channel));

  r = uv_tcp_init(uv_default_loop(), &tcp_server);
  ASSERT(r == 0);

  r = uv_tcp_bind(&tcp_server, uv_ip4_addr("0.0.0.0", TEST_PORT));
  ASSERT(r == 0);

  if (!listen_after_write) {
    r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection);
    ASSERT(r == 0);
  }

  buf = uv_buf_init("hello\n", 6);
  r = uv_write2(&write_req, (uv_stream_t*)&channel, &buf, 1,
      (uv_stream_t*)&tcp_server, NULL);
  ASSERT(r == 0);

  if (listen_after_write) {
    r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection);
    ASSERT(r == 0);
  }

  r = uv_run(uv_default_loop());
  ASSERT(r == 0);

  ASSERT(connection_accepted == 1);
  ASSERT(close_cb_called == 3);

  return 0;
}
Example #15
0
int ipc_helper(int listen_after_write) {
  /*
   * This is launched from test-ipc.c. stdin is a duplex channel that we
   * over which a handle will be transmitted.
   */

  uv_write_t write_req;
  int r;
  uv_buf_t buf;

  r = uv_pipe_init(uv_default_loop(), &channel, 1);
  ASSERT(r == 0);

  uv_pipe_open(&channel, 0);

  ASSERT(uv_is_readable((uv_stream_t*) &channel));
  ASSERT(uv_is_writable((uv_stream_t*) &channel));
  ASSERT(!uv_is_closing((uv_handle_t*) &channel));

  r = uv_tcp_init(uv_default_loop(), &tcp_server);
  ASSERT(r == 0);

  r = uv_tcp_bind(&tcp_server, uv_ip4_addr("0.0.0.0", TEST_PORT));
  ASSERT(r == 0);

  if (!listen_after_write) {
    r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection);
    ASSERT(r == 0);
  }

  buf = uv_buf_init("hello\n", 6);
  r = uv_write2(&write_req, (uv_stream_t*)&channel, &buf, 1,
      (uv_stream_t*)&tcp_server, NULL);
  ASSERT(r == 0);

  if (listen_after_write) {
    r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection);
    ASSERT(r == 0);
  }

  r = uv_run(uv_default_loop());
  ASSERT(r == 0);

  ASSERT(connection_accepted == 1);
  ASSERT(close_cb_called == 3);

  MAKE_VALGRIND_HAPPY();
  return 0;
}
Example #16
0
File: stream.c Project: lucc/neovim
/// Sets the stream associated with `fd` to "blocking" mode.
///
/// @return `0` on success, or `-errno` on failure.
int stream_set_blocking(int fd, bool blocking)
{
  // Private loop to avoid conflict with existing watcher(s):
  //    uv__io_stop: Assertion `loop->watchers[w->fd] == w' failed.
  uv_loop_t loop;
  uv_pipe_t stream;
  uv_loop_init(&loop);
  uv_pipe_init(&loop, &stream, 0);
  uv_pipe_open(&stream, fd);
  int retval = uv_stream_set_blocking((uv_stream_t *)&stream, blocking);
  uv_close((uv_handle_t *)&stream, NULL);
  uv_run(&loop, UV_RUN_NOWAIT);  // not necessary, but couldn't hurt.
  uv_loop_close(&loop);
  return retval;
}
Example #17
0
static PyObject *
Pipe_func_open(Pipe *self, PyObject *args)
{
    int fd;

    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    if (!PyArg_ParseTuple(args, "i:open", &fd)) {
        return NULL;
    }

    uv_pipe_open((uv_pipe_t *)UV_HANDLE(self), fd);

    Py_RETURN_NONE;
}
Example #18
0
uv_err_t uv_pipe_pair(uv_pipe_t* a, uv_pipe_t* b) {
  int fds[2];
  int r;
  uv_err_t err;

  /* Make sure that the mutex is only initialized once. */
  uv_once(&uv__pipe_pair_lock_guard, uv__pipe_pair_lock_init);

  uv_mutex_lock(&uv__pipe_pair_lock);

  r = uv__make_socketpair(fds, UV__F_NONBLOCK | UV__F_IPC);

  if (r) {
    err = uv__new_sys_error(errno);
  } else {
    uv_pipe_open(a, fds[0]);
    uv_pipe_open(b, fds[1]);
    err = uv_ok_;
  }

  uv_mutex_unlock(&uv__pipe_pair_lock);

  return err;
}
Example #19
0
File: test-ipc.c Project: clibs/uv
int ipc_helper_bind_twice(void) {
  /*
   * This is launched from test-ipc.c. stdin is a duplex channel
   * over which two handles will be transmitted.
   */
  struct sockaddr_in addr;
  uv_write_t write_req;
  uv_write_t write_req2;
  int r;
  uv_buf_t buf;
  uv_os_fd_t stdin_handle = uv_convert_fd_to_handle(0);

  ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));

  r = uv_pipe_init(uv_default_loop(), &channel, 1);
  ASSERT(r == 0);

  uv_pipe_open(&channel, stdin_handle);

  ASSERT(1 == uv_is_readable((uv_stream_t*) &channel));
  ASSERT(1 == uv_is_writable((uv_stream_t*) &channel));
  ASSERT(0 == uv_is_closing((uv_handle_t*) &channel));

  buf = uv_buf_init("hello\n", 6);

  r = uv_tcp_init(uv_default_loop(), &tcp_server);
  ASSERT(r == 0);
  r = uv_tcp_init(uv_default_loop(), &tcp_server2);
  ASSERT(r == 0);

  r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
  ASSERT(r == 0);
  r = uv_tcp_bind(&tcp_server2, (const struct sockaddr*) &addr, 0);
  ASSERT(r == 0);

  r = uv_write2(&write_req, (uv_stream_t*)&channel, &buf, 1,
                (uv_stream_t*)&tcp_server, NULL);
  ASSERT(r == 0);
  r = uv_write2(&write_req2, (uv_stream_t*)&channel, &buf, 1,
                (uv_stream_t*)&tcp_server2, NULL);
  ASSERT(r == 0);

  r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  ASSERT(r == 0);

  MAKE_VALGRIND_HAPPY();
  return 0;
}
Example #20
0
void
rig_pb_stream_set_fd_transport(rig_pb_stream_t *stream, int fd)
{
    uv_loop_t *loop = rut_uv_shell_get_loop(stream->shell);

    c_return_if_fail(stream->type == STREAM_TYPE_DISCONNECTED);

    stream->type = STREAM_TYPE_FD;

    uv_pipe_init(loop, &stream->fd.uv_fd_pipe, true /* enable handle passing */);
    stream->fd.uv_fd_pipe.data = stream;

    uv_pipe_open(&stream->fd.uv_fd_pipe, fd);

    set_connected(stream);
}
Example #21
0
int ipc_helper_tcp_connection() {
  /*
   * This is launched from test-ipc.c. stdin is a duplex channel that we
   * over which a handle will be transmitted.
   */

  int r;
  struct sockaddr_in addr;

  r = uv_pipe_init(uv_default_loop(), &channel, 1);
  ASSERT(r == 0);

  uv_pipe_open(&channel, 0);

  ASSERT(uv_is_readable((uv_stream_t*)&channel));
  ASSERT(uv_is_writable((uv_stream_t*)&channel));
  ASSERT(!uv_is_closing((uv_handle_t*)&channel));

  r = uv_tcp_init(uv_default_loop(), &tcp_server);
  ASSERT(r == 0);

  r = uv_tcp_bind(&tcp_server, uv_ip4_addr("0.0.0.0", TEST_PORT));
  ASSERT(r == 0);

  r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection_tcp_conn);
  ASSERT(r == 0);

  /* Make a connection to the server */
  r = uv_tcp_init(uv_default_loop(), &conn.conn);
  ASSERT(r == 0);

  addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  r = uv_tcp_connect(&conn.conn_req, (uv_tcp_t*)&conn.conn, addr, connect_child_process_cb);
  ASSERT(r == 0);

  r = uv_run(uv_default_loop());
  ASSERT(r == 0);

  ASSERT(tcp_conn_read_cb_called == 1);
  ASSERT(tcp_conn_write_cb_called == 1);
  ASSERT(close_cb_called == 4);

  MAKE_VALGRIND_HAPPY();
  return 0;
}
Example #22
0
void *init_stdio_handle(uv_file fd,int readable)
{
    void *handle;
    uv_handle_type type = uv_guess_handle(fd);
    //printf("%d: %d -- %d\n", fd, type);
    switch(type)
    {
        case UV_TTY:
            handle = malloc(sizeof(uv_tty_t));
            if (uv_tty_init(jl_io_loop,(uv_tty_t*)handle,fd,readable)) {
                jl_errorf("Error initializing stdio in uv_tty_init (%d, %d)\n", fd, type);
                abort();
            }
            ((uv_tty_t*)handle)->data=0;
            uv_tty_set_mode((void*)handle,0); //cooked stdio
            break;
        case UV_FILE:
#ifdef _WIN32
            jl_errorf("This type of handle for stdio is not yet supported on Windows (%d, %d)!\n", fd, type);
            handle = NULL;
            break;
#endif
        case UV_NAMED_PIPE:
            handle = malloc(sizeof(uv_pipe_t));
            if (uv_pipe_init(jl_io_loop, (uv_pipe_t*)handle, (readable?UV_PIPE_READABLE:UV_PIPE_WRITEABLE))) {
                jl_errorf("Error initializing stdio in uv_pipe_init (%d, %d)\n", fd, type);
                abort();
            }
            if (uv_pipe_open((uv_pipe_t*)handle,fd)) {
                jl_errorf("Error initializing stdio in uv_pipe_open (%d, %d)\n", fd, type);
                abort();
            }
            ((uv_pipe_t*)handle)->data=0;
            break;
        case UV_TCP:
        case UV_UDP:
        default:
            jl_errorf("This type of handle for stdio is not yet supported (%d, %d)!\n", fd, type);
            handle = NULL;
            break;
    }
    return handle;
}
Example #23
0
int main (int argc, char *argv[])
{
   UNUSED (argc);
   UNUSED (argv);

   uv_loop_t *loop = uv_default_loop ();

#ifdef HF_BASE_APP
   HF::UID::URI *uid = new HF::UID::URI ("hf://base.example.com");
#endif

#ifdef HF_NODE_APP

   if (argc < 2)
   {
      std::cout << "usage : " << argv[0] << " [number]" << std::endl;
      exit (-1);
   }

   std::stringstream ss;
   ss << "hf://node.example.com/" << argv[1];

   HF::UID::URI *uid = new HF::UID::URI (ss.str ());
#endif

   HF::Application::Initialize (transport);
   transport.uid (uid);

   HF::Application::Handle ("?");

   uv_pipe_init (loop, &stdin_pipe, 0);
   uv_pipe_open (&stdin_pipe, 0);

   uv_read_start ((uv_stream_t *) &stdin_pipe, alloc_buffer, read_stdin);

   uv_run (loop, UV_RUN_DEFAULT);

   HF::Application::Save ();

   return 0;
}
Example #24
0
File: pipe.c Project: imclab/pyuv
static PyObject *
Pipe_func_open(Pipe *self, PyObject *args)
{
    int err;
    long fd;

    RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL);
    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    if (!PyArg_ParseTuple(args, "l:open", &fd)) {
        return NULL;
    }

    err = uv_pipe_open(&self->pipe_h, (uv_file)fd);
    if (err < 0) {
        RAISE_UV_EXCEPTION(err, PyExc_PipeError);
        return NULL;
    }

    Py_RETURN_NONE;
}
Example #25
0
void stream_init(Loop *loop, Stream *stream, int fd, uv_stream_t *uvstream,
    void *data)
{
  stream->uvstream = uvstream;

  if (fd >= 0) {
    uv_handle_type type = uv_guess_handle(fd);
    stream->fd = fd;

    if (type == UV_FILE) {
      // Non-blocking file reads are simulated with an idle handle that reads in
      // chunks of the ring buffer size, giving time for other events to be
      // processed between reads.
      uv_idle_init(&loop->uv, &stream->uv.idle);
      stream->uv.idle.data = stream;
    } else {
      assert(type == UV_NAMED_PIPE || type == UV_TTY);
      uv_pipe_init(&loop->uv, &stream->uv.pipe, 0);
      uv_pipe_open(&stream->uv.pipe, fd);
      stream->uvstream = (uv_stream_t *)&stream->uv.pipe;
    }
  }

  if (stream->uvstream) {
    stream->uvstream->data = stream;
  }

  stream->data = data;
  stream->internal_data = NULL;
  stream->fpos = 0;
  stream->curmem = 0;
  stream->maxmem = 0;
  stream->pending_reqs = 0;
  stream->read_cb = NULL;
  stream->write_cb = NULL;
  stream->close_cb = NULL;
  stream->internal_close_cb = NULL;
  stream->closed = false;
  stream->buffer = NULL;
}
Example #26
0
/* stdin is a duplex channel over which a handle is sent.
 * We receive it and send it back where it came from.
 */
int ipc_send_recv_helper(void) {
  int r;

  memset(&ctx, 0, sizeof(ctx));

  r = uv_pipe_init(uv_default_loop(), &ctx.channel, 1);
  ASSERT(r == 0);

  uv_pipe_open(&ctx.channel, 0);
  ASSERT(1 == uv_is_readable((uv_stream_t*)&ctx.channel));
  ASSERT(1 == uv_is_writable((uv_stream_t*)&ctx.channel));
  ASSERT(0 == uv_is_closing((uv_handle_t*)&ctx.channel));

  r = uv_read2_start((uv_stream_t*)&ctx.channel, alloc_cb, read2_cb);
  ASSERT(r == 0);

  r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  ASSERT(r == 0);

  MAKE_VALGRIND_HAPPY();
  return 0;
}
Example #27
0
void stdinReadStart(WrenVM* vm)
{
  if (stdinStream == NULL)
  {
    if (uv_guess_handle(stdinDescriptor) == UV_TTY)
    {
      // stdin is connected to a terminal.
      uv_tty_t* handle = (uv_tty_t*)malloc(sizeof(uv_tty_t));
      uv_tty_init(getLoop(), handle, stdinDescriptor, true);
      stdinStream = (uv_stream_t*)handle;
    }
    else
    {
      // stdin is a pipe or a file.
      uv_pipe_t* handle = (uv_pipe_t*)malloc(sizeof(uv_pipe_t));
      uv_pipe_init(getLoop(), handle, false);
      uv_pipe_open(handle, stdinDescriptor);
      stdinStream = (uv_stream_t*)handle;
    }
  }

  uv_read_start(stdinStream, allocCallback, stdinReadCallback);
  // TODO: Check return.
}
Example #28
0
static int stdio_over_pipes_helper() {
  /* Write several buffers to test that the write order is preserved. */
  char* buffers[] = {
    "he",
    "ll",
    "o ",
    "wo",
    "rl",
    "d",
    "\n"
  };

  uv_write_t write_req[ARRAY_SIZE(buffers)];
  uv_buf_t buf[ARRAY_SIZE(buffers)];
  int r, i;
  uv_loop_t* loop = uv_default_loop();
  
  ASSERT(UV_NAMED_PIPE == uv_guess_handle(0));
  ASSERT(UV_NAMED_PIPE == uv_guess_handle(1));

  r = uv_pipe_init(loop, &stdin_pipe, 0);
  ASSERT(r == 0);
  r = uv_pipe_init(loop, &stdout_pipe, 0);
  ASSERT(r == 0);

  uv_pipe_open(&stdin_pipe, 0);
  uv_pipe_open(&stdout_pipe, 1);

  /* Unref both stdio handles to make sure that all writes complete. */
  uv_unref(loop);
  uv_unref(loop);

  for (i = 0; i < ARRAY_SIZE(buffers); i++) {
    buf[i] = uv_buf_init((char*)buffers[i], strlen(buffers[i]));
  }

  for (i = 0; i < ARRAY_SIZE(buffers); i++) {
    r = uv_write(&write_req[i], (uv_stream_t*)&stdout_pipe, &buf[i], 1,
      after_pipe_write);
    ASSERT(r == 0);
  }

  uv_run(loop);

  ASSERT(after_write_called == 7);
  ASSERT(on_pipe_read_called == 0);
  ASSERT(close_cb_called == 0);

  uv_ref(loop);
  uv_ref(loop);

  r = uv_read_start((uv_stream_t*)&stdin_pipe, on_pipe_read_alloc,
    on_pipe_read);
  ASSERT(r == 0);

  uv_run(loop);

  ASSERT(after_write_called == 7);
  ASSERT(on_pipe_read_called == 1);
  ASSERT(close_cb_called == 2);

  return 0;
}
Example #29
0
/* u2_term_io_init(): initialize terminal.
*/
void
u2_term_io_init()
{
  u2_utty* uty_u = malloc(sizeof(u2_utty));

  if ( u2_yes == u2_Host.ops_u.dem ) {
    uty_u->fid_i = 1;

    uv_pipe_init(u2L, &(uty_u->pop_u), uty_u->fid_i);
    uv_pipe_open(&(uty_u->pop_u), uty_u->fid_i);
  }
  else {
    //  Initialize event processing.  Rawdog it.
    //
    {
      uty_u->fid_i = 0;                       //  stdin, yes we write to it...

      uv_poll_init(u2L, &(uty_u->wax_u), uty_u->fid_i);
      uv_poll_start(&(uty_u->wax_u),
                    UV_READABLE | UV_WRITABLE,
                    _term_poll_cb);
    }

    //  Configure horrible stateful terminfo api.
    //
    {
      if ( 0 != setupterm(0, 2, 0) ) {
        c3_assert(!"init-setupterm");
      }
    }

    //  Load terminfo strings.
    //
    {
      c3_w len_w;

#   define _utfo(way, nam) \
      { \
        uty_u->ufo_u.way.nam##_y = (const c3_y *) tigetstr(#nam); \
        c3_assert(uty_u->ufo_u.way.nam##_y); \
      }

      uty_u->ufo_u.inn.max_w = 0;

#if 1
      _utfo(inn, kcuu1);
      _utfo(inn, kcud1);
      _utfo(inn, kcub1);
      _utfo(inn, kcuf1);

      _utfo(out, clear);
      _utfo(out, el);
      // _utfo(out, el1);
      _utfo(out, ed);
      _utfo(out, bel);
      _utfo(out, cub1);
      _utfo(out, cuf1);
      _utfo(out, cuu1);
      _utfo(out, cud1);
      // _utfo(out, cub);
      // _utfo(out, cuf);
#else
      //  libuv hardcodes an ansi terminal - which doesn't seem to work...
      //
      uty_u->ufo_u.out.clear_y = "\033[H\033[J";
      uty_u->ufo_u.out.el_y = "\033[K";
      uty_u->ufo_u.out.ed_y = "\033[J";
      uty_u->ufo_u.out.bel_y = "\007";
      uty_u->ufo_u.out.cub1_y = "\010";
      uty_u->ufo_u.out.cud1_y = "\033[B";
      uty_u->ufo_u.out.cuu1_y = "\033[A";
      uty_u->ufo_u.out.cuf1_y = "\033[C";
#endif

      //  Terminfo chronically reports the wrong sequence for arrow
      //  keys on xterms.  Drastic fix for ridiculous unacceptable bug.
      //  Yes, we could fix this with smkx/rmkx, but this is retarded as well.
      {
        uty_u->ufo_u.inn.kcuu1_y = (const c3_y*)"\033[A";
        uty_u->ufo_u.inn.kcud1_y = (const c3_y*)"\033[B";
        uty_u->ufo_u.inn.kcuf1_y = (const c3_y*)"\033[C";
        uty_u->ufo_u.inn.kcub1_y = (const c3_y*)"\033[D";
      }

      uty_u->ufo_u.inn.max_w = 0;
      if ( (len_w = strlen((c3_c*)uty_u->ufo_u.inn.kcuu1_y)) >
            uty_u->ufo_u.inn.max_w )
      {
        uty_u->ufo_u.inn.max_w = len_w;
      }
      if ( (len_w = strlen((c3_c*)uty_u->ufo_u.inn.kcud1_y)) >
            uty_u->ufo_u.inn.max_w )
      {
        uty_u->ufo_u.inn.max_w = len_w;
      }
      if ( (len_w = strlen((c3_c*)uty_u->ufo_u.inn.kcub1_y)) >
            uty_u->ufo_u.inn.max_w )
      {
        uty_u->ufo_u.inn.max_w = len_w;
      }
      if ( (len_w = strlen((c3_c*)uty_u->ufo_u.inn.kcuf1_y)) >
            uty_u->ufo_u.inn.max_w )
      {
        uty_u->ufo_u.inn.max_w = len_w;
      }
    }

    //  Load old terminal state to restore.
    //
#if 1
    {
      if ( 0 != tcgetattr(uty_u->fid_i, &uty_u->bak_u) ) {
        c3_assert(!"init-tcgetattr");
      }
      if ( -1 == fcntl(uty_u->fid_i, F_GETFL, &uty_u->cug_i) ) {
        c3_assert(!"init-fcntl");
      }
      uty_u->cug_i &= ~O_NONBLOCK;                // could fix?
      uty_u->nob_i = uty_u->cug_i | O_NONBLOCK;   // O_NDELAY on older unix
    }

    //  Construct raw termios configuration.
    //
    {
      uty_u->raw_u = uty_u->bak_u;

      uty_u->raw_u.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
      uty_u->raw_u.c_iflag &= ~(ICRNL | INPCK | ISTRIP);
      uty_u->raw_u.c_cflag &= ~(CSIZE | PARENB);
      uty_u->raw_u.c_cflag |= CS8;
      uty_u->raw_u.c_oflag &= ~(OPOST);
      uty_u->raw_u.c_cc[VMIN] = 0;
      uty_u->raw_u.c_cc[VTIME] = 0;
    }
#endif

    //  Initialize mirror and accumulator state.
    //
    {
      uty_u->tat_u.mir.lin_w = 0;
      uty_u->tat_u.mir.len_w = 0;
      uty_u->tat_u.mir.cus_w = 0;

      uty_u->tat_u.esc.ape = u2_no;
      uty_u->tat_u.esc.bra = u2_no;

      uty_u->tat_u.fut.len_w = 0;
      uty_u->tat_u.fut.wid_w = 0;
    }
  }

  //  This is terminal 1, linked in host.
  //
  {
    uty_u->tid_l = 1;
    uty_u->out_u = 0;
    uty_u->tou_u = 0;

    uty_u->nex_u = u2_Host.uty_u;
    u2_Host.uty_u = uty_u;
    u2_Host.tem_u = uty_u;
  }

  if ( u2_no == u2_Host.ops_u.dem ) {
    //  Start raw input.
    //
    {
      if ( 0 != tcsetattr(uty_u->fid_i, TCSADRAIN, &uty_u->raw_u) ) {
        c3_assert(!"init-tcsetattr");
      }
      if ( -1 == fcntl(uty_u->fid_i, F_SETFL, uty_u->nob_i) ) {
        c3_assert(!"init-fcntl");
      }
    }
  }
}
Example #30
0
File: luv.c Project: Strongc/luv
LUALIB_API int luaopen_luv(lua_State *L) {

#ifndef WIN32
  signal(SIGPIPE, SIG_IGN);
#endif

  int i;
  uv_loop_t*    loop;
  luv_state_t*  curr;
  luv_object_t* stdfh;

  lua_settop(L, 0);

  /* register decoders */
  lua_pushcfunction(L, luvL_lib_decoder);
  lua_setfield(L, LUA_REGISTRYINDEX, "luv:lib:decoder");

#ifdef USE_ZMQ
  lua_pushcfunction(L, luvL_zmq_ctx_decoder);
  lua_setfield(L, LUA_REGISTRYINDEX, "luv:zmq:decoder");
#endif

  /* luv */
  luvL_new_module(L, "luv", luv_funcs);

  /* luv.thread */
  luvL_new_module(L, "luv_thread", luv_thread_funcs);
  lua_setfield(L, -2, "thread");
  luvL_new_class(L, LUV_THREAD_T, luv_thread_meths);
  lua_pop(L, 1);

  if (!MAIN_INITIALIZED) {
    luvL_thread_init_main(L);
    lua_pop(L, 1);
  }

  /* luv.fiber */
  luvL_new_module(L, "luv_fiber", luv_fiber_funcs);

  /* borrow coroutine.yield (fast on LJ2) */
  lua_getglobal(L, "coroutine");
  lua_getfield(L, -1, "yield");
  lua_setfield(L, -3, "yield");
  lua_pop(L, 1); /* coroutine */

  lua_setfield(L, -2, "fiber");

  luvL_new_class(L, LUV_FIBER_T, luv_fiber_meths);
  lua_pop(L, 1);

  /* luv.codec */
  luvL_new_module(L, "luv_codec", luv_codec_funcs);
  lua_setfield(L, -2, "codec");

  /* luv.timer */
  luvL_new_module(L, "luv_timer", luv_timer_funcs);
  lua_setfield(L, -2, "timer");
  luvL_new_class(L, LUV_TIMER_T, luv_timer_meths);
  lua_pop(L, 1);

  /* luv.idle */
  luvL_new_module(L, "luv_idle", luv_idle_funcs);
  lua_setfield(L, -2, "idle");
  luvL_new_class(L, LUV_IDLE_T, luv_idle_meths);
  lua_pop(L, 1);

  /* luv.fs */
  luvL_new_module(L, "luv_fs", luv_fs_funcs);
  lua_setfield(L, -2, "fs");
  luvL_new_class(L, LUV_FILE_T, luv_file_meths);
  lua_pop(L, 1);

  /* luv.pipe */
  luvL_new_module(L, "luv_pipe", luv_pipe_funcs);
  lua_setfield(L, -2, "pipe");
  luvL_new_class(L, LUV_PIPE_T, luv_stream_meths);
  luaL_register(L, NULL, luv_pipe_meths);
  lua_pop(L, 1);

  /* luv.std{in,out,err} */
  if (!MAIN_INITIALIZED) {
    MAIN_INITIALIZED = 1;
    loop = luvL_event_loop(L);
    curr = luvL_state_self(L);

    const char* stdfhs[] = { "stdin", "stdout", "stderr" };
    for (i = 0; i < 3; i++) {
#ifdef WIN32
      const uv_file fh = GetStdHandle(i == 0 ? STD_INPUT_HANDLE
       : (i == 1 ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE));
#else
      const uv_file fh = i;
#endif
      stdfh = (luv_object_t*)lua_newuserdata(L, sizeof(luv_object_t));
      luaL_getmetatable(L, LUV_PIPE_T);
      lua_setmetatable(L, -2);
      luvL_object_init(curr, stdfh);
      uv_pipe_init(loop, &stdfh->h.pipe, 0);
      uv_pipe_open(&stdfh->h.pipe, fh);
      lua_pushvalue(L, -1);
      lua_setfield(L, LUA_REGISTRYINDEX, stdfhs[i]);
      lua_setfield(L, -2, stdfhs[i]);
    }
  }

  /* luv.net */
  luvL_new_module(L, "luv_net", luv_net_funcs);
  lua_setfield(L, -2, "net");
  luvL_new_class(L, LUV_NET_TCP_T, luv_stream_meths);
  luaL_register(L, NULL, luv_net_tcp_meths);
  lua_pop(L, 1);

  /* luv.process */
  luvL_new_module(L, "luv_process", luv_process_funcs);
  lua_setfield(L, -2, "process");
  luvL_new_class(L, LUV_PROCESS_T, luv_process_meths);
  lua_pop(L, 1);

  /* luv.zmq */
#ifdef USE_ZMQ
  luvL_new_module(L, "luv_zmq", luv_zmq_funcs);
  const luv_const_reg_t* c = luv_zmq_consts;
  for (; c->key; c++) {
    lua_pushinteger(L, c->val);
    lua_setfield(L, -2, c->key);
  }
  lua_setfield(L, -2, "zmq");
  luvL_new_class(L, LUV_ZMQ_CTX_T, luv_zmq_ctx_meths);
  luvL_new_class(L, LUV_ZMQ_SOCKET_T, luv_zmq_socket_meths);
  lua_pop(L, 2);
#endif

  lua_settop(L, 1);
  return 1;
}