Can someone please help in me debugging as I am new to libuv ? I am trying to accomplish as follows - Create a server (domain socket) - Create a client - connect to server - pass the file descriptor(some arbitrary handle, xyz) using uv_write2 - server accepts the file descriptor (arbitrary handle, xyz) and uses it to read it.
Attached is my client and server program , I know I am doing some silly mistake somewhere. PS - please rename makefile.c to makefile as it was not allowing me to upload makefile. On Monday, 9 December 2013 14:04:48 UTC+5:30, Pramod Sharma wrote: > What about windows ? > I just want to pass regular file handle(process file handle which is > trying to connect) from the client to server and vice versa so that I can > do some authentication on them. > > On Saturday, 19 October 2013 15:26:51 UTC+5:30, Ben Noordhuis wrote: > >> On Fri, Oct 18, 2013 at 11:09 PM, Pramod Sharma >> <[email protected]> wrote: >> > Thanks. So libuv internally is not doing anything with the descriptor >> > assuming it is a pipe or so ? >> >> Not on UNIX platforms, no. >> > -- You received this message because you are subscribed to the Google Groups "libuv" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/libuv. For more options, visit https://groups.google.com/groups/opt_out.
#include "uv.h"
#include <errno.h>
#include <string.h> /* memset */
#include <fcntl.h>
#include <sys/stat.h>
/* FIXME we shouldn't need to branch in this file */
#if defined(__unix__) || defined(__POSIX__) || \
defined(__APPLE__) || defined(_AIX)
#include <unistd.h> /* unlink, rmdir, etc. */
#else
# include <direct.h>
# include <io.h>
# define unlink _unlink
# define rmdir _rmdir
# define stat _stati64
# define open _open
# define write _write
# define lseek _lseek
# define close _close
#endif
#define UV_TEST_PIPE_NAME "./testpipe"
#define UV_TEST_FILE_HANDLE "./xyz"
#include <stdio.h>
typedef struct client_s {
uv_loop_t *uv_loop;
uv_pipe_t uv_client_pipe;
int fd;
uv_pipe_t uv_fs_pipe;
}client_t ;
client_t self;
static void write2_cb(uv_write_t* req, int status) {
if(status != 0)
printf("write2 cb invovked with non-zero status - uv error <%d>\n", uv_last_error(self.uv_loop));
else
printf("write2 cb invoked status <%d>\n", status);
//uv_close((uv_handle_t *)&self.uv_fs_pipe, NULL);
//close(self.fd);
}
/* libuv callback */
static void connect_cb(uv_connect_t* req, int status) {
printf("uv pipe connect callback with status <%d>\n", status);
if(-1 != (self.fd = open(UV_TEST_FILE_HANDLE, O_RDONLY))) {
uv_write_t uv_write_req;
uv_buf_t uv_buf;
int r = 0;
uv_buf_init(".", 1);
if(0 == (r = uv_pipe_init(self.uv_loop, &self.uv_fs_pipe, 0))) {
if(0 == (r = uv_pipe_open(&self.uv_fs_pipe, self.fd))) {
if(0 != (r = uv_write2(&uv_write_req, (uv_stream_t *)&self.uv_client_pipe, &uv_buf, 1, (uv_stream_t *)&self.uv_fs_pipe, write2_cb)))
printf("uv_write2 failed - uv error <%d> \n", uv_last_error(self.uv_loop));
}
else
printf("uv_pipe_open failed - uv error <%d>\n", uv_last_error(self.uv_loop));
}
else
printf("uv_pipe_init failed - uv error <%d> \n", uv_last_error(self.uv_loop));
}
else
printf("open failed for %s file, create the file.\n", UV_TEST_FILE_HANDLE);
}
int main(int argc, char *argv[]) {
int r = 0;
self.uv_loop = uv_default_loop();
if(0 == (r = uv_pipe_init(self.uv_loop, &self.uv_client_pipe, 1))) {
uv_connect_t uv_connect;
uv_pipe_connect(&uv_connect, &self.uv_client_pipe, UV_TEST_PIPE_NAME, &connect_cb);
uv_run(self.uv_loop, UV_RUN_DEFAULT);
}
else
printf("uv_pipe init failed - uv error <%d> \n", uv_last_error(self.uv_loop));
}
#include "uv.h"
#include <errno.h>
#include <string.h> /* memset */
#include <fcntl.h>
#include <sys/stat.h>
/* FIXME we shouldn't need to branch in this file */
#if defined(__unix__) || defined(__POSIX__) || \
defined(__APPLE__) || defined(_AIX)
#include <unistd.h> /* unlink, rmdir, etc. */
#else
# include <direct.h>
# include <io.h>
# define unlink _unlink
# define rmdir _rmdir
# define stat _stati64
# define open _open
# define write _write
# define lseek _lseek
# define close _close
#endif
#include <stdio.h>
#define UV_TEST_PIPE_NAME "./testpipe"
#define UV_TEST_FILE_HANDLE "./xyz"
typedef struct server_s {
uv_loop_t *uv_loop;
uv_pipe_t uv_server_pipe;
uv_pipe_t uv_client_pipe;
uv_pipe_t uv_fs_pipe;
}server_t ;
server_t self;
static uv_buf_t read2_handle_pass_alloc_cb(uv_handle_t* handle, size_t suggested_size) {
/* we're not actually reading anything so a small buffer is okay */
static char buf[8];
return uv_buf_init(buf, sizeof(buf));
}
static void read2_cb(uv_pipe_t* handle,ssize_t nread, uv_buf_t buf, uv_handle_type pending) {
int r;
printf("read2cb callback for handle type %d\n", pending);
if(pending == UV_NAMED_PIPE) {
if(0 == (r = uv_pipe_init(self.uv_loop, &self.uv_fs_pipe, 0))) {
if(0 == (r = uv_accept((uv_stream_t*)handle, (uv_stream_t *)&self.uv_fs_pipe))) {
char buffer[1024] = {0};
r = read(self.uv_client_pipe.io_watcher.fd, buffer, 1024);
printf("handle passed by client fd = %d, bytes = %d , contextnes are %s\n", self.uv_fs_pipe.io_watcher.fd, r, buffer);
}
else
printf("uv_accept failed - uv error <%d> <%s>", uv_last_error(self.uv_loop), uv_strerror(uv_last_error(self.uv_loop)));
}
else
printf("uv_pipe_init failed - uv error <%d> <%s>", uv_last_error(self.uv_loop), uv_strerror(uv_last_error(self.uv_loop)));
}
else {
uv_close((uv_handle_t*)&self.uv_client_pipe,NULL);
printf("handle type is not named pipe\n.");
}
}
/* libuv callback */
static void on_new_connection(uv_stream_t* server, int status) {
int r = 0;
printf("on new connection recieved with status <%d>\n",status);
if(0 == (r = uv_pipe_init(self.uv_loop, &self.uv_client_pipe, 1))) {
if(0 == (r = uv_accept(server, (uv_stream_t *)&self.uv_client_pipe))) {
if( 0 != uv_read2_start((uv_stream_t *)&self.uv_client_pipe, read2_handle_pass_alloc_cb, read2_cb))
printf("uv_read2_start failed - uv error <%d> <%s>", uv_last_error(self.uv_loop), uv_strerror(uv_last_error(self.uv_loop)));
}
else
printf("uv_accept failed - uv error <%d> <%s>", uv_last_error(self.uv_loop), uv_strerror(uv_last_error(self.uv_loop)));
}
else
printf("uv_pipe_init failed - uv error <%d> <%s>", uv_last_error(self.uv_loop), uv_strerror(uv_last_error(self.uv_loop)));
}
int main(int argc, char *argv[]) {
int r = 0;
unlink(UV_TEST_PIPE_NAME);
self.uv_loop = uv_default_loop();
if(0 == (r = uv_pipe_init(self.uv_loop, &self.uv_server_pipe, 0))) {
if(0 == (r = uv_pipe_bind(&self.uv_server_pipe, UV_TEST_PIPE_NAME))) {
if(0 == (r = uv_listen((uv_stream_t*)&self.uv_server_pipe, 128, &on_new_connection))) {
uv_run(self.uv_loop, UV_RUN_DEFAULT);
}
else
printf("uv_listen failed - uv error <%d> <%s>", uv_last_error(self.uv_loop), uv_strerror(uv_last_error(self.uv_loop)));
}
else
printf("uv_pipe_bind failed - uv error <%d> <%s>", uv_last_error(self.uv_loop), uv_strerror(uv_last_error(self.uv_loop)));
}
else
printf("uv_pipe_init failed - uv error <%d> <%s>", uv_last_error(self.uv_loop), uv_strerror(uv_last_error(self.uv_loop)));
uv_close((uv_handle_t*)&self.uv_server_pipe,NULL);
}
xyz
Description: Xmol XYZ data
server: gcc -g -o server uv_server.c -I/code/5.0/pramod_r/include/ -L/code/5.0/pramod_r/ -luv -lpthread -ldl -ldl -lc -lm -lrt client: gcc -g -o client uv_client.c -I/code/5.0/pramod_r/include/ -L/code/5.0/pramod_r/ -luv -lpthread -ldl -ldl -lc -lm -lrt all: server client clean: rm client rm server
