本文整理汇总了C++中pj_ioqueue_register_sock函数的典型用法代码示例。如果您正苦于以下问题:C++ pj_ioqueue_register_sock函数的具体用法?C++ pj_ioqueue_register_sock怎么用?C++ pj_ioqueue_register_sock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pj_ioqueue_register_sock函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: PJ_DEF
PJ_DEF(pj_status_t) pj_activesock_create( pj_pool_t *pool,
pj_sock_t sock,
int sock_type,
const pj_activesock_cfg *opt,
pj_ioqueue_t *ioqueue,
const pj_activesock_cb *cb,
void *user_data,
pj_activesock_t **p_asock)
{
pj_activesock_t *asock;
pj_ioqueue_callback ioq_cb;
pj_status_t status;
PJ_ASSERT_RETURN(pool && ioqueue && cb && p_asock, PJ_EINVAL);
PJ_ASSERT_RETURN(sock!=0 && sock!=PJ_INVALID_SOCKET, PJ_EINVAL);
PJ_ASSERT_RETURN(sock_type==pj_SOCK_STREAM() ||
sock_type==pj_SOCK_DGRAM(), PJ_EINVAL);
PJ_ASSERT_RETURN(!opt || opt->async_cnt >= 1, PJ_EINVAL);
asock = PJ_POOL_ZALLOC_T(pool, pj_activesock_t);
asock->ioqueue = ioqueue;
asock->stream_oriented = (sock_type == pj_SOCK_STREAM());
asock->async_count = (opt? opt->async_cnt : 1);
asock->whole_data = (opt? opt->whole_data : 1);
asock->max_loop = PJ_ACTIVESOCK_MAX_LOOP;
asock->user_data = user_data;
pj_memcpy(&asock->cb, cb, sizeof(*cb));
pj_bzero(&ioq_cb, sizeof(ioq_cb));
ioq_cb.on_read_complete = &ioqueue_on_read_complete;
ioq_cb.on_write_complete = &ioqueue_on_write_complete;
#if PJ_HAS_TCP
ioq_cb.on_connect_complete = &ioqueue_on_connect_complete;
ioq_cb.on_accept_complete = &ioqueue_on_accept_complete;
#endif
status = pj_ioqueue_register_sock(pool, ioqueue, sock, asock,
&ioq_cb, &asock->key);
if (status != PJ_SUCCESS) {
pj_activesock_close(asock);
return status;
}
if (asock->whole_data) {
/* Must disable concurrency otherwise there is a race condition */
// DEAN force to enable concurrency to solve dead-lock
pj_ioqueue_set_concurrency(asock->key, 1);
} else if (opt && opt->concurrency >= 0) {
pj_ioqueue_set_concurrency(asock->key, opt->concurrency);
}
#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \
PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0
asock->sock = sock;
asock->bg_setting = PJ_ACTIVESOCK_TCP_IPHONE_OS_BG;
#endif
*p_asock = asock;
return PJ_SUCCESS;
}
示例2: register_socket_handler
//
// Register handler.
// This will call handler->get_socket_handle()
//
pj_status_t register_socket_handler(Pj_Pool *pool,
Pj_Event_Handler *handler)
{
return pj_ioqueue_register_sock( pool->pool_(), ioq_,
handler->get_socket_handle(),
handler, &cb_, &handler->key_ );
}
示例3: udp_echo_srv_ioqueue
int udp_echo_srv_ioqueue(void)
{
pj_pool_t *pool;
pj_sock_t sock;
pj_ioqueue_t *ioqueue;
pj_ioqueue_callback callback;
int i;
pj_thread_t *thread[ECHO_SERVER_MAX_THREADS];
pj_status_t rc;
pj_bzero(&callback, sizeof(callback));
callback.on_read_complete = &on_read_complete;
callback.on_write_complete = &on_write_complete;
pool = pj_pool_create(mem, NULL, 4000, 4000, NULL);
if (!pool)
return -10;
rc = pj_ioqueue_create(pool, 2, &ioqueue);
if (rc != PJ_SUCCESS) {
app_perror("...pj_ioqueue_create error", rc);
return -20;
}
rc = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0,
ECHO_SERVER_START_PORT, &sock);
if (rc != PJ_SUCCESS) {
app_perror("...app_socket error", rc);
return -30;
}
rc = pj_ioqueue_register_sock(pool, ioqueue, sock, NULL,
&callback, &key);
if (rc != PJ_SUCCESS) {
app_perror("...error registering socket", rc);
return -40;
}
rc = pj_atomic_create(pool, 0, &total_bytes);
if (rc != PJ_SUCCESS) {
app_perror("...error creating atomic variable", rc);
return -45;
}
for (i=0; i<ECHO_SERVER_MAX_THREADS; ++i) {
rc = pj_thread_create(pool, NULL, &worker_thread, ioqueue,
PJ_THREAD_DEFAULT_STACK_SIZE, 0,
&thread[i]);
if (rc != PJ_SUCCESS) {
app_perror("...create thread error", rc);
return -50;
}
}
echo_srv_common_loop(total_bytes);
return 0;
}
示例4: register_to_ioqueue
/* Register socket to ioqueue */
static pj_status_t register_to_ioqueue(struct udp_transport *tp)
{
pj_ioqueue_t *ioqueue;
pj_ioqueue_callback ioqueue_cb;
/* Ignore if already registered */
if (tp->key != NULL)
return PJ_SUCCESS;
/* Register to ioqueue. */
ioqueue = pjsip_endpt_get_ioqueue(tp->base.endpt);
pj_memset(&ioqueue_cb, 0, sizeof(ioqueue_cb));
ioqueue_cb.on_read_complete = &udp_on_read_complete;
ioqueue_cb.on_write_complete = &udp_on_write_complete;
return pj_ioqueue_register_sock(tp->base.pool, ioqueue, tp->sock, tp,
&ioqueue_cb, &tp->key);
}
示例5: init_sock
/* Initialize UDP socket */
static pj_status_t init_sock(pj_dns_resolver *resv)
{
pj_ioqueue_callback socket_cb;
pj_status_t status;
/* Create the UDP socket */
status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &resv->udp_sock);
if (status != PJ_SUCCESS)
return status;
/* Bind to any address/port */
status = pj_sock_bind_in(resv->udp_sock, 0, 0);
if (status != PJ_SUCCESS)
return status;
/* Register to ioqueue */
pj_bzero(&socket_cb, sizeof(socket_cb));
socket_cb.on_read_complete = &on_read_complete;
status = pj_ioqueue_register_sock(resv->pool, resv->ioqueue,
resv->udp_sock, resv, &socket_cb,
&resv->udp_key);
if (status != PJ_SUCCESS)
return status;
pj_ioqueue_op_key_init(&resv->udp_op_rx_key, sizeof(resv->udp_op_rx_key));
pj_ioqueue_op_key_init(&resv->udp_op_tx_key, sizeof(resv->udp_op_tx_key));
/* Start asynchronous read to the UDP socket */
resv->udp_len = sizeof(resv->udp_rx_pkt);
resv->udp_addr_len = sizeof(resv->udp_src_addr);
status = pj_ioqueue_recvfrom(resv->udp_key, &resv->udp_op_rx_key,
resv->udp_rx_pkt, &resv->udp_len,
PJ_IOQUEUE_ALWAYS_ASYNC,
&resv->udp_src_addr, &resv->udp_addr_len);
if (status != PJ_EPENDING)
return status;
return PJ_SUCCESS;
}
示例6: bench_test
/*
* Benchmarking IOQueue
*/
static int bench_test(pj_bool_t allow_concur, int bufsize,
int inactive_sock_count)
{
pj_sock_t ssock=-1, csock=-1;
pj_sockaddr_in addr;
pj_pool_t *pool = NULL;
pj_sock_t *inactive_sock=NULL;
pj_ioqueue_op_key_t *inactive_read_op;
char *send_buf, *recv_buf;
pj_ioqueue_t *ioque = NULL;
pj_ioqueue_key_t *skey, *ckey, *keys[SOCK_INACTIVE_MAX+2];
pj_timestamp t1, t2, t_elapsed;
int rc=0, i; /* i must be signed */
pj_str_t temp;
char errbuf[PJ_ERR_MSG_SIZE];
TRACE__((THIS_FILE, " bench test %d", inactive_sock_count));
// Create pool.
pool = pj_pool_create(mem, NULL, POOL_SIZE, 4000, NULL);
// Allocate buffers for send and receive.
send_buf = (char*)pj_pool_alloc(pool, bufsize);
recv_buf = (char*)pj_pool_alloc(pool, bufsize);
// Allocate sockets for sending and receiving.
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &ssock);
if (rc == PJ_SUCCESS) {
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &csock);
} else
csock = PJ_INVALID_SOCKET;
if (rc != PJ_SUCCESS) {
app_perror("...error: pj_sock_socket()", rc);
goto on_error;
}
// Bind server socket.
pj_bzero(&addr, sizeof(addr));
addr.sin_family = pj_AF_INET();
addr.sin_port = pj_htons(PORT);
if (pj_sock_bind(ssock, &addr, sizeof(addr)))
goto on_error;
pj_assert(inactive_sock_count+2 <= PJ_IOQUEUE_MAX_HANDLES);
// Create I/O Queue.
rc = pj_ioqueue_create(pool, PJ_IOQUEUE_MAX_HANDLES, &ioque);
if (rc != PJ_SUCCESS) {
app_perror("...error: pj_ioqueue_create()", rc);
goto on_error;
}
// Set concurrency
rc = pj_ioqueue_set_default_concurrency(ioque, allow_concur);
if (rc != PJ_SUCCESS) {
app_perror("...error: pj_ioqueue_set_default_concurrency()", rc);
goto on_error;
}
// Allocate inactive sockets, and bind them to some arbitrary address.
// Then register them to the I/O queue, and start a read operation.
inactive_sock = (pj_sock_t*)pj_pool_alloc(pool,
inactive_sock_count*sizeof(pj_sock_t));
inactive_read_op = (pj_ioqueue_op_key_t*)pj_pool_alloc(pool,
inactive_sock_count*sizeof(pj_ioqueue_op_key_t));
pj_bzero(&addr, sizeof(addr));
addr.sin_family = pj_AF_INET();
for (i=0; i<inactive_sock_count; ++i) {
pj_ssize_t bytes;
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &inactive_sock[i]);
if (rc != PJ_SUCCESS || inactive_sock[i] < 0) {
app_perror("...error: pj_sock_socket()", rc);
goto on_error;
}
if ((rc=pj_sock_bind(inactive_sock[i], &addr, sizeof(addr))) != 0) {
pj_sock_close(inactive_sock[i]);
inactive_sock[i] = PJ_INVALID_SOCKET;
app_perror("...error: pj_sock_bind()", rc);
goto on_error;
}
rc = pj_ioqueue_register_sock(pool, ioque, inactive_sock[i],
NULL, &test_cb, &keys[i]);
if (rc != PJ_SUCCESS) {
pj_sock_close(inactive_sock[i]);
inactive_sock[i] = PJ_INVALID_SOCKET;
app_perror("...error(1): pj_ioqueue_register_sock()", rc);
PJ_LOG(3,(THIS_FILE, "....i=%d", i));
goto on_error;
}
bytes = bufsize;
rc = pj_ioqueue_recv(keys[i], &inactive_read_op[i], recv_buf, &bytes, 0);
if (rc != PJ_EPENDING) {
pj_sock_close(inactive_sock[i]);
inactive_sock[i] = PJ_INVALID_SOCKET;
app_perror("...error: pj_ioqueue_read()", rc);
goto on_error;
//.........这里部分代码省略.........
示例7: perform_test
/* Calculate the bandwidth for the specific test configuration.
* The test is simple:
* - create sockpair_cnt number of producer-consumer socket pair.
* - create thread_cnt number of worker threads.
* - each producer will send buffer_size bytes data as fast and
* as soon as it can.
* - each consumer will read buffer_size bytes of data as fast
* as it could.
* - measure the total bytes received by all consumers during a
* period of time.
*/
static int perform_test(pj_bool_t allow_concur,
int sock_type, const char *type_name,
unsigned thread_cnt, unsigned sockpair_cnt,
pj_size_t buffer_size,
pj_size_t *p_bandwidth)
{
enum { MSEC_DURATION = 5000 };
pj_pool_t *pool;
test_item *items;
pj_thread_t **thread;
pj_ioqueue_t *ioqueue;
pj_status_t rc;
pj_ioqueue_callback ioqueue_callback;
pj_uint32_t total_elapsed_usec, total_received;
pj_highprec_t bandwidth;
pj_timestamp start, stop;
unsigned i;
TRACE_((THIS_FILE, " starting test.."));
ioqueue_callback.on_read_complete = &on_read_complete;
ioqueue_callback.on_write_complete = &on_write_complete;
thread_quit_flag = 0;
pool = pj_pool_create(mem, NULL, 4096, 4096, NULL);
if (!pool)
return -10;
items = (test_item*) pj_pool_alloc(pool, sockpair_cnt*sizeof(test_item));
thread = (pj_thread_t**)
pj_pool_alloc(pool, thread_cnt*sizeof(pj_thread_t*));
TRACE_((THIS_FILE, " creating ioqueue.."));
rc = pj_ioqueue_create(pool, sockpair_cnt*2, &ioqueue);
if (rc != PJ_SUCCESS) {
app_perror("...error: unable to create ioqueue", rc);
return -15;
}
rc = pj_ioqueue_set_default_concurrency(ioqueue, allow_concur);
if (rc != PJ_SUCCESS) {
app_perror("...error: pj_ioqueue_set_default_concurrency()", rc);
return -16;
}
/* Initialize each producer-consumer pair. */
for (i=0; i<sockpair_cnt; ++i) {
pj_ssize_t bytes;
items[i].ioqueue = ioqueue;
items[i].buffer_size = buffer_size;
items[i].outgoing_buffer = (char*) pj_pool_alloc(pool, buffer_size);
items[i].incoming_buffer = (char*) pj_pool_alloc(pool, buffer_size);
items[i].bytes_recv = items[i].bytes_sent = 0;
/* randomize outgoing buffer. */
pj_create_random_string(items[i].outgoing_buffer, buffer_size);
/* Create socket pair. */
TRACE_((THIS_FILE, " calling socketpair.."));
rc = app_socketpair(pj_AF_INET(), sock_type, 0,
&items[i].server_fd, &items[i].client_fd);
if (rc != PJ_SUCCESS) {
app_perror("...error: unable to create socket pair", rc);
return -20;
}
/* Register server socket to ioqueue. */
TRACE_((THIS_FILE, " register(1).."));
rc = pj_ioqueue_register_sock(pool, ioqueue,
items[i].server_fd,
&items[i], &ioqueue_callback,
&items[i].server_key);
if (rc != PJ_SUCCESS) {
app_perror("...error: registering server socket to ioqueue", rc);
return -60;
}
/* Register client socket to ioqueue. */
TRACE_((THIS_FILE, " register(2).."));
rc = pj_ioqueue_register_sock(pool, ioqueue,
items[i].client_fd,
&items[i], &ioqueue_callback,
&items[i].client_key);
if (rc != PJ_SUCCESS) {
app_perror("...error: registering server socket to ioqueue", rc);
return -70;
}
//.........这里部分代码省略.........
示例8: unregister_test
/*
* unregister_test()
* Check if callback is still called after socket has been unregistered or
* closed.
*/
static int unregister_test(pj_bool_t allow_concur)
{
enum { RPORT = 50000, SPORT = 50001 };
pj_pool_t *pool;
pj_ioqueue_t *ioqueue;
pj_sock_t ssock;
pj_sock_t rsock;
int addrlen;
pj_sockaddr_in addr;
pj_ioqueue_key_t *key;
pj_ioqueue_op_key_t opkey;
pj_ioqueue_callback cb;
unsigned packet_cnt;
char sendbuf[10], recvbuf[10];
pj_ssize_t bytes;
pj_time_val timeout;
pj_status_t status;
pool = pj_pool_create(mem, "test", 4000, 4000, NULL);
if (!pool) {
app_perror("Unable to create pool", PJ_ENOMEM);
return -100;
}
status = pj_ioqueue_create(pool, 16, &ioqueue);
if (status != PJ_SUCCESS) {
app_perror("Error creating ioqueue", status);
return -110;
}
// Set concurrency
TRACE_("set concurrency...");
status = pj_ioqueue_set_default_concurrency(ioqueue, allow_concur);
if (status != PJ_SUCCESS) {
return -112;
}
/* Create sender socket */
status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, SPORT, &ssock);
if (status != PJ_SUCCESS) {
app_perror("Error initializing socket", status);
return -120;
}
/* Create receiver socket. */
status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, RPORT, &rsock);
if (status != PJ_SUCCESS) {
app_perror("Error initializing socket", status);
return -130;
}
/* Register rsock to ioqueue. */
pj_bzero(&cb, sizeof(cb));
cb.on_read_complete = &on_read_complete;
packet_cnt = 0;
status = pj_ioqueue_register_sock(pool, ioqueue, rsock, &packet_cnt,
&cb, &key);
if (status != PJ_SUCCESS) {
app_perror("Error registering to ioqueue", status);
return -140;
}
/* Init operation key. */
pj_ioqueue_op_key_init(&opkey, sizeof(opkey));
/* Start reading. */
bytes = sizeof(recvbuf);
status = pj_ioqueue_recv( key, &opkey, recvbuf, &bytes, 0);
if (status != PJ_EPENDING) {
app_perror("Expecting PJ_EPENDING, but got this", status);
return -150;
}
/* Init destination address. */
addrlen = sizeof(addr);
status = pj_sock_getsockname(rsock, &addr, &addrlen);
if (status != PJ_SUCCESS) {
app_perror("getsockname error", status);
return -160;
}
/* Override address with 127.0.0.1, since getsockname will return
* zero in the address field.
*/
addr.sin_addr = pj_inet_addr2("127.0.0.1");
/* Init buffer to send */
pj_ansi_strcpy(sendbuf, "Hello0123");
/* Send one packet. */
bytes = sizeof(sendbuf);
status = pj_sock_sendto(ssock, sendbuf, &bytes, 0,
&addr, sizeof(addr));
if (status != PJ_SUCCESS) {
//.........这里部分代码省略.........
示例9: many_handles_test
/*
* Testing with many handles.
* This will just test registering PJ_IOQUEUE_MAX_HANDLES count
* of sockets to the ioqueue.
*/
static int many_handles_test(pj_bool_t allow_concur)
{
enum { MAX = PJ_IOQUEUE_MAX_HANDLES };
pj_pool_t *pool;
pj_ioqueue_t *ioqueue;
pj_sock_t *sock;
pj_ioqueue_key_t **key;
pj_status_t rc;
int count, i; /* must be signed */
PJ_LOG(3,(THIS_FILE,"...testing with so many handles"));
pool = pj_pool_create(mem, NULL, 4000, 4000, NULL);
if (!pool)
return PJ_ENOMEM;
key = (pj_ioqueue_key_t**)
pj_pool_alloc(pool, MAX*sizeof(pj_ioqueue_key_t*));
sock = (pj_sock_t*) pj_pool_alloc(pool, MAX*sizeof(pj_sock_t));
/* Create IOQueue */
rc = pj_ioqueue_create(pool, MAX, &ioqueue);
if (rc != PJ_SUCCESS || ioqueue == NULL) {
app_perror("...error in pj_ioqueue_create", rc);
return -10;
}
// Set concurrency
rc = pj_ioqueue_set_default_concurrency(ioqueue, allow_concur);
if (rc != PJ_SUCCESS) {
return -11;
}
/* Register as many sockets. */
for (count=0; count<MAX; ++count) {
sock[count] = PJ_INVALID_SOCKET;
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[count]);
if (rc != PJ_SUCCESS || sock[count] == PJ_INVALID_SOCKET) {
PJ_LOG(3,(THIS_FILE, "....unable to create %d-th socket, rc=%d",
count, rc));
break;
}
key[count] = NULL;
rc = pj_ioqueue_register_sock(pool, ioqueue, sock[count],
NULL, &test_cb, &key[count]);
if (rc != PJ_SUCCESS || key[count] == NULL) {
PJ_LOG(3,(THIS_FILE, "....unable to register %d-th socket, rc=%d",
count, rc));
return -30;
}
}
/* Test complete. */
/* Now deregister and close all handles. */
/* NOTE for RTEMS:
* It seems that the order of close(sock) is pretty important here.
* If we close the sockets with the same order as when they were created,
* RTEMS doesn't seem to reuse the sockets, thus next socket created
* will have descriptor higher than the last socket created.
* If we close the sockets in the reverse order, then the descriptor will
* get reused.
* This used to cause problem with select ioqueue, since the ioqueue
* always gives FD_SETSIZE for the first select() argument. This ioqueue
* behavior can be changed with setting PJ_SELECT_NEEDS_NFDS macro.
*/
for (i=count-1; i>=0; --i) {
///for (i=0; i<count; ++i) {
rc = pj_ioqueue_unregister(key[i]);
if (rc != PJ_SUCCESS) {
app_perror("...error in pj_ioqueue_unregister", rc);
}
}
rc = pj_ioqueue_destroy(ioqueue);
if (rc != PJ_SUCCESS) {
app_perror("...error in pj_ioqueue_destroy", rc);
}
pj_pool_release(pool);
PJ_LOG(3,(THIS_FILE,"....many_handles_test() ok"));
return 0;
}
示例10: compliance_test_2
//.........这里部分代码省略.........
app_perror("...ERROR in pj_ioqueue_set_default_concurrency()", rc);
return -11;
}
// Allocate buffers for send and receive.
send_buf = (char*)pj_pool_alloc(pool, bufsize);
recv_buf = (char*)pj_pool_alloc(pool, bufsize);
// Create listener socket
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, &listener.sock);
if (rc != PJ_SUCCESS) {
app_perror("...error creating socket", rc);
status=-20; goto on_error;
}
// Bind listener socket.
pj_sockaddr_in_init(&listener.addr, 0, 0);
if ((rc=pj_sock_bind(listener.sock, &listener.addr, sizeof(listener.addr))) != 0 ) {
app_perror("...bind error", rc);
status=-30; goto on_error;
}
// Get listener address.
listener.addr_len = sizeof(listener.addr);
rc = pj_sock_getsockname(listener.sock, &listener.addr, &listener.addr_len);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR in pj_sock_getsockname()", rc);
status=-40; goto on_error;
}
listener.addr.sin_addr = pj_inet_addr(pj_cstr(&s, "127.0.0.1"));
// Register listener socket.
rc = pj_ioqueue_register_sock(pool, ioque, listener.sock, NULL, &test_cb,
&listener.key);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR", rc);
status=-50; goto on_error;
}
// Listener socket listen().
if (pj_sock_listen(listener.sock, 5)) {
app_perror("...ERROR in pj_sock_listen()", rc);
status=-60; goto on_error;
}
for (test_loop=0; test_loop < TEST_LOOP; ++test_loop) {
// Client connect and server accept.
for (i=0; i<MAX_PAIR; ++i) {
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, &client[i].sock);
if (rc != PJ_SUCCESS) {
app_perror("...error creating socket", rc);
status=-70; goto on_error;
}
rc = pj_ioqueue_register_sock(pool, ioque, client[i].sock, NULL,
&test_cb, &client[i].key);
if (rc != PJ_SUCCESS) {
app_perror("...error ", rc);
status=-80; goto on_error;
}
// Server socket accept()
pj_ioqueue_op_key_init(&server[i].accept_op,
示例11: compliance_test
/*
* compliance_test()
* To test that the basic IOQueue functionality works. It will just exchange
* data between two sockets.
*/
static int compliance_test(pj_bool_t allow_concur)
{
pj_sock_t ssock=-1, csock=-1;
pj_sockaddr_in addr, dst_addr;
int addrlen;
pj_pool_t *pool = NULL;
char *send_buf, *recv_buf;
pj_ioqueue_t *ioque = NULL;
pj_ioqueue_key_t *skey = NULL, *ckey = NULL;
pj_ioqueue_op_key_t read_op, write_op;
int bufsize = BUF_MIN_SIZE;
pj_ssize_t bytes;
int status = -1;
pj_str_t temp;
pj_bool_t send_pending, recv_pending;
pj_status_t rc;
pj_set_os_error(PJ_SUCCESS);
// Create pool.
pool = pj_pool_create(mem, NULL, POOL_SIZE, 4000, NULL);
// Allocate buffers for send and receive.
send_buf = (char*)pj_pool_alloc(pool, bufsize);
recv_buf = (char*)pj_pool_alloc(pool, bufsize);
// Allocate sockets for sending and receiving.
TRACE_("creating sockets...");
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &ssock);
if (rc==PJ_SUCCESS)
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &csock);
else
csock = PJ_INVALID_SOCKET;
if (rc != PJ_SUCCESS) {
app_perror("...ERROR in pj_sock_socket()", rc);
status=-1; goto on_error;
}
// Bind server socket.
TRACE_("bind socket...");
pj_bzero(&addr, sizeof(addr));
addr.sin_family = pj_AF_INET();
addr.sin_port = pj_htons(PORT);
if (pj_sock_bind(ssock, &addr, sizeof(addr))) {
status=-10; goto on_error;
}
// Create I/O Queue.
TRACE_("create ioqueue...");
rc = pj_ioqueue_create(pool, PJ_IOQUEUE_MAX_HANDLES, &ioque);
if (rc != PJ_SUCCESS) {
status=-20; goto on_error;
}
// Set concurrency
TRACE_("set concurrency...");
rc = pj_ioqueue_set_default_concurrency(ioque, allow_concur);
if (rc != PJ_SUCCESS) {
status=-21; goto on_error;
}
// Register server and client socket.
// We put this after inactivity socket, hopefully this can represent the
// worst waiting time.
TRACE_("registering first sockets...");
rc = pj_ioqueue_register_sock(pool, ioque, ssock, NULL,
&test_cb, &skey);
if (rc != PJ_SUCCESS) {
app_perror("...error(10): ioqueue_register error", rc);
status=-25; goto on_error;
}
TRACE_("registering second sockets...");
rc = pj_ioqueue_register_sock( pool, ioque, csock, NULL,
&test_cb, &ckey);
if (rc != PJ_SUCCESS) {
app_perror("...error(11): ioqueue_register error", rc);
status=-26; goto on_error;
}
// Randomize send_buf.
pj_create_random_string(send_buf, bufsize);
// Register reading from ioqueue.
TRACE_("start recvfrom...");
pj_bzero(&addr, sizeof(addr));
addrlen = sizeof(addr);
bytes = bufsize;
rc = pj_ioqueue_recvfrom(skey, &read_op, recv_buf, &bytes, 0,
&addr, &addrlen);
if (rc != PJ_SUCCESS && rc != PJ_EPENDING) {
app_perror("...error: pj_ioqueue_recvfrom", rc);
status=-28; goto on_error;
} else if (rc == PJ_EPENDING) {
recv_pending = 1;
PJ_LOG(3, (THIS_FILE,
//.........这里部分代码省略.........
示例12: compliance_test_0
/*
* Compliance test for success scenario.
*/
static int compliance_test_0(pj_bool_t allow_concur)
{
pj_sock_t ssock=-1, csock0=-1, csock1=-1;
pj_sockaddr_in addr, client_addr, rmt_addr;
int client_addr_len;
pj_pool_t *pool = NULL;
char *send_buf, *recv_buf;
pj_ioqueue_t *ioque = NULL;
pj_ioqueue_key_t *skey=NULL, *ckey0=NULL, *ckey1=NULL;
pj_ioqueue_op_key_t accept_op;
int bufsize = BUF_MIN_SIZE;
pj_ssize_t status = -1;
int pending_op = 0;
pj_timestamp t_elapsed;
pj_str_t s;
pj_status_t rc;
// Create pool.
pool = pj_pool_create(mem, NULL, POOL_SIZE, 4000, NULL);
// Allocate buffers for send and receive.
send_buf = (char*)pj_pool_alloc(pool, bufsize);
recv_buf = (char*)pj_pool_alloc(pool, bufsize);
// Create server socket and client socket for connecting
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, &ssock);
if (rc != PJ_SUCCESS) {
app_perror("...error creating socket", rc);
status=-1; goto on_error;
}
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, &csock1);
if (rc != PJ_SUCCESS) {
app_perror("...error creating socket", rc);
status=-1; goto on_error;
}
// Bind server socket.
pj_sockaddr_in_init(&addr, 0, 0);
if ((rc=pj_sock_bind(ssock, &addr, sizeof(addr))) != 0 ) {
app_perror("...bind error", rc);
status=-10; goto on_error;
}
// Get server address.
client_addr_len = sizeof(addr);
rc = pj_sock_getsockname(ssock, &addr, &client_addr_len);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR in pj_sock_getsockname()", rc);
status=-15; goto on_error;
}
addr.sin_addr = pj_inet_addr(pj_cstr(&s, "127.0.0.1"));
// Create I/O Queue.
rc = pj_ioqueue_create(pool, PJ_IOQUEUE_MAX_HANDLES, &ioque);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR in pj_ioqueue_create()", rc);
status=-20; goto on_error;
}
// Concurrency
rc = pj_ioqueue_set_default_concurrency(ioque, allow_concur);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR in pj_ioqueue_set_default_concurrency()", rc);
status=-21; goto on_error;
}
// Register server socket and client socket.
rc = pj_ioqueue_register_sock(pool, ioque, ssock, NULL, &test_cb, &skey);
if (rc == PJ_SUCCESS)
rc = pj_ioqueue_register_sock(pool, ioque, csock1, NULL, &test_cb,
&ckey1);
else
ckey1 = NULL;
if (rc != PJ_SUCCESS) {
app_perror("...ERROR in pj_ioqueue_register_sock()", rc);
status=-23; goto on_error;
}
// Server socket listen().
if (pj_sock_listen(ssock, 5)) {
app_perror("...ERROR in pj_sock_listen()", rc);
status=-25; goto on_error;
}
// Server socket accept()
client_addr_len = sizeof(pj_sockaddr_in);
status = pj_ioqueue_accept(skey, &accept_op, &csock0,
&client_addr, &rmt_addr, &client_addr_len);
if (status != PJ_EPENDING) {
app_perror("...ERROR in pj_ioqueue_accept()", rc);
status=-30; goto on_error;
}
if (status==PJ_EPENDING) {
++pending_op;
}
//.........这里部分代码省略.........
示例13: compliance_test_1
/*
* Compliance test for failed scenario.
* In this case, the client connects to a non-existant service.
*/
static int compliance_test_1(pj_bool_t allow_concur)
{
pj_sock_t csock1=PJ_INVALID_SOCKET;
pj_sockaddr_in addr;
pj_pool_t *pool = NULL;
pj_ioqueue_t *ioque = NULL;
pj_ioqueue_key_t *ckey1 = NULL;
pj_ssize_t status = -1;
int pending_op = 0;
pj_str_t s;
pj_status_t rc;
// Create pool.
pool = pj_pool_create(mem, NULL, POOL_SIZE, 4000, NULL);
// Create I/O Queue.
rc = pj_ioqueue_create(pool, PJ_IOQUEUE_MAX_HANDLES, &ioque);
if (!ioque) {
status=-20; goto on_error;
}
// Concurrency
rc = pj_ioqueue_set_default_concurrency(ioque, allow_concur);
if (rc != PJ_SUCCESS) {
status=-21; goto on_error;
}
// Create client socket
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, &csock1);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR in pj_sock_socket()", rc);
status=-1; goto on_error;
}
// Register client socket.
rc = pj_ioqueue_register_sock(pool, ioque, csock1, NULL,
&test_cb, &ckey1);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR in pj_ioqueue_register_sock()", rc);
status=-23; goto on_error;
}
// Initialize remote address.
pj_sockaddr_in_init(&addr, pj_cstr(&s, "127.0.0.1"), NON_EXISTANT_PORT);
// Client socket connect()
status = pj_ioqueue_connect(ckey1, &addr, sizeof(addr));
if (status==PJ_SUCCESS) {
// unexpectedly success!
status = -30;
goto on_error;
}
if (status != PJ_EPENDING) {
// success
} else {
++pending_op;
}
callback_connect_status = -2;
callback_connect_key = NULL;
// Poll until we've got result
while (pending_op) {
pj_time_val timeout = {1, 0};
#ifdef PJ_SYMBIAN
callback_call_count = 0;
pj_symbianos_poll(-1, 1000);
status = callback_call_count;
#else
status = pj_ioqueue_poll(ioque, &timeout);
#endif
if (status > 0) {
if (callback_connect_key==ckey1) {
if (callback_connect_status == 0) {
// unexpectedly connected!
status = -50;
goto on_error;
}
}
if (status > pending_op) {
PJ_LOG(3,(THIS_FILE,
"...error: pj_ioqueue_poll() returned %d "
"(only expecting %d)",
status, pending_op));
return -552;
}
pending_op -= status;
if (pending_op == 0) {
status = 0;
}
}
}
//.........这里部分代码省略.........
示例14: PJ_DEF
/**
* Create UDP stream transport from existing socket info.
*/
PJ_DEF(pj_status_t) pjmedia_transport_udp_attach( pjmedia_endpt *endpt,
const char *name,
const pjmedia_sock_info *si,
unsigned options,
pjmedia_transport **p_tp)
{
struct transport_udp *tp;
pj_pool_t *pool;
pj_ioqueue_t *ioqueue;
pj_ioqueue_callback rtp_cb, rtcp_cb;
pj_ssize_t size;
unsigned i;
pj_status_t status;
/* Sanity check */
PJ_ASSERT_RETURN(endpt && si && p_tp, PJ_EINVAL);
/* Check name */
if (!name)
name = "udpmedia";
/* Get ioqueue instance */
ioqueue = pjmedia_endpt_get_ioqueue(endpt);
/* Create transport structure */
pool = pjmedia_endpt_create_pool(endpt, name, 4000, 4000);
if (!pool)
return PJ_ENOMEM;
tp = pj_pool_zalloc(pool, sizeof(struct transport_udp));
tp->pool = pool;
tp->options = options;
pj_ansi_strcpy(tp->base.name, name);
tp->base.op = &transport_udp_op;
/* Copy socket infos */
tp->rtp_sock = si->rtp_sock;
tp->rtp_addr_name = si->rtp_addr_name;
tp->rtcp_sock = si->rtcp_sock;
tp->rtcp_addr_name = si->rtcp_addr_name;
/* If address is 0.0.0.0, use host's IP address */
if (tp->rtp_addr_name.sin_addr.s_addr == 0) {
pj_in_addr hostip;
status = pj_gethostip(&hostip);
if (status != PJ_SUCCESS)
goto on_error;
tp->rtp_addr_name.sin_addr = hostip;
}
/* Same with RTCP */
if (tp->rtcp_addr_name.sin_addr.s_addr == 0) {
tp->rtcp_addr_name.sin_addr.s_addr = tp->rtp_addr_name.sin_addr.s_addr;
}
/* Setup RTP socket with the ioqueue */
pj_bzero(&rtp_cb, sizeof(rtp_cb));
rtp_cb.on_read_complete = &on_rx_rtp;
status = pj_ioqueue_register_sock(pool, ioqueue, tp->rtp_sock, tp,
&rtp_cb, &tp->rtp_key);
if (status != PJ_SUCCESS)
goto on_error;
pj_ioqueue_op_key_init(&tp->rtp_read_op, sizeof(tp->rtp_read_op));
for (i=0; i<PJ_ARRAY_SIZE(tp->rtp_pending_write); ++i)
pj_ioqueue_op_key_init(&tp->rtp_pending_write[i].op_key,
sizeof(tp->rtp_pending_write[i].op_key));
/* Kick of pending RTP read from the ioqueue */
tp->rtp_addrlen = sizeof(tp->rtp_src_addr);
size = sizeof(tp->rtp_pkt);
status = pj_ioqueue_recvfrom(tp->rtp_key, &tp->rtp_read_op,
tp->rtp_pkt, &size, PJ_IOQUEUE_ALWAYS_ASYNC,
&tp->rtp_src_addr, &tp->rtp_addrlen);
if (status != PJ_EPENDING)
goto on_error;
/* Setup RTCP socket with ioqueue */
pj_bzero(&rtcp_cb, sizeof(rtcp_cb));
rtcp_cb.on_read_complete = &on_rx_rtcp;
status = pj_ioqueue_register_sock(pool, ioqueue, tp->rtcp_sock, tp,
&rtcp_cb, &tp->rtcp_key);
if (status != PJ_SUCCESS)
goto on_error;
pj_ioqueue_op_key_init(&tp->rtcp_read_op, sizeof(tp->rtcp_read_op));
pj_ioqueue_op_key_init(&tp->rtcp_write_op, sizeof(tp->rtcp_write_op));
/* Kick of pending RTCP read from the ioqueue */
//.........这里部分代码省略.........
示例15: PJ_DEF
PJ_DEF(pj_status_t) pj_stun_detect_nat_type(const pj_sockaddr_in *server,
pj_stun_config *stun_cfg,
void *user_data,
pj_stun_nat_detect_cb *cb)
{
pj_pool_t *pool;
nat_detect_session *sess;
pj_stun_session_cb sess_cb;
pj_ioqueue_callback ioqueue_cb;
int addr_len;
pj_status_t status;
PJ_ASSERT_RETURN(server && stun_cfg, PJ_EINVAL);
PJ_ASSERT_RETURN(stun_cfg->pf && stun_cfg->ioqueue && stun_cfg->timer_heap,
PJ_EINVAL);
/*
* Init NAT detection session.
*/
pool = pj_pool_create(stun_cfg->pf, "natck%p", PJNATH_POOL_LEN_NATCK,
PJNATH_POOL_INC_NATCK, NULL);
if (!pool)
return PJ_ENOMEM;
sess = PJ_POOL_ZALLOC_T(pool, nat_detect_session);
sess->pool = pool;
sess->user_data = user_data;
sess->cb = cb;
status = pj_mutex_create_recursive(pool, pool->obj_name, &sess->mutex);
if (status != PJ_SUCCESS)
goto on_error;
pj_memcpy(&sess->server, server, sizeof(pj_sockaddr_in));
/*
* Init timer to self-destroy.
*/
sess->timer_heap = stun_cfg->timer_heap;
sess->timer.cb = &on_sess_timer;
sess->timer.user_data = sess;
/*
* Initialize socket.
*/
status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sess->sock);
if (status != PJ_SUCCESS)
goto on_error;
/*
* Bind to any.
*/
pj_bzero(&sess->local_addr, sizeof(pj_sockaddr_in));
sess->local_addr.sin_family = pj_AF_INET();
status = pj_sock_bind(sess->sock, &sess->local_addr,
sizeof(pj_sockaddr_in));
if (status != PJ_SUCCESS)
goto on_error;
/*
* Get local/bound address.
*/
addr_len = sizeof(sess->local_addr);
status = pj_sock_getsockname(sess->sock, &sess->local_addr, &addr_len);
if (status != PJ_SUCCESS)
goto on_error;
/*
* Find out which interface is used to send to the server.
*/
status = get_local_interface(server, &sess->local_addr.sin_addr);
if (status != PJ_SUCCESS)
goto on_error;
PJ_LOG(5,(sess->pool->obj_name, "Local address is %s:%d",
pj_inet_ntoa(sess->local_addr.sin_addr),
pj_ntohs(sess->local_addr.sin_port)));
PJ_LOG(5,(sess->pool->obj_name, "Server set to %s:%d",
pj_inet_ntoa(server->sin_addr),
pj_ntohs(server->sin_port)));
/*
* Register socket to ioqueue to receive asynchronous input
* notification.
*/
pj_bzero(&ioqueue_cb, sizeof(ioqueue_cb));
ioqueue_cb.on_read_complete = &on_read_complete;
status = pj_ioqueue_register_sock(sess->pool, stun_cfg->ioqueue,
sess->sock, sess, &ioqueue_cb,
&sess->key);
if (status != PJ_SUCCESS)
goto on_error;
/*
* Create STUN session.
*/
pj_bzero(&sess_cb, sizeof(sess_cb));
//.........这里部分代码省略.........