了解socket-server,需先学习下socket的server的一般流程,顺着流程去看代码就比较容易了。
1. socket
socket(PF_LOCAL, SOCK_STREAM, 0)
2. bind
bind(fd, (struct sockaddr*) &sa, SUN_LEN(&sa)
3. listen
listen(fd, 5) 可以用poll进行client的connect的监听
4. accept
accept(fd, NULL, NULL)
5. read/write
用accept获取的id进行read/write
6. close
----
pa_socket_server_new_unix()/pa_socket_server_new_ipv4() 创建socket,bind和listen,
pa_socket_server_new()使用mainloop中的poll进行client connect的监听,有connect时,callback()会返回。
callback()中进行accept(),使用pa_iochannel_new()进行输入输出进行监听。
输入输出处理这里比较绕,
s->on_connection(s, io, s->userdata); -> pa_protocol_native_new()中的pa_socket_server_set_callback(p->server, on_connection, p);
->pa_pstream_new()中的io_callback()-> do_something() -> pa_iochannel_read(p->io, d, l) -> pstream_memblock_callback() -> stream->memblockq
----
■ static void callback(struct pa_mainloop_api *mainloop, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata)
数据监听后,accept的处理,使用mainloop对于client进行数据通信的socket的监听
■ struct pa_socket_server* pa_socket_server_new(struct pa_mainloop_api *m, int fd)
使用mainloop中的poll进行数据的监听
■ struct pa_socket_server* pa_socket_server_new_unix(struct pa_mainloop_api *m, const char *filename)
文件socket的创建
■ struct pa_socket_server* pa_socket_server_new_ipv4(struct pa_mainloop_api *m, uint32_t address, uint16_t port)
ip和端口形式的socket的创建
■ void pa_socket_server_free(struct pa_socket_server*s)
释放
■ void pa_socket_server_set_callback(struct pa_socket_server*s, void (*on_connection)(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata), void *userdata)
回调函数