本文整理汇总了C++中pj_sockaddr_init函数的典型用法代码示例。如果您正苦于以下问题:C++ pj_sockaddr_init函数的具体用法?C++ pj_sockaddr_init怎么用?C++ pj_sockaddr_init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pj_sockaddr_init函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: init_stack
static pj_status_t init_stack()
{
pj_sockaddr addr;
pjsip_inv_callback inv_cb;
pj_status_t status;
pj_log_set_level(5);
status = pj_init();
CHECK_STATUS();
pj_log_set_level(3);
status = pjlib_util_init();
CHECK_STATUS();
pj_caching_pool_init(&app.cp, NULL, 0);
app.pool = pj_pool_create( &app.cp.factory, "sipecho", 512, 512, 0);
status = pjsip_endpt_create(&app.cp.factory, NULL, &app.sip_endpt);
CHECK_STATUS();
pj_log_set_level(4);
pj_sockaddr_init(AF, &addr, NULL, (pj_uint16_t)SIP_PORT);
if (AF == pj_AF_INET()) {
status = pjsip_udp_transport_start( app.sip_endpt, &addr.ipv4, NULL,
1, NULL);
} else if (AF == pj_AF_INET6()) {
status = pjsip_udp_transport_start6(app.sip_endpt, &addr.ipv6, NULL,
1, NULL);
} else {
status = PJ_EAFNOTSUP;
}
pj_log_set_level(3);
CHECK_STATUS();
status = pjsip_tsx_layer_init_module(app.sip_endpt) ||
pjsip_ua_init_module( app.sip_endpt, NULL );
CHECK_STATUS();
pj_bzero(&inv_cb, sizeof(inv_cb));
inv_cb.on_state_changed = &call_on_state_changed;
inv_cb.on_new_session = &call_on_forked;
inv_cb.on_media_update = &call_on_media_update;
inv_cb.on_rx_offer = &call_on_rx_offer;
status = pjsip_inv_usage_init(app.sip_endpt, &inv_cb) ||
pjsip_100rel_init_module(app.sip_endpt) ||
pjsip_endpt_register_module( app.sip_endpt, &mod_sipecho) ||
pjsip_endpt_register_module( app.sip_endpt, &msg_logger) ||
//pjmedia_endpt_create(&app.cp.factory,
// pjsip_endpt_get_ioqueue(app.sip_endpt),
// 0, &app.med_endpt) ||
pj_thread_create(app.pool, "sipecho", &worker_proc, NULL, 0, 0,
&app.worker_thread);
CHECK_STATUS();
return PJ_SUCCESS;
}
示例2: PJ_DEF
/* Get the default IP interface */
PJ_DEF(pj_status_t) pj_getdefaultipinterface(int af, pj_sockaddr *addr)
{
pj_sock_t fd;
pj_str_t cp;
pj_sockaddr a;
int len;
pj_uint8_t zero[64];
pj_status_t status;
addr->addr.sa_family = (pj_uint16_t)af;
status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &fd);
if (status != PJ_SUCCESS) {
return status;
}
PJ_LOG(4, ("sock_common.c", "pj_getdefaultipinterface() pj_sock_socket."));
if (af == PJ_AF_INET) {
cp = pj_str("1.1.1.1");
} else {
cp = pj_str("1::1");
}
status = pj_sockaddr_init(af, &a, &cp, 53);
if (status != PJ_SUCCESS) {
pj_sock_close(fd);
return status;
}
PJ_LOG(4, ("sock_common.c", "pj_getdefaultipinterface() pj_sockaddr_init."));
status = pj_sock_connect(fd, &a, pj_sockaddr_get_len(&a));
if (status != PJ_SUCCESS) {
pj_sock_close(fd);
return status;
}
PJ_LOG(4, ("sock_common.c", "pj_getdefaultipinterface() pj_sock_connect."));
len = sizeof(a);
status = pj_sock_getsockname(fd, &a, &len);
if (status != PJ_SUCCESS) {
pj_sock_close(fd);
return status;
}
PJ_LOG(4, ("sock_common.c", "pj_getdefaultipinterface() pj_sock_getsockname."));
pj_sock_close(fd);
/* Check that the address returned is not zero */
pj_bzero(zero, sizeof(zero));
if (pj_memcmp(pj_sockaddr_get_addr(&a), zero,
pj_sockaddr_get_addr_len(&a))==0)
{
PJ_LOG(4, ("sock_common.c", "pj_getdefaultipinterface() interface not found."));
return PJ_ENOTFOUND;
}
pj_sockaddr_copy_addr(addr, &a);
/* Success */
return PJ_SUCCESS;
}
示例3: PJ_DEF
/*
* Initialize pjsip_udp_transport_cfg structure with default values.
*/
PJ_DEF(void) pjsip_udp_transport_cfg_default(pjsip_udp_transport_cfg *cfg,
int af)
{
pj_bzero(cfg, sizeof(*cfg));
cfg->af = af;
pj_sockaddr_init(cfg->af, &cfg->bind_addr, NULL, 0);
cfg->async_cnt = 1;
}
示例4: dns_aaaa_callback
/*
* This callback is called when target is resolved with DNS AAAA query.
*/
static void dns_aaaa_callback(void *user_data,
pj_status_t status,
pj_dns_parsed_packet *pkt)
{
struct query *query = (struct query*) user_data;
pjsip_server_addresses *srv = &query->server;
/* Reset outstanding job */
query->object6 = NULL;
if (status == PJ_SUCCESS) {
pj_dns_addr_record rec;
unsigned i;
/* Parse the response */
rec.addr_count = 0;
status = pj_dns_parse_addr_response(pkt, &rec);
/* Build server addresses and call callback */
for (i = 0; i < rec.addr_count &&
srv->count < PJSIP_MAX_RESOLVED_ADDRESSES; ++i)
{
/* Should not happen, just in case */
if (rec.addr[i].af != pj_AF_INET6())
continue;
srv->entry[srv->count].type = query->naptr[0].type |
PJSIP_TRANSPORT_IPV6;
srv->entry[srv->count].priority = 0;
srv->entry[srv->count].weight = 0;
srv->entry[srv->count].addr_len = sizeof(pj_sockaddr_in6);
pj_sockaddr_init(pj_AF_INET6(), &srv->entry[srv->count].addr,
0, (pj_uint16_t)query->req.def_port);
srv->entry[srv->count].addr.ipv6.sin6_addr = rec.addr[i].ip.v6;
++srv->count;
}
}
if (status != PJ_SUCCESS) {
char errmsg[PJ_ERR_MSG_SIZE];
/* Log error */
pj_strerror(status, errmsg, sizeof(errmsg));
PJ_LOG(4,(query->objname, "DNS AAAA record resolution failed: %s",
errmsg));
query->last_error = status;
}
/* Call the callback if all DNS queries have been completed */
if (query->object == NULL && query->object6 == NULL) {
if (srv->count > 0)
(*query->cb)(PJ_SUCCESS, query->token, &query->server);
else
(*query->cb)(query->last_error, query->token, NULL);
}
}
示例5: PJ_DEF
/*
* Initialize pjsip_tcp_transport_cfg structure with default values.
*/
PJ_DEF(void) pjsip_tcp_transport_cfg_default(pjsip_tcp_transport_cfg *cfg,
int af)
{
pj_bzero(cfg, sizeof(*cfg));
cfg->af = af;
pj_sockaddr_init(cfg->af, &cfg->bind_addr, NULL, 0);
cfg->async_cnt = 1;
cfg->reuse_addr = PJSIP_TCP_TRANSPORT_REUSEADDR;
}
示例6: srv_resolver_cb
/* Callback to be called by DNS SRV resolution */
static void srv_resolver_cb(void *user_data,
pj_status_t status,
const pj_dns_srv_record *rec)
{
struct query *query = (struct query*) user_data;
pjsip_server_addresses srv;
unsigned i;
if (status != PJ_SUCCESS) {
char errmsg[PJ_ERR_MSG_SIZE];
/* Log error */
pj_strerror(status, errmsg, sizeof(errmsg));
PJ_LOG(4,(query->objname, "DNS A/AAAA record resolution failed: %s",
errmsg));
/* Call the callback */
(*query->cb)(status, query->token, NULL);
return;
}
/* Build server addresses and call callback */
srv.count = 0;
for (i=0; i<rec->count; ++i) {
const pj_dns_addr_record *s = &rec->entry[i].server;
unsigned j;
for (j = 0; j < s->addr_count &&
srv.count < PJSIP_MAX_RESOLVED_ADDRESSES; ++j)
{
srv.entry[srv.count].type = query->naptr[0].type;
srv.entry[srv.count].priority = rec->entry[i].priority;
srv.entry[srv.count].weight = rec->entry[i].weight;
pj_sockaddr_init(s->addr[j].af,
&srv.entry[srv.count].addr,
0, (pj_uint16_t)rec->entry[i].port);
if (s->addr[j].af == pj_AF_INET6())
srv.entry[srv.count].addr.ipv6.sin6_addr = s->addr[j].ip.v6;
else
srv.entry[srv.count].addr.ipv4.sin_addr = s->addr[j].ip.v4;
srv.entry[srv.count].addr_len =
pj_sockaddr_get_len(&srv.entry[srv.count].addr);
/* Update transport type if this is IPv6 */
if (s->addr[j].af == pj_AF_INET6())
srv.entry[srv.count].type |= PJSIP_TRANSPORT_IPV6;
++srv.count;
}
}
/* Call the callback */
(*query->cb)(PJ_SUCCESS, query->token, &srv);
}
示例7: PJ_DEF
PJ_DEF(pj_status_t) pj_activesock_create_udp( pj_pool_t *pool,
const pj_sockaddr *addr,
const pj_activesock_cfg *opt,
pj_ioqueue_t *ioqueue,
const pj_activesock_cb *cb,
void *user_data,
pj_activesock_t **p_asock,
pj_sockaddr *bound_addr)
{
pj_sock_t sock_fd;
pj_sockaddr default_addr;
pj_status_t status;
if (addr == NULL) {
pj_sockaddr_init(pj_AF_INET(), &default_addr, NULL, 0);
addr = &default_addr;
}
status = pj_sock_socket(addr->addr.sa_family, pj_SOCK_DGRAM(), 0,
&sock_fd);
if (status != PJ_SUCCESS) {
return status;
}
status = pj_sock_bind(sock_fd, addr, pj_sockaddr_get_len(addr));
if (status != PJ_SUCCESS) {
pj_sock_close(sock_fd);
return status;
}
status = pj_activesock_create(pool, sock_fd, pj_SOCK_DGRAM(), opt,
ioqueue, cb, user_data, p_asock);
if (status != PJ_SUCCESS) {
pj_sock_close(sock_fd);
return status;
}
if (bound_addr) {
int addr_len = sizeof(*bound_addr);
status = pj_sock_getsockname(sock_fd, bound_addr, &addr_len);
if (status != PJ_SUCCESS) {
pj_activesock_close(*p_asock);
return status;
}
}
return PJ_SUCCESS;
}
示例8: create_msgint3
static pj_stun_msg* create_msgint3(pj_pool_t *pool, test_vector *v)
{
pj_stun_msg *msg;
pj_sockaddr mapped_addr;
pj_str_t s1;
pj_status_t status;
status = pj_stun_msg_create(pool, v->msg_type, PJ_STUN_MAGIC,
(pj_uint8_t*)v->tsx_id, &msg);
if (status != PJ_SUCCESS)
goto on_error;
status = pj_stun_msg_add_string_attr(pool, msg, PJ_STUN_ATTR_SOFTWARE,
pj_cstr(&s1, "test vector"));
if (status != PJ_SUCCESS)
goto on_error;
status = pj_sockaddr_init(pj_AF_INET6(), &mapped_addr,
pj_cstr(&s1, "2001:db8:1234:5678:11:2233:4455:6677"),
32853);
if (status != PJ_SUCCESS)
goto on_error;
status = pj_stun_msg_add_sockaddr_attr(pool, msg,
PJ_STUN_ATTR_XOR_MAPPED_ADDR,
PJ_TRUE, &mapped_addr,
sizeof(pj_sockaddr));
if (status != PJ_SUCCESS)
goto on_error;
status = pj_stun_msg_add_msgint_attr(pool, msg);
if (status != PJ_SUCCESS)
goto on_error;
status = pj_stun_msg_add_uint_attr(pool, msg, PJ_STUN_ATTR_FINGERPRINT, 0);
if (status != PJ_SUCCESS)
goto on_error;
return msg;
on_error:
app_perror(" error: create_msgint3()", status);
return NULL;
}
示例9: get_local_interface
static pj_status_t get_local_interface(const pj_sockaddr *server,
pj_sockaddr *local_addr)
{
pj_sock_t sock;
pj_sockaddr tmp, local;
int addr_len;
pj_status_t status;
status = pj_sock_socket(server->addr.sa_family, pj_SOCK_DGRAM(),
0, &sock);
if (status != PJ_SUCCESS)
return status;
addr_len = pj_sockaddr_get_len(server);
pj_sockaddr_init(server->addr.sa_family, &local, NULL, 0);
status = pj_sock_bind(sock, &local, addr_len);
if (status != PJ_SUCCESS) {
pj_sock_close(sock);
return status;
}
status = pj_sock_connect(sock, server, addr_len);
if (status != PJ_SUCCESS) {
pj_sock_close(sock);
return status;
}
status = pj_sock_getsockname(sock, &tmp, &addr_len);
if (status != PJ_SUCCESS) {
pj_sock_close(sock);
return status;
}
pj_sockaddr_cp(local_addr, &tmp);
pj_sock_close(sock);
return PJ_SUCCESS;
}
示例10: dns_srv_resolver_cb
/* DNS resolver callback */
static void dns_srv_resolver_cb(void *user_data,
pj_status_t status,
const pj_dns_srv_record *rec)
{
pj_stun_sock *stun_sock = (pj_stun_sock*) user_data;
pj_grp_lock_acquire(stun_sock->grp_lock);
/* Clear query */
stun_sock->q = NULL;
/* Handle error */
if (status != PJ_SUCCESS) {
sess_fail(stun_sock, PJ_STUN_SOCK_DNS_OP, status);
pj_grp_lock_release(stun_sock->grp_lock);
return;
}
pj_assert(rec->count);
pj_assert(rec->entry[0].server.addr_count);
pj_assert(rec->entry[0].server.addr[0].af == stun_sock->af);
/* Set the address */
pj_sockaddr_init(stun_sock->af, &stun_sock->srv_addr, NULL,
rec->entry[0].port);
if (stun_sock->af == pj_AF_INET6()) {
stun_sock->srv_addr.ipv6.sin6_addr =
rec->entry[0].server.addr[0].ip.v6;
} else {
stun_sock->srv_addr.ipv4.sin_addr =
rec->entry[0].server.addr[0].ip.v4;
}
/* Start sending Binding request */
get_mapped_addr(stun_sock);
pj_grp_lock_release(stun_sock->grp_lock);
}
示例11: PJ_DEF
/*
* This is the public API to create, initialize, register, and start the
* TCP listener.
*/
PJ_DEF(pj_status_t) pjsip_tcp_transport_start2(pjsip_endpoint *endpt,
const pj_sockaddr_in *local,
const pjsip_host_port *a_name,
unsigned async_cnt,
pjsip_tpfactory **p_factory)
{
pjsip_tcp_transport_cfg cfg;
pjsip_tcp_transport_cfg_default(&cfg, pj_AF_INET());
if (local)
pj_sockaddr_cp(&cfg.bind_addr, local);
else
pj_sockaddr_init(cfg.af, &cfg.bind_addr, NULL, 0);
if (a_name)
pj_memcpy(&cfg.addr_name, a_name, sizeof(*a_name));
if (async_cnt)
cfg.async_cnt = async_cnt;
return pjsip_tcp_transport_start3(endpt, &cfg, p_factory);
}
示例12: perf_test
/* Test will perform multiple clients trying to connect to single server.
* Once SSL connection established, echo test will be performed.
*/
static int perf_test(unsigned clients, unsigned ms_handshake_timeout)
{
pj_pool_t *pool = NULL;
pj_ioqueue_t *ioqueue = NULL;
pj_timer_heap_t *timer = NULL;
pj_ssl_sock_t *ssock_serv = NULL;
pj_ssl_sock_t **ssock_cli = NULL;
pj_ssl_sock_param param;
struct test_state state_serv = { 0 };
struct test_state *state_cli = NULL;
pj_sockaddr addr, listen_addr;
pj_ssl_cert_t *cert = NULL;
pj_status_t status;
unsigned i, cli_err = 0, tot_sent = 0, tot_recv = 0;
pj_time_val start;
pool = pj_pool_create(mem, "ssl_perf", 256, 256, NULL);
status = pj_ioqueue_create(pool, PJ_IOQUEUE_MAX_HANDLES, &ioqueue);
if (status != PJ_SUCCESS) {
goto on_return;
}
status = pj_timer_heap_create(pool, PJ_IOQUEUE_MAX_HANDLES, &timer);
if (status != PJ_SUCCESS) {
goto on_return;
}
/* Set cert */
{
pj_str_t tmp1, tmp2, tmp3, tmp4;
status = pj_ssl_cert_load_from_files(pool,
pj_strset2(&tmp1, (char*)CERT_CA_FILE),
pj_strset2(&tmp2, (char*)CERT_FILE),
pj_strset2(&tmp3, (char*)CERT_PRIVKEY_FILE),
pj_strset2(&tmp4, (char*)CERT_PRIVKEY_PASS),
&cert);
if (status != PJ_SUCCESS) {
goto on_return;
}
}
pj_ssl_sock_param_default(¶m);
param.cb.on_accept_complete = &ssl_on_accept_complete;
param.cb.on_connect_complete = &ssl_on_connect_complete;
param.cb.on_data_read = &ssl_on_data_read;
param.cb.on_data_sent = &ssl_on_data_sent;
param.ioqueue = ioqueue;
param.timer_heap = timer;
param.timeout.sec = 0;
param.timeout.msec = ms_handshake_timeout;
pj_time_val_normalize(¶m.timeout);
/* Init default bind address */
{
pj_str_t tmp_st;
pj_sockaddr_init(PJ_AF_INET, &addr, pj_strset2(&tmp_st, "127.0.0.1"), 0);
}
/* SERVER */
param.user_data = &state_serv;
state_serv.pool = pool;
state_serv.echo = PJ_TRUE;
state_serv.is_server = PJ_TRUE;
status = pj_ssl_sock_create(pool, ¶m, &ssock_serv);
if (status != PJ_SUCCESS) {
goto on_return;
}
status = pj_ssl_sock_set_certificate(ssock_serv, pool, cert);
if (status != PJ_SUCCESS) {
goto on_return;
}
status = pj_ssl_sock_start_accept(ssock_serv, pool, &addr, pj_sockaddr_get_len(&addr));
if (status != PJ_SUCCESS) {
goto on_return;
}
/* Get listening address for clients to connect to */
{
pj_ssl_sock_info info;
char buf[64];
pj_ssl_sock_get_info(ssock_serv, &info);
pj_sockaddr_cp(&listen_addr, &info.local_addr);
pj_sockaddr_print((pj_sockaddr_t*)&listen_addr, buf, sizeof(buf), 1);
PJ_LOG(3, ("", "...Listener ready at %s", buf));
}
/* CLIENTS */
clients_num = clients;
//.........这里部分代码省略.........
示例13: PJ_DEF
/*
* Create stream info from SDP media line.
*/
PJ_DEF(pj_status_t) pjmedia_vid_stream_info_from_sdp(
pjmedia_vid_stream_info *si,
pj_pool_t *pool,
pjmedia_endpt *endpt,
const pjmedia_sdp_session *local,
const pjmedia_sdp_session *remote,
unsigned stream_idx)
{
const pjmedia_sdp_attr *attr;
const pjmedia_sdp_media *local_m;
const pjmedia_sdp_media *rem_m;
const pjmedia_sdp_conn *local_conn;
const pjmedia_sdp_conn *rem_conn;
int rem_af, local_af;
pj_sockaddr local_addr;
pj_status_t status;
PJ_UNUSED_ARG(endpt);
/* Validate arguments: */
PJ_ASSERT_RETURN(pool && si && local && remote, PJ_EINVAL);
PJ_ASSERT_RETURN(stream_idx < local->media_count, PJ_EINVAL);
PJ_ASSERT_RETURN(stream_idx < remote->media_count, PJ_EINVAL);
/* Keep SDP shortcuts */
local_m = local->media[stream_idx];
rem_m = remote->media[stream_idx];
local_conn = local_m->conn ? local_m->conn : local->conn;
if (local_conn == NULL)
return PJMEDIA_SDP_EMISSINGCONN;
rem_conn = rem_m->conn ? rem_m->conn : remote->conn;
if (rem_conn == NULL)
return PJMEDIA_SDP_EMISSINGCONN;
/* Media type must be video */
if (pj_stricmp(&local_m->desc.media, &ID_VIDEO) != 0)
return PJMEDIA_EINVALIMEDIATYPE;
/* Reset: */
pj_bzero(si, sizeof(*si));
/* Media type: */
si->type = PJMEDIA_TYPE_VIDEO;
/* Transport protocol */
/* At this point, transport type must be compatible,
* the transport instance will do more validation later.
*/
status = pjmedia_sdp_transport_cmp(&rem_m->desc.transport,
&local_m->desc.transport);
if (status != PJ_SUCCESS)
return PJMEDIA_SDPNEG_EINVANSTP;
if (pj_stricmp(&local_m->desc.transport, &ID_RTP_AVP) == 0) {
si->proto = PJMEDIA_TP_PROTO_RTP_AVP;
} else if (pj_stricmp(&local_m->desc.transport, &ID_RTP_SAVP) == 0) {
si->proto = PJMEDIA_TP_PROTO_RTP_SAVP;
} else {
si->proto = PJMEDIA_TP_PROTO_UNKNOWN;
return PJ_SUCCESS;
}
/* Check address family in remote SDP */
rem_af = pj_AF_UNSPEC();
if (pj_stricmp(&rem_conn->net_type, &ID_IN)==0) {
if (pj_stricmp(&rem_conn->addr_type, &ID_IP4)==0) {
rem_af = pj_AF_INET();
} else if (pj_stricmp(&rem_conn->addr_type, &ID_IP6)==0) {
rem_af = pj_AF_INET6();
}
}
if (rem_af==pj_AF_UNSPEC()) {
/* Unsupported address family */
return PJ_EAFNOTSUP;
}
/* Set remote address: */
status = pj_sockaddr_init(rem_af, &si->rem_addr, &rem_conn->addr,
rem_m->desc.port);
if (status != PJ_SUCCESS) {
/* Invalid IP address. */
return PJMEDIA_EINVALIDIP;
}
/* Check address family of local info */
//.........这里部分代码省略.........
示例14: PJ_DEF
/*
* Create a new listener on the specified port.
*/
PJ_DEF(pj_status_t) pj_turn_listener_create_udp( pj_turn_srv *srv,
int af,
const pj_str_t *bound_addr,
unsigned port,
unsigned concurrency_cnt,
unsigned flags,
pj_turn_listener **p_listener)
{
pj_pool_t *pool;
struct udp_listener *udp;
pj_ioqueue_callback ioqueue_cb;
unsigned i;
pj_status_t status;
/* Create structure */
pool = pj_pool_create(srv->core.pf, "udp%p", 1000, 1000, NULL);
udp = PJ_POOL_ZALLOC_T(pool, struct udp_listener);
udp->base.pool = pool;
udp->base.obj_name = pool->obj_name;
udp->base.server = srv;
udp->base.tp_type = PJ_TURN_TP_UDP;
udp->base.sock = PJ_INVALID_SOCKET;
udp->base.destroy = &udp_destroy;
udp->read_cnt = concurrency_cnt;
udp->base.flags = flags;
udp->tp.obj_name = udp->base.obj_name;
udp->tp.info = udp->base.info;
udp->tp.listener = &udp->base;
udp->tp.sendto = &udp_sendto;
udp->tp.add_ref = &udp_add_ref;
udp->tp.dec_ref = &udp_dec_ref;
/* Create socket */
status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &udp->base.sock);
if (status != PJ_SUCCESS)
goto on_error;
/* Init bind address */
status = pj_sockaddr_init(af, &udp->base.addr, bound_addr,
(pj_uint16_t)port);
if (status != PJ_SUCCESS)
goto on_error;
/* Create info */
pj_ansi_strcpy(udp->base.info, "UDP:");
pj_sockaddr_print(&udp->base.addr, udp->base.info+4,
sizeof(udp->base.info)-4, 3);
/* Bind socket */
status = pj_sock_bind(udp->base.sock, &udp->base.addr,
pj_sockaddr_get_len(&udp->base.addr));
if (status != PJ_SUCCESS)
goto on_error;
/* Register to ioqueue */
pj_bzero(&ioqueue_cb, sizeof(ioqueue_cb));
ioqueue_cb.on_read_complete = on_read_complete;
status = pj_ioqueue_register_sock(pool, srv->core.ioqueue, udp->base.sock,
udp, &ioqueue_cb, &udp->key);
/* Create op keys */
udp->read_op = (struct read_op**)pj_pool_calloc(pool, concurrency_cnt,
sizeof(struct read_op*));
/* Create each read_op and kick off read operation */
for (i=0; i<concurrency_cnt; ++i) {
pj_pool_t *rpool = pj_pool_create(srv->core.pf, "rop%p",
1000, 1000, NULL);
udp->read_op[i] = PJ_POOL_ZALLOC_T(pool, struct read_op);
udp->read_op[i]->pkt.pool = rpool;
on_read_complete(udp->key, &udp->read_op[i]->op_key, 0);
}
/* Done */
PJ_LOG(4,(udp->base.obj_name, "Listener %s created", udp->base.info));
*p_listener = &udp->base;
return PJ_SUCCESS;
on_error:
udp_destroy(&udp->base);
return status;
}
示例15: stun_destroy_test
static int stun_destroy_test(void)
{
enum { LOOP = 500 };
struct stun_test_session test_sess;
pj_sockaddr bind_addr;
int addr_len;
pj_caching_pool cp;
pj_pool_t *pool;
unsigned i;
pj_status_t status;
int rc = 0;
PJ_LOG(3,(THIS_FILE, " STUN destroy concurrency test"));
pj_bzero(&test_sess, sizeof(test_sess));
pj_caching_pool_init(&cp, NULL, 0);
pool = pj_pool_create(&cp.factory, "testsess", 512, 512, NULL);
pj_stun_config_init(&test_sess.stun_cfg, &cp.factory, 0, NULL, NULL);
status = pj_timer_heap_create(pool, 1023, &test_sess.stun_cfg.timer_heap);
pj_assert(status == PJ_SUCCESS);
status = pj_lock_create_recursive_mutex(pool, NULL, &test_sess.lock);
pj_assert(status == PJ_SUCCESS);
pj_timer_heap_set_lock(test_sess.stun_cfg.timer_heap, test_sess.lock, PJ_TRUE);
pj_assert(status == PJ_SUCCESS);
status = pj_ioqueue_create(pool, 512, &test_sess.stun_cfg.ioqueue);
pj_assert(status == PJ_SUCCESS);
pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &test_sess.server_sock);
pj_sockaddr_init(pj_AF_INET(), &bind_addr, NULL, 0);
status = pj_sock_bind(test_sess.server_sock, &bind_addr, pj_sockaddr_get_len(&bind_addr));
pj_assert(status == PJ_SUCCESS);
addr_len = sizeof(bind_addr);
status = pj_sock_getsockname(test_sess.server_sock, &bind_addr, &addr_len);
pj_assert(status == PJ_SUCCESS);
test_sess.server_port = pj_sockaddr_get_port(&bind_addr);
status = pj_event_create(pool, NULL, PJ_TRUE, PJ_FALSE, &test_sess.server_event);
pj_assert(status == PJ_SUCCESS);
for (i=0; i<SERVER_THREAD_CNT; ++i) {
status = pj_thread_create(pool, NULL,
&server_thread_proc, &test_sess,
0, 0, &test_sess.server_threads[i]);
pj_assert(status == PJ_SUCCESS);
}
for (i=0; i<WORKER_THREAD_CNT; ++i) {
status = pj_thread_create(pool, NULL,
&worker_thread_proc, &test_sess,
0, 0, &test_sess.worker_threads[i]);
pj_assert(status == PJ_SUCCESS);
}
/* Test 1: Main thread calls destroy while callback is processing response */
PJ_LOG(3,(THIS_FILE, " Destroy in main thread while callback is running"));
for (i=0; i<LOOP; ++i) {
int sleep = pj_rand() % 5;
PJ_LOG(3,(THIS_FILE, " Try %-3d of %d", i+1, LOOP));
/* Test 1: destroy at the same time when receiving response */
pj_bzero(&test_sess.param, sizeof(test_sess.param));
test_sess.param.client_sleep_after_start = 20;
test_sess.param.client_sleep_before_destroy = sleep;
test_sess.param.server_wait_for_event = PJ_TRUE;
stun_destroy_test_session(&test_sess);
PJ_LOG(3,(THIS_FILE,
" stun test a: sleep delay:%d: clients with response: %d",
sleep, test_sess.param.client_got_response));
/* Test 2: destroy at the same time with STUN retransmit timer */
test_sess.param.server_drop_request = PJ_TRUE;
test_sess.param.client_sleep_after_start = 0;
test_sess.param.client_sleep_before_destroy = PJ_STUN_RTO_VALUE;
test_sess.param.server_wait_for_event = PJ_FALSE;
stun_destroy_test_session(&test_sess);
PJ_LOG(3,(THIS_FILE, " stun test b: retransmit concurrency"));
/* Test 3: destroy at the same time with receiving response
* AND STUN retransmit timer */
test_sess.param.client_got_response = 0;
test_sess.param.server_drop_request = PJ_FALSE;
test_sess.param.client_sleep_after_start = PJ_STUN_RTO_VALUE;
test_sess.param.client_sleep_before_destroy = 0;
test_sess.param.server_wait_for_event = PJ_TRUE;
stun_destroy_test_session(&test_sess);
PJ_LOG(3,(THIS_FILE,
" stun test c: clients with response: %d",
test_sess.param.client_got_response));
pj_thread_sleep(10);
//.........这里部分代码省略.........