pjsip学習------二
5482 ワード
pjsipの中で最も基礎的で最も重要で、私は2つあると思って、1つはpj_ですioqueue、一つはpj_timer .
今度はioqueueについてお話しします.
pjsipのioqueueは非同期で、同時操作をサポートできるキューです.主にsocket送受信に用いられる.さらにプラットフォームごとに、その最下位の実装は異なります.何種類かあります.
1 .I/O Completion ports ( Windows NT/2000/XP)
2/dev/epoll (linux)
3 select()
次に、pj_について例のプログラムで説明します.ioqueueの作成に使用します.この例のプログラムには2つの部分がある.サーバ側とクライアント側.クライアント側はランダム文字列を送信し、server側は印刷します.
server.c
client.c
今度はioqueueについてお話しします.
pjsipのioqueueは非同期で、同時操作をサポートできるキューです.主にsocket送受信に用いられる.さらにプラットフォームごとに、その最下位の実装は異なります.何種類かあります.
1 .I/O Completion ports ( Windows NT/2000/XP)
2/dev/epoll (linux)
3 select()
次に、pj_について例のプログラムで説明します.ioqueueの作成に使用します.この例のプログラムには2つの部分がある.サーバ側とクライアント側.クライアント側はランダム文字列を送信し、server側は印刷します.
server.c
#include "pjlib.h"
typedef struct test_op_key{
pj_ioqueue_op_key_t op_key ;
char data[200] ;
}test_op_key ;
static void on_ioqueue_read( pj_ioqueue_key_t *key , pj_ioqueue_op_key_t * op_key ,
pj_ssize_t bytes_read)
{
PJ_LOG(3 , ("read " ,"read %d" , bytes_read )) ;
test_op_key *tt_key = (test_op_key*)op_key ;
PJ_LOG(3 , ("read " ,"read %s" , tt_key->data )) ;
}
static void on_ioqueue_write( pj_ioqueue_key_t *key , pj_ioqueue_op_key_t *op_key ,
pj_ssize_t bytes_written)
{
PJ_LOG(3 , ("wirte " , "%d" , bytes_written)) ;
}
static void on_ioqueue_accept( pj_ioqueue_key_t *key , pj_ioqueue_op_key_t *op_key ,
pj_sock_t sock , int status )
{
}
static void on_ioqueue_connect (pj_ioqueue_key_t *key , int status )
{
}
static pj_ioqueue_callback test_cb = {
&on_ioqueue_read ,
&on_ioqueue_write ,
&on_ioqueue_accept ,
&on_ioqueue_connect ,
};
//
int main()
{
pj_status_t status ;
pj_caching_pool cp ;
pj_pool_t *pool ;
pj_sock_t ssock = -1 ,csock = -1 ;
pj_sockaddr_in addr ;
pj_ioqueue_t *ioque = NULL ;
pj_ioqueue_key_t *skey ;
test_op_key read_op ;
pj_ssize_t bytes ;
status = pj_init();
if(status != PJ_SUCCESS )
{
printf("error init ");
return -1 ;
}
pj_caching_pool_init(&cp , NULL , 0 ) ;
pool = pj_pool_create( &cp.factory , NULL , 1024 , 1024 , NULL ) ;
pj_sock_socket( pj_AF_INET() , pj_SOCK_DGRAM() , 0 , &ssock ) ;
pj_sock_socket( pj_AF_INET() , pj_SOCK_DGRAM() , 0 , &csock ) ;
pj_bzero(&addr , sizeof(addr) );
pj_str_t s ;
addr.sin_family = pj_AF_INET();
addr.sin_port = pj_htons(34780) ;
addr.sin_addr = pj_inet_addr(pj_cstr(&s , "127.0.0.1" )) ;
pj_sock_bind( ssock , &addr , sizeof(addr));
pj_ioqueue_create(pool , PJ_IOQUEUE_MAX_HANDLES , &ioque ) ; // queue
pj_ioqueue_register_sock(pool , ioque , ssock , NULL , &test_cb , &skey ) ; //r socket queue
pj_bzero(&addr , sizeof(addr)) ;
int addrlen = sizeof(addr ) ;
bytes = 200 ;
do{
pj_time_val timeout = { 1 , 0 } ;
status = pj_ioqueue_poll ( ioque , &timeout ) ; //poll queue
status =pj_ioqueue_recvfrom(skey , &(read_op.op_key) , read_op.data , &bytes , 0 ,&addr ,&addrlen ) ;
// recv 。 test_op_key , , 。
}while(1) ;
pj_caching_pool_destroy(&cp) ;
pj_shutdown();
return 0 ;
}
client.c
#include "pjlib.h"
static void on_ioqueue_read( pj_ioqueue_key_t *key , pj_ioqueue_op_key_t * op_key ,
pj_ssize_t bytes_read)
{
PJ_LOG(3 , ("read " ,"read %d" , bytes_read )) ;
}
static void on_ioqueue_write( pj_ioqueue_key_t *key , pj_ioqueue_op_key_t *op_key ,
pj_ssize_t bytes_written)
{
PJ_LOG(3 , ("wirte " , "%d" , bytes_written)) ;
}
static void on_ioqueue_accept( pj_ioqueue_key_t *key , pj_ioqueue_op_key_t *op_key ,
pj_sock_t sock , int status )
{
}
static void on_ioqueue_connect (pj_ioqueue_key_t *key , int status )
{
}
static pj_ioqueue_callback test_cb = {
&on_ioqueue_read ,
&on_ioqueue_write ,
&on_ioqueue_accept ,
&on_ioqueue_connect ,
};
int main()
{
pj_status_t status ;
pj_caching_pool cp ;
pj_pool_t *pool ;
pj_sock_t ssock = -1 ,csock = -1 ;
pj_sockaddr_in addr ,dst_addr ;
char * send_buf ;
pj_ioqueue_t *ioque = NULL ;
pj_ioqueue_key_t *ckey ;
pj_ioqueue_op_key_t write_op ;
pj_ssize_t bytes ;
status = pj_init();
if(status != PJ_SUCCESS )
{
printf("error init ");
return -1 ;
}
pj_caching_pool_init(&cp , NULL , 0 ) ;
pool = pj_pool_create( &cp.factory , NULL , 1024 , 1024 , NULL ) ;
send_buf = (char*)pj_pool_alloc(pool , 200 ) ;
pj_create_random_string(send_buf , 200 ) ;
pj_sock_socket( pj_AF_INET() , pj_SOCK_DGRAM() , 0 , &ssock ) ;
pj_sock_socket( pj_AF_INET() , pj_SOCK_DGRAM() , 0 , &csock ) ;
pj_bzero(&addr , sizeof(addr) );
pj_str_t s ;
addr.sin_family = pj_AF_INET();
addr.sin_port = pj_htons(34790) ;
addr.sin_addr = pj_inet_addr(pj_cstr(&s , "127.0.0.1" )) ;
pj_sock_bind( ssock , &addr , sizeof(addr));
pj_ioqueue_create(pool , PJ_IOQUEUE_MAX_HANDLES , &ioque ) ;
pj_ioqueue_register_sock(pool , ioque , ssock , NULL , &test_cb , &ckey ) ;
pj_str_t tmp ;
tmp = pj_str("127.0.0.1") ;
status = pj_sockaddr_in_init(&dst_addr , &tmp , 34780 ) ;
bytes = 200 ;
do{
pj_time_val timeout = { 1 , 0 } ;
status = pj_ioqueue_poll ( ioque , &timeout ) ;
PJ_LOG(3 , ("this " ,"poll rc = %d" ,status )) ;
pj_create_random_string(send_buf , 200 ) ;
status = pj_ioqueue_sendto(ckey , &write_op , send_buf , &bytes , 0 ,&dst_addr ,
sizeof(dst_addr)) ;
}while(1) ;
pj_caching_pool_destroy(&cp) ;
pj_shutdown();
return 0 ;
}