Libuv库(探讨)---第四节:网络相关

【C++】libevent 、libev、 libuv 、asio、 muduo、 ace 等C++ 网络库 首先排除preactor模式的(难写),即asio和ACE;其次,尽量选用多进程单线程(简单可横向扩展)的,可以参考nginx。 整体感觉:ACE太庞大,asio 太赶时髦。 ACE太过庞大,使得你即便是只使用它的一小部分,也不得不引用它的全部。而且框架一大堆,模式一个加一个,很多编程习惯也要改变。学习曲线太陡,也难以将它作为一个模块集成自己的应用。 asio呢,有个牛大大说它是现时代... 阅读详情

索引目录:https://blog.youkuaiyun.com/knowledgebao/article/details/84776754


目录

系列目录:

Stream基类相关

TTY

PIPE

TCP

UDP

Networking in libuv is not much different from directly using the BSD socket interface, some things are easier, all are non-blocking, but the concepts stay the same. In addition libuv offers utility functions to abstract the annoying, repetitive and low-level tasks like setting up sockets using the BSD socket structures, DNS lookup, and tweaking various socket parameters.The uv_tcp_t and uv_udp_t structures are used for network I/O.

libuv中的网络与直接使用BSD套接字接口没什么不同,有些东西更容易,都是非阻塞的,但概念保持不变。此外,libuv还提供实用程序功能来抽象恼人的,重复的和低级别的任务,例如使用BSD套接字结构设置套接字,DNS查找以及调整各种套接字参数。这里网络相关的主要涉及TCP和UDP,由于TCP是Stream流,而PIPE和TTY也是Stream类,所以他们有共同的结构体,libuv将TCP、PIPE、TTY归为Stream流所以这里一并对PIPE和TTY的相关API也做一个简单介绍。

下边是TCP、PIPE、TTY的结构体。他们有相同的结构  UV_HANDLE_FIELDS,UV_STREAM_FIELDS,所以TCP、PIPE、TTY可以强制转换成Stream类型。

/* uv_stream_t is a subclass of uv_handle_t.
 * uv_stream is an abstract class.
 * uv_stream_t is the parent class of uv_tcp_t, uv_pipe_t and uv_tty_t. */
struct uv_stream_s {
  UV_HANDLE_FIELDS
  UV_STREAM_FIELDS
};
/*uv_pipe_t is a subclass of uv_stream_t.
 * Representing a pipe stream or pipe server. On Windows this is a Named
 * Pipe. On Unix this is a Unix domain socket.*/
struct uv_pipe_s {
  UV_HANDLE_FIELDS
  UV_STREAM_FIELDS
  int ipc; /* non-zero if this pipe is used for passing handles */
  UV_PIPE_PRIVATE_FIELDS
};
/*
 * uv_tty_t is a subclass of uv_stream_t.
 * Representing a stream for the console.
 */
struct uv_tty_s {
  UV_HANDLE_FIELDS
  UV_STREAM_FIELDS
  UV_TTY_PRIVATE_FIELDS
};
/*
 * uv_tcp_t is a subclass of uv_stream_t.
 * Represents a TCP stream or TCP server.
 */
struct uv_tcp_s {
  UV_HANDLE_FIELDS
  UV_STREAM_FIELDS
  UV_TCP_PRIVATE_FIELDS
};

Stream基类相关

这里边的函数可以被PIPE、TTY、TCP调用,不同的类型可能会有一些差异,但是表示的功能相同。官网API:http://docs.libuv.org/en/v1.x/stream.html

int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb)

int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb);
int uv_accept(uv_stream_t* server, uv_stream_t* client);

int uv_read_start(uv_stream_t*,uv_alloc_cb alloc_cb,uv_read_cb read_cb);
int uv_read_stop(uv_stream_t*);

int uv_is_readable(const uv_stream_t* handle);
int uv_is_writable(const uv_stream_t* handle);
int uv_stream_set_blocking(uv_stream_t* handle, int blocking);

int uv_write(uv_write_t* req,uv_stream_t* handle,const uv_buf_t bufs[],unsigned int nbufs,uv_write_cb cb);
int uv_write2(uv_write_t* req,uv_stream_t* handle,const uv_buf_t bufs[],unsigned int nbufs,uv_stream_t* send_handle,uv_write_cb cb);
int uv_try_write(uv_stream_t* handle,const uv_buf_t bufs[],unsigned int nbufs);

size_t uv_stream_get_write_queue_size(const uv_stream_t* stream);

http://docs.libuv.org/en/v1.x/stream.html

uv_shutdown:关闭连接,此关闭会等待写操作完成。

uv_listen:开始监听,backlog表示监听个数。同linux的listen(2)。

uv_accept:

接收请求,一般在uv_connection_cb回调中调用,只允许调用一次。多次会失败。

此接口只能在bind的回调里边处理。server and client must be handles running on the same loop.

uv_read_start:

Read data from an incoming stream. The uv_read_cb callback will be made several times until there is no more data to read or uv_read_stop() is called.

开始读数据

uv_read_stop:

Stop reading data from the stream. The uv_read_cb callback will no longer be called.

This function is idempotent and may be safely called on a stopped stream.

停止读数据

uv_is_readable

Returns 1 if the stream is readable, 0 otherwise.

uv_is_writable:

Returns 1 if the stream is writable, 0 otherwise.

uv_stream_set_blocking:

Enable or disable blocking mode for a stream.

当block被设置为1后,所有的操作都会被置为同步,window仅pipe支持,不要过度依赖此接口,将来libuv可能废弃此接口。

uv_stre此接口在开始位置就应该被设置,当有reg在队列中时,设置可能失效。

am_get_write_queue_size:

Returns stream->write_queue_size.

uv_write:

Write data to stream. Buffers are written in order. Example:

Note:The memory pointed to by the buffers must remain valid until the callback gets called. This also holds foruv_write2().

写数据,buffer空间在回调之前必须保持有效。

uv_write2:

Extended write function for sending handles over a pipe. The pipe must be initialized with ipc == 1.

Note: send_handle must be a TCP socket or pipe, which is a server or a connection (listening or connected state). Bound sockets or pipes will be assumed to be servers.

uv_write的扩展,多一个handle参数。通过pipe发送一个handle,此pipe必须使用ipc==1初始化。hand必须是tcp socket server或者是pipe。

uv_try_write:

Same as uv_write(), but won’t queue a write request if it can’t be completed immediately.

Will return either:

> 0: number of bytes written (can be less than the supplied buffer size).

< 0: negative error code (UV_EAGAIN is returned if no data can be sent immediately).

同uv_write, 并且不关联req的同步写操作,类似于直接调用系统API。

TTY

http://docs.libuv.org/en/v1.x/tty.html

TTY handles represent a stream for the console.

uv_tty_t is a ‘subclass’ of uv_stream_t.

在Linux中,TTY也许是跟终端有关系的最为混乱的术语。TTY是TeleTYpe的一个老缩写。Teletypes,或者teletypewriters,原来指的是电传打字机,是通过串行线用打印机键盘通过阅读和发送信息的东西,和古老的电报机区别并不是很大。之后,当计算机只能以批处理方式运行时(当时穿孔卡片阅读器是唯一一种使程序载入运行的方式),电传打字机成为唯一能够被使用的“实时”输入/输出设备。最终,电传打字机被键盘和显示器终端所取代,但在终端或TTY接插的地方,操作系统仍然需要一个程序来监视串行端口。一个getty“Get TTY”的处理过程是:一个程序监视物理的TTY/终端接口。对一个虚拟网络控制台(VNC)来说,一个伪装的TTY(Pseudo-TTY,即假冒的TTY,也叫做“PTY”)是等价的终端。当你运行一个xterm(终端仿真程序)或GNOME终端程序时,PTY对虚拟的用户或者如xterm一样的伪终端来说,就像是一个TTY在运行。“Pseudo”的意思是“duplicating in a fake way”(用伪造的方法复制),它相比“virtual”或“emulated”更能真实的说明问题。而在的计算中,它却处于被放弃的阶段。

串行端口终端(Serial Port Terminal)是使用计算机串行端口连接的终端设备。计算机把每个串行端口都看作是一个字符设备。有段时间这些串行端口设备通常被称为终端设备,因为那时它的最大用途就是用来连接终端。这些串行端口所对应的设备名称是/dev/tts/0(或/dev/ttyS0),/dev/tts/1(或/dev/ttyS1)等,设备号分别是(4,0),(4,1)等,分别对应于DOS系统下的COM1、COM2等。若要向一个端口发送数据,可以在命令行上把标准输出重定向到这些特殊文件名上即可。例如,在命令行提示符下键入:echo test > /dev/ttyS1会把单词”test”发送到连接在ttyS1(COM2)端口的设备上。

typedef enum {
  /* Initial/normal terminal mode */
  UV_TTY_MODE_NORMAL,
  /* Raw input mode (On Windows, ENABLE_WINDOW_INPUT is also enabled) */
  UV_TTY_MODE_RAW,
  /* Binary-safe I/O mode for IPC (Unix-only) */
  UV_TTY_MODE_IO
} uv_tty_mode_t;

int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd, int readable);
int uv_tty_set_mode(uv_tty_t*, uv_tty_mode_t mode);
int uv_tty_reset_mode(void);
int uv_tty_get_winsize(uv_tty_t*, int* width, int* height);

uv_handle_type uv_guess_handle(uv_file file);

http://docs.libuv.org/en/v1.x/tty.html

uv_tty_init:

Initialize a new TTY stream with the given file descriptor. Usually the file descriptor will be:

0 = stdin

1 = stdout

2 = stderr

readable, specifies if you plan on calling uv_read_start() with this stream. stdin is readable, stdout is not.

On Unix this function will determine the path of the fd of the terminal using ttyname_r(3), open it, and use it if the passed file descriptor refers to a TTY. This lets libuv put the tty in non-blocking mode without affecting other processes that share the tty.

This function is not thread safe on systems that don’t support ioctl TIOCGPTN or TIOCPTYGNAME, for instance OpenBSD and Solaris.

Note: If reopening the TTY fails, libuv falls back to blocking writes for non-readable TTY streams.

Changed in version 1.9.0:: the path of the TTY is determined by ttyname_r(3). In earlier versions libuv opened /dev/ttyinstead.

Changed in version 1.5.0:: trying to initialize a TTY stream with a file descriptor that refers to a file returns UV_EINVAL on UNIX.

uv_tty_set_mode:

the mode is specified as a uv_tty_mode_t value.

Set the TTY using the specified terminal mode.

uv_tty_reset_mode:

To be called when the program exits. Resets TTY settings to default values for the next process to take over.

This function is async signal-safe on Unix platforms but can fail with error code UV_EBUSY if you call it when execution is inside uv_tty_set_mode().

uv_tty_get_winsize:

Gets the current Window size. On success it returns 0.

获取window大小。

PIPE

Pipe handles provide an abstraction over local domain sockets on Unix and named pipes on Windows.

uv_pipe_t is a ‘subclass’ of uv_stream_t.

int uv_pipe_init(uv_loop_t*, uv_pipe_t* handle, int ipc);
int uv_pipe_open(uv_pipe_t*, uv_file file);
int uv_pipe_bind(uv_pipe_t* handle, const char* name);
void uv_pipe_connect(uv_connect_t* req,
                               uv_pipe_t* handle,
                               const char* name,
                               uv_connect_cb cb);
int uv_pipe_getsockname(const uv_pipe_t* handle,
                                  char* buffer,
                                  size_t* size);
int uv_pipe_getpeername(const uv_pipe_t* handle,
                                  char* buffer,
                                  size_t* size);
void uv_pipe_pending_instances(uv_pipe_t* handle, int count);
int uv_pipe_pending_count(uv_pipe_t* handle);
uv_handle_type uv_pipe_pending_type(uv_pipe_t* handle);
int uv_pipe_chmod(uv_pipe_t* handle, int flags);

uv_pipe_init

Initialize a pipe handle. The ipc argument is a boolean to indicate if this pipe will be used for handle passing between processes.

初始化一个pipe,ipc表示是否跨进程。

uv_pipe_open

Open an existing file descriptor or HANDLE as a pipe.

打开以及已经存在的handle。

uv_pipe_bind

Bind the pipe to a file path (Unix) or a name (Windows).

绑定路径(Unix)或者名称(Windows)。

uv_pipe_connect:

Connect to the Unix domain socket or the named pipe.

连接pipe。

uv_pipe_getsockname:

Get the name of the Unix domain socket or the named pipe.

获取pipe名字,buffer不含结束符,size不包含结束符。

uv_pipe_getpeername

Get the name of the Unix domain socket or the named pipe to which the handle is connected.

获取对方pipe的名称。

uv_pipe_pending_instances

Set the number of pending pipe instance handles when the pipe server is waiting for connections.

Note:This setting applies to Windows only.

设置待处理句柄个数,仅适用于window

uv_pipe_pending_count:

uv_pipe_pending_type:

Used to receive handles over IPC pipes.

First - call uv_pipe_pending_count(), if it’s > 0 then initialize a handle of the given type, returned by uv_pipe_pending_type() and call uv_accept(pipe, handle).

Pipe待处理任务个数,uv_accept使用之前调用。

uv_pipe_chmod:

Alters pipe permissions, allowing it to be accessed from processes run by different users. Makes the pipe writable or readable by all users. Mode can be UV_WRITABLE, UV_READABLE or UV_WRITABLE | UV_READABLE. This function is blocking.

此函数是同步的,设置pipe的权限。

TCP

http://docs.libuv.org/en/v1.x/tcp.html

TCP handles are used to represent both TCP streams and servers.

uv_tcp_t is a ‘subclass’ of uv_stream_t.

Server sockets proceed by:

  uv_tcp_init the TCP handle.

  uv_tcp_bind it.

  Call uv_listen on the handle to have a callback invoked whenever a new connection is   established by a client.

  Use uv_accept to accept the connection.

  Use stream operations to communicate with the client.

int uv_tcp_init(uv_loop_t*, uv_tcp_t* handle);
int uv_tcp_init_ex(uv_loop_t*, uv_tcp_t* handle, unsigned int flags);
int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock);
int uv_tcp_nodelay(uv_tcp_t* handle, int enable);
int uv_tcp_keepalive(uv_tcp_t* handle,
                               int enable,
                               unsigned int delay);
int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable);
int uv_tcp_bind(uv_tcp_t* handle,
const struct sockaddr* addr, unsigned int flags);
int uv_tcp_getsockname(const uv_tcp_t* handle,
struct sockaddr* name, int* namelen);
int uv_tcp_getpeername(const uv_tcp_t* handle,
struct sockaddr* name, int* namelen);
int uv_tcp_connect(uv_connect_t* req,uv_tcp_t* handle,
const struct sockaddr* addr, uv_connect_cb cb);

uv_tcp_init:Initialize the handle. No socket is created as of yet.

uv_tcp_init_ex:Initialize the handle with the specified flags. At the moment only the lower 8 bits of the flags parameter are used as the socket domain. A socket will be created for the given domain. If the specified domain is AF_UNSPEC no socket is created, just like uv_tcp_init().

flags:AF_UNIX域、AF_INET域、AF_UNSPEC域等。

uv_tcp_open:Open an existing file descriptor or SOCKET as a TCP handle.

Changed in version 1.2.1: the file descriptor is set to non-blocking mode.

Note: The passed file descriptor or SOCKET is not checked for its type, but it’s required that it represents a valid stream socket.

打开句柄

uv_tcp_nodelay:Enable TCP_NODELAY, which disables Nagle’s algorithm.

https://www.cnblogs.com/wajika/p/6573014.html

uv_tcp_keepalive:Enable / disable TCP keep-alive. delay is the initial delay in seconds, ignored when enable is zero.

uv_tcp_simultaneous_accepts:Enable / disable simultaneous asynchronous accept requests that are queued by the operating system when listening for new TCP connections.

This setting is used to tune a TCP server for the desired performance. Having simultaneous accepts can significantly improve the rate of accepting connections (which is why it is enabled by default) but may lead to uneven load distribution in multi-process setups.

是否开启系统默认的同步接收。默认是开启的,同步接收accett

uv_tcp_bind:Bind the handle to an address and port. addr should point to an initialized struct sockaddr_in or struct sockaddr_in6.

When the port is already taken, you can expect to see an UV_EADDRINUSE error from either uv_tcp_bind(), uv_listen()or uv_tcp_connect(). That is, a successful call to this function does not guarantee that the call to uv_listen() or uv_tcp_connect() will succeed as well.

flags可以包含UV_TCP_IPV6ONLY,在这种情况下,禁用双栈支持并且仅使用IPv6。

uv_tcp_getsockname:Get the current address to which the handle is bound. addr must point to a valid and big enough chunk of memory, structsockaddr_storage is recommended for IPv4 and IPv6 support.

获取本地地址。

uv_tcp_getpeername:Get the address of the peer connected to the handle. addr must point to a valid and big enough chunk of memory, structsockaddr_storage is recommended for IPv4 and IPv6 support.

获取对方的地址。

uv_tcp_connect:Establish an IPv4 or IPv6 TCP connection. Provide an initialized TCP handle and an uninitialized uv_connect_t. addrshould point to an initialized struct sockaddr_in or struct sockaddr_in6.

On Windows if the addr is initialized to point to an unspecified address (0.0.0.0 or ::) it will be changed to point to localhost. This is done to match the behavior of Linux systems.

客户端与tcp的Server建立连接。

UDP

UDP handles encapsulate UDP communication for both clients and servers.

http://docs.libuv.org/en/v1.x/udp.html

int uv_udp_init(uv_loop_t*, uv_udp_t* handle);
int uv_udp_init_ex(uv_loop_t*, uv_udp_t* handle, unsigned int flags);
int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock);
int uv_udp_bind(uv_udp_t* handle,
                          const struct sockaddr* addr,
                          unsigned int flags);
int uv_udp_send(uv_udp_send_t* req,uv_udp_t* handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr* addr, uv_udp_send_cb send_cb);
int uv_udp_try_send(uv_udp_t* handle,const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr* addr);
size_t uv_udp_get_send_queue_size(const uv_udp_t* handle);
size_t uv_udp_get_send_queue_count(const uv_udp_t* handle);

int uv_udp_recv_start(uv_udp_t* handle,uv_alloc_cb alloc_cb, uv_udp_recv_cb recv_cb);
int uv_udp_recv_stop(uv_udp_t* handle);

uv_udp_t:

/* uv_udp_t is a subclass of uv_handle_t. */

struct uv_udp_s {

  UV_HANDLE_FIELDS

  size_t send_queue_size;//待发送字节数

  size_t send_queue_count;//请求发送个数

  UV_UDP_PRIVATE_FIELDS

};

uv_udp_init:

Initialize a new UDP handle. The actual socket is created lazily. Returns 0 on success.

uv_udp_init_ex:

Initialize the handle with the specified flags. At the moment the lower 8 bits of the flags parameter are used as the socket domain. A socket will be created for the given domain. If the specified domain is AF_UNSPEC no socket is created, just like uv_udp_init().

uv_udp_open:

Opens an existing file descriptor or Windows SOCKET as a UDP handle.

Unix only: The only requirement of the sock argument is that it follows the datagram contract (works in unconnected mode, supports sendmsg()/recvmsg(), etc). In other words, other datagram-type sockets like raw sockets or netlink sockets can also be passed to this function.

Changed in version 1.2.1: the file descriptor is set to non-blocking mode.

Note:The passed file descriptor or SOCKET is not checked for its type, but it’s required that it represents a valid datagram socket.

打开init后的udp句柄

uv_udp_bind:
Bind the UDP handle to an IP address and port.

Parameters:handle – UDP handle. Should have been initialized with uv_udp_init().

addr – struct sockaddr_in or struct sockaddr_in6 with the address and port to bind to.

flags – Indicate how the socket will be bound, UV_UDP_IPV6ONLY and UV_UDP_REUSEADDR are supported.

Returns:0 on success, or an error code < 0 on failure.

uv_udp_send:

Send data over the UDP socket. If the socket has not previously been bound with uv_udp_bind() it will be bound to 0.0.0.0 (the “all interfaces” IPv4 address) and a random port number.

On Windows if the addr is initialized to point to an unspecified address (0.0.0.0 or ::) it will be changed to point to localhost. This is done to match the behavior of Linux systems.

Parameters:req – UDP request handle. Need not be initialized(malloc).

handle – UDP handle. Should have been initialized with uv_udp_init().

bufs – List of buffers to send.

nbufs – Number of buffers in bufs.

addr – struct sockaddr_in or struct sockaddr_in6 with the address and port of the remote peer.

send_cb – Callback to invoke when the data has been sent out.

Returns:0 on success, or an error code < 0 on failure.

Changed in version 1.19.0: added 0.0.0.0 and :: to localhost mapping

uv_udp_try_send:

Same as uv_udp_send(), but won’t queue a send request if it can’t be completed immediately.

Returns:>= 0: number of bytes sent (it matches the given buffer size). < 0: negative error code (UV_EAGAIN is returned when the message can’t be sent immediately).

没有send_cb,不会加入发送队列,直接发送,其他同vu_udp_send.

uv_udp_get_send_queue_size:

Returns handle->send_queue_size.

Number of bytes queued for sending. This field strictly shows how much information is currently queued.

 

uv_udp_get_send_queue_count:

Returns handle->send_queue_count.

Number of send requests currently in the queue awaiting to be processed.

队列中的req请求数目。

 

uv_udp_recv_start:

Prepare for receiving data. If the socket has not previously been bound with uv_udp_bind() it is bound to 0.0.0.0 (the “all interfaces” IPv4 address) and a random port number.

Parameters:handle – UDP handle. Should have been initialized with uv_udp_init().

alloc_cb – Callback to invoke when temporary storage is needed.

recv_cb – Callback to invoke with received data.

Returns:0 on success, or an error code < 0 on failure.

 

uv_udp_recv_stop:

Stop listening for incoming datagrams.

Parameters:handle – UDP handle. Should have been initialized with uv_udp_init().

Returns:0 on success, or an error code < 0 on failure.

int uv_udp_getsockname(const uv_udp_t* handle,struct sockaddr* name,int* namelen);
int uv_udp_set_membership(uv_udp_t* handle,const char* multicast_addr,const char* interface_addr,uv_membership membership);
int uv_udp_set_multicast_loop(uv_udp_t* handle, int on);
int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl);
int uv_udp_set_multicast_interface(uv_udp_t* handle, const char* interface_addr);
int uv_udp_set_broadcast(uv_udp_t* handle, int on);
int uv_udp_set_ttl(uv_udp_t* handle, int ttl);
int uv_ip4_addr(const char* ip, int port, struct sockaddr_in* addr);
int uv_ip6_addr(const char* ip, int port, struct sockaddr_in6* addr);
int uv_ip4_name(const struct sockaddr_in* src, char* dst, size_t size);
int uv_ip6_name(const struct sockaddr_in6* src, char* dst, size_t size);
int uv_inet_ntop(int af, const void* src, char* dst, size_t size);
int uv_inet_pton(int af, const char* src, void* dst);

uv_udp_getsockname:

Get the local IP and port of the UDP handle.

Parameters:handle – UDP handle. Should have been initialized with uv_udp_init() and bound.

name – Pointer to the structure to be filled with the address data. In order to support IPv4 and IPv6 struct sockaddr_storage should be used.

namelen – On input it indicates the data of the name field. On output it indicates how much of it was filled.

Returns:0 on success, or an error code < 0 on failure.

uv_udp_set_membership:

Set membership for a multicast address

Parameters:handle – UDP handle. Should have been initialized with uv_udp_init().

multicast_addr – Multicast address to set membership for.

interface_addr – Interface address.

membership – Should be UV_JOIN_GROUP or UV_LEAVE_GROUP.

Returns:0 on success, or an error code < 0 on failure.

uv_udp_set_multicast_loop:

Set IP multicast loop flag. Makes multicast packets loop back to local sockets.

Parameters:handle – UDP handle. Should have been initialized with uv_udp_init().

本机是否接收组播包

on – 1 for on, 0 for off.

Returns:0 on success, or an error code < 0 on failure.

 uv_udp_set_multicast_ttl:

Set the multicast ttl( Time To Live)

Parameters:handle – UDP handle. Should have been initialized with uv_udp_init().

ttl – 1 through 255.

Returns:0 on success, or an error code < 0 on failure.

uv_udp_set_multicast_interface:

Set the multicast interface to send or receive data on.

Parameters:handle – UDP handle. Should have been initialized with uv_udp_init().

interface_addr – interface address.

Returns:0 on success, or an error code < 0 on failure.

uv_udp_set_broadcast:

Set broadcast on or off.

Parameters:handle – UDP handle. Should have been initialized with uv_udp_init().

on – 1 for on, 0 for off.

Returns:0 on success, or an error code < 0 on failure.

uv_udp_set_ttl:

Set the time to live. ( Time To Live)

Parameters:handle – UDP handle. Should have been initialized with uv_udp_init().

ttl – 1 through 255.

Returns:0 on success, or an error code < 0 on failure.

uv_ip4_addr:

Convert a string containing an IPv4 addresses to a binary structure.

ip到addr

uv_ip6_addr:

Convert a string containing an IPv6 addresses to a binary structure.

uv_ip4_name:

Convert a binary structure containing an IPv4 address to a string.

addr到ip

uv_ip6_name:

Convert a binary structure containing an IPv6 address to a string.

uv_inet_ntop://将点分十进制的ip地址转化为用于网络传输的数值格式

uv_inet_pton://将数值格式转化为点分十进制的ip地址格式

Cross-platform IPv6-capable implementation of inet_ntop(3) and inet_pton(3). On success they return 0. In case of error the target dst pointer is unmodified

基于libuv的tcp端口转发(keepalive和超时设置) 基于libuv的tcp端口转发(keepalive和超时设置)前言代码参考文章 前言 网上关于libuv的代码示例较少,官方文档和代码中的示例都只是给了简单的例子,实际使用的时候可能会遇到很多内存泄漏和指针异常的问题 代码 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <uv.h> #define CRTDBG_MAP_ALLOC #include "crtdbg.h" 阅读详情

相关推荐

基于libuv封装网络事件库

官网源码地址libuv是一个跨平台的异步事件驱动库,非阻塞方式执行I/O操作、网络通信、定时器等。主要特点:1) 跨平台支持: 支持windows、linux、mac。2) 事件循环: libuv有一个基于事件循环的模型,不停轮询事件,有事件产生就会进行相应的回调。3) 异步非阻塞IO: 支持文件、socket、管道等多种异步回调。

zqw_4181的博客 558

libuv学习笔记(12)

libuv学习笔记(12) uv_tcp_t数据结构与相关函数(1)

paohui0134的博客 4069

Flink计算pv和uv的通用方法

PV(访问量):即Page View, 即页面浏览量或点击量,用户每次刷新即被计算一次。 UV(独立访客):即Unique Visitor,访问您网站的一台电脑客户端为一个访客。00:00-24:00内相同的客户端只被计算一次。 计算网站App的实时pv和uv,是很常见的统计需求,这里提供通用的计算方法,不同的业务需求只需要小改即可拿来即用。 需求 利用Flink实时统计,从0点到当前的pv、uv。 一、需求分析 从Kafka发送过来的数据含有:时间戳、时间、维度、用户id,需要从不同维度统计从

weixin_54707168的博客 2857

基于libuv的TCP设计

本人一直在寻找一个跨平台的网络库,

wqvbjhc的专栏 2万+

从零构建Elasticsearch MCP代理:自然语言查询背后的技术揭秘

本文深入解析如何从零构建Elasticsearch MCP代理,实现自然语言查询的技术架构与实战应用。通过MCP协议与Elasticsearch的结合,系统能够将自然语言转换为精确的DSL查询,提升非技术用户的数据查询效率。文章详细介绍了核心架构、DSL生成引擎、端到端系统搭建及生产环境最佳实践,为开发者提供全面的技术指南。

lll78的博客 877

调用D3D11硬解码和渲染VideoProcessor版本

在https://blog.youkuaiyun.com/robothn/article/details/78781321一文中使用shader来显示FFMPEG硬解码后的YUV420P,本文调用D3D11的videoprocessor来进行图像空间变换和尺寸变换,取得了较好的效果。 1.初始化D3D11设备、videodevice等数据结构 FFMPEG调用D3D11硬解码,获取其对应的device,videodevice。。。 FFMPEG使用av_hwdevice_find_type_by_name调用D3d11

zlyadvocate的博客 9285

uv_stream_t—流句柄

流句柄提供双工通信通道的抽象。 uv_stream_t是一个抽象类,libuv提供的形式3个实现uv_tcp_t,uv_pipe_t和uv_tty_t。 数据类型 uv_stream_t 流句柄类型。 uv_connect_t 连接请求类型。 uv_shutdown_t 关机请求类型。 uv_write_t 写请求类型。重用这种类型的对象...

xinqingwuji的博客 1432

Windows Server 搭建ftp服务

Windows Server 搭建ftp服务

ch258563的博客 1010

libuv库使用

Settings->AboutSettings->Proyx。

kessity的博客 3086

国产FPGA逆袭!深度评测复旦微RFVU3P5G核心板在相控阵雷达中的表现

本文深度评测了复旦微RFVU3P5G核心板在相控阵雷达中的实战表现。该核心板凭借射频直采架构、高采样率ADC/DAC及丰富的DSP资源,在多通道同步精度、动态范围及系统稳定性等关键指标上表现出色,展现了国产FPGA在高端雷达应用中的硬核实力与供应链自主价值。

b0c1d2的博客 724

ESP-IDF 利用ESP32构建智能音乐播放器(一)---------- 实现SD卡MP3文件的解码与播放

本文详细介绍了如何利用ESP32和ESP-IDF开发框架,从零开始构建一个智能音乐播放器。文章重点讲解了如何通过SD卡模块读取MP3文件,并集成Helix MP3解码库进行软件解码,最终通过I2S接口驱动音频模块实现播放。内容涵盖硬件连接、软件环境搭建、核心解码与输出逻辑,为嵌入式音频开发提供了完整的实战指南。

weixin_29163797的博客 234

Libuv库概述

[-] 序言 本书为谁而写背景本书代码 Libuv 基础 事件循环Event loopsHello World libuv 的默认事件循环Default loop 监视器Watchers 空转Idling 文件系统 读写文件文件系统相关操作Filesystem operations缓冲区与流Buffers and Streams文件变更事件File c

人生代码,代码人生。。。 2510

【模型解读】【代码复现】Segment Anything Model(SAM)

Meta AI的一个新的人工智能模型,只需一次点击就能 "分割出 "任何图像中的任何物体! SAM是一个可提示的分割系统,对不熟悉的物体和图像具有zero-shot的概括性,并且不需要额外的训练。

lalula的博客 2万+

网络编程-libuv介绍

libuv整理

littleboy_IT 4485

[QMT量化交易小白入门]-二十八、有了回测年化收益好的策略,如何在QMT量化实盘呢(附python代码解析)

本专栏主要是介绍QMT的基础用法,常见函数,写策略的方法,也会分享一些量化交易的思路,大概会写100篇左右。QMT的相关资料较少,在使用过程中不断的摸索,遇到了一些问题,记录下来和大家一起沟通,共同进步,自己淋过雨了,希望大家都有一把伞。

木头大左的博客 2927

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Libuv库概述及libevent、libev、libuv对比(转载)

  node.js最初发起于2009年,是一个可以让JavaScript代码脱离浏览器的执行环境,Libuv使用的是Google的V8执行引擎和Mac Lehmann的Libev。Node.js将事件驱动的I/O模型与该模型的编程语言(JavaScript)融合在一起,随着node.js的日益流行,node.js的开发者们也意识到应该让Node.js在Windows平台下也能工作,但是Libev只能在Unix环境下运行,Windows平台上与kqueue(FreeBSD)或者(e)poll(Linux)等内

田螺姑娘的博客 1253

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Libuv 介绍(一)

Libevent、libev、libuv三个网络库,都是c语言实现的异步事件库 libevent :名气最大,应用最广泛,历史悠久的跨平台事件库; libev :较libevent而言,设计更简练,性能更好,但对Windows支持不够好; libuv :开发node的过程中需要一个跨平台的事件库,他们首选了libev,但又要支持Windows,故重新封装了一套,linux下用libev实现,...

qq_35886593的博客 2万+

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

【libuv高效编程】libuv学习超详细教程1——libuv的编译与安装

libuv 是一个高性能的,事件驱动的I/O,并且支持多平台的网络库,接下来就手把手带你进入libuv编译安装的学习...

杰杰的博客 1万+

网络库libevent、libev、libuv对比

Libevent、libev、libuv三个网络库,都是c语言实现的异步事件库Asynchronousevent library)。 异步事件库本质上是提供异步事件通知(Asynchronous Event Notification,AEN)的。异步事件通知机制就是根据发生的事件,调用相应的回调函数进行处理。 事件(Event):事件是异步事件通知机制的核心,比如fd事件、超时事件、信

小麒麟的成长之路 7万+

c 的网络I/O库总结(libevent,libuv,libev,libeio)

linux 的网络I/O库总结(libevent,libuv,libev,libeio)

weixin_38597669的博客 4153

异步I/O库-libuv介绍

libuv是一个跨平台的支持事件驱动的异步I/O的库,使开发者可以以非阻塞的方式执行文件I/O操作、网络通信、子进程管理等。事件循环:libuv有一个基于事件循环的模型,它不断地轮询事件,并在事件发生时调用相应的回调函数。异步I/O:libuv提供了异步文件I/O和网络I/O的接口,使得开发者可以执行I/O操作而不阻塞主线程。线程池:libuv使用线程池来处理一些不能以非阻塞方式执行的I/O操作,如文件系统操作在某些操作系统上。DNS解析:libuv提供了异步DNS解析的接口。

希望能帮助大家,希望大家多多支持,你们的支持是我前进的动力。 4153

网络库libevent、libev、libuv、libhv对比

本文非原创,而是转摘自Libevent、libev、libuv三个网络库,都是c语言实现的异步事件库。异步事件库本质上是提供异步事件通知(Asynchronous Event Notification,AEN)的。异步事件通知机制就是根据发生的事件,调用相应的回调函数进行处理。事件是异步事件通知机制的核心,比如fd事件、超时事件、信号事件、定时器事件。

$firecat利白的代码足迹$ 1万+

【libuv高效编程】libuv学习超详细教程2——libuv框架初窥

libuv的设计框架,从网络I/O看,在linux(unix)平台它可以通过网络的底层epoll作为异步的I/O处理,它的中间有一层抽象层uv__io_t;而在Windows平台可以通过IOCP...

杰杰的博客 7289

libuv 高性能事件驱动库 简介

libuv是一个高性能事件驱动库,屏蔽了各种操作系统的差异从而提供了统一的API。libuv严格使用异步、事件驱动的编程风格。其核心工作是提供事件循环及基于 I/O或其他活动事件的回调机制。libuv库包含了诸如计时器、非阻塞网络支持、异步文件系统访问、线程创建、子进程等核心工具。 libuv有二个主要功能,一个是循环调度模块,也就是异步IO的核心Loop模块,一个是全局的线程池Thread Po...

whatday的专栏 2229

Libuv

Libevent、libev、libuv三个网络库,都是c语言实现的异步事件库 libevent :名气最大,应用最广泛,历史悠久的跨平台事件库; libev :较libevent而言,设计更简练,性能更好,但对Windows支持不够好; libuv :开发node的过程中需要一个跨平台的事件库,他们首选了libev,但又要支持Windows,故重新封装了一套,linux下用libev实现,Windows下用IOCP实现; 功能 由epoll,kqueue,IOCP和事件端口支持的功能齐全的..

QtHalcon 3406
上一篇: Libuv库(探讨)---第三节:文件系统
下一篇: Libuv库(探讨)---第五节:线程相关
knowledgebao
博客等级 码龄15年 401粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值