使用splice实现高效的代理服务器

很多网络应用场景下, 当原设备与目标设备无法直接建立连接时,这时就需要一台代理服务器进行中转。代理服务器只需要将来自源设备的报文 原封不动的转发给目标设备,而并不需要知道报文的具体内容。在这种情况下,服务器就没必要将客户端的报文读出来。每读取一次都需要从内核态到用户态的一次拷贝,在流量密集型的应用中,这样做显然很消耗服务器性能。而splice就是为了解决这个问题,它实现了不同文件描述符之间的0拷贝操作。关于splice函数的描述如下:

ssize_t splice(int fd_in, loff_t* off_in,int fd_out, loff_t* off_out, size_t len, unsigned int flags);

fd_in参数是带输入的文件描述符

off_in表示从输入数据流的何处开始读取数据,如果fd_in是管道文件描述符,则必须设置为NULL,表示从0开始

fd_out/off_out 表示输出的文件描述符与偏移

len表示要读取的长度

flags 标志,常用的两个标志SPLICE_F_NONBLOCK,表示非阻塞的splice操作,但要依赖于文件描述符本身的阻塞状态;SPLICE_F_MORE表示给内核一个提示,后续splice调用将读取更多数据

使用splice时,fd_in于fd_out必须至少有一个是管道文件描述符

下面给出一个demo,实现两个客户端之间的报文透传

服务器代码:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <pthread.h> #define MAX_EVENT_NUMBER 1024
#define BUFFER_SIZE 10 int connfd[] = {}; int setnonblocking( int fd )
{
int old_option = fcntl( fd, F_GETFL );
int new_option = old_option | O_NONBLOCK;
fcntl( fd, F_SETFL, new_option );
return old_option;
} void addfd( int epollfd, int fd, bool enable_et )
{
epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN;
if( enable_et )
{
event.events |= EPOLLET;
}
epoll_ctl( epollfd, EPOLL_CTL_ADD, fd, &event );
setnonblocking( fd );
} void lt( epoll_event* events, int number, int epollfd, int listenfd )
{
char buf[ BUFFER_SIZE ];
for ( int i = ; i < number; i++ )
{
int sockfd = events[i].data.fd;
if ( sockfd == listenfd )
{
static int conntimes = ;
struct sockaddr_in client_address;
socklen_t client_addrlength = sizeof( client_address );
int fd = accept( listenfd, ( struct sockaddr* )&client_address, &client_addrlength );
addfd( epollfd, fd, false );
if(conntimes <= )
{
connfd[conntimes] = fd;
}
conntimes++;
printf("new conn, %d\n",fd);
}
else if ( events[i].events & EPOLLIN )
{
int pipefd[];
pipe(pipefd);
if(sockfd == connfd[])
{
splice(connfd[], NULL, pipefd[], NULL, ,SPLICE_F_MORE);
splice(pipefd[], NULL, connfd[], NULL, ,SPLICE_F_MORE);
}
else if(sockfd == connfd[])
{
splice(connfd[], NULL, pipefd[], NULL, ,SPLICE_F_MORE);
splice(pipefd[], NULL, connfd[], NULL, ,SPLICE_F_MORE);
}
else
{
int recvlen = read(sockfd,buf,BUFFER_SIZE-);
buf[recvlen] = ;
printf("recv buf : %s\n",buf);
}
close(pipefd[]);
close(pipefd[]);
}
else
{
printf( "something else happened \n" );
}
}
} int main( int argc, char* argv[] )
{
if( argc <= )
{
printf( "usage: %s ip_address port_number\n", basename( argv[] ) );
return ;
}
const char* ip = argv[];
int port = atoi( argv[] ); int ret = ;
struct sockaddr_in address;
bzero( &address, sizeof( address ) );
address.sin_family = AF_INET;
inet_pton( AF_INET, ip, &address.sin_addr );
address.sin_port = htons( port ); int listenfd = socket( PF_INET, SOCK_STREAM, );
assert( listenfd >= ); ret = bind( listenfd, ( struct sockaddr* )&address, sizeof( address ) );
assert( ret != - ); ret = listen( listenfd, );
assert( ret != - ); epoll_event events[ MAX_EVENT_NUMBER ];
int epollfd = epoll_create( );
assert( epollfd != - );
addfd( epollfd, listenfd, true ); while( )
{
int ret = epoll_wait( epollfd, events, MAX_EVENT_NUMBER, - );
if ( ret < )
{
printf( "epoll failure\n" );
break;
}
lt( events, ret, epollfd, listenfd );
}
close( listenfd );
return ;
}

客户端代码:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <poll.h>
#include <fcntl.h> int connectserver(const char *ip, int port)
{
int rcv_size = ;
size_t optlen = sizeof(int);
struct sockaddr_in server_address;
bzero( &server_address, sizeof( server_address ) );
server_address.sin_family = AF_INET;
inet_pton( AF_INET, ip, &server_address.sin_addr );
server_address.sin_port = htons( port );
int sockfd = socket( PF_INET, SOCK_STREAM, );
assert( sockfd >= );
if ( connect( sockfd, ( struct sockaddr* )&server_address, sizeof( server_address ) ) < )
{
printf( "connection failed\n" );
close( sockfd );
return -;
}
return sockfd;
} int main( int argc, char* argv[] )
{
if( argc <= )
{
printf( "usage: %s ip_address port_number\n", argv[] );
return ;
}
const char* ip = argv[];
int port = atoi( argv[] );
int pipefd[];
pipe(pipefd);
int sockfd = connectserver(ip,port);
assert(sockfd >= );
fd_set readsets;
FD_ZERO(&readsets);
while()
{
FD_SET(STDIN_FILENO,&readsets);
FD_SET(sockfd, &readsets);
int ret = select(sockfd+ , &readsets,NULL,NULL,NULL);
assert(ret >= );
if(FD_ISSET(STDIN_FILENO, &readsets))
{
splice(STDIN_FILENO, NULL, pipefd[], NULL, ,SPLICE_F_MORE);
splice(pipefd[], NULL, sockfd, NULL, ,SPLICE_F_MORE);
}
else if(FD_ISSET(sockfd,&readsets))
{
splice(sockfd, NULL, pipefd[], NULL, ,SPLICE_F_MORE);
splice(pipefd[], NULL, STDOUT_FILENO, NULL, ,SPLICE_F_MORE);
}
}
close(pipefd[]);
close(pipefd[]);
close(sockfd);
return ;
}
上一篇:Python笔记总结week4


下一篇:华为机试题【10】-求数字基root