- Timestamp:
- Feb 23, 2007 1:07:54 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib-util/src/pjstun-client/client_main.c
r993 r996 19 19 #include <pjlib-util.h> 20 20 #include <pjlib.h> 21 #include "stun_session.h" 22 23 #include <conio.h> 21 24 22 25 23 26 #define THIS_FILE "client_main.c" 24 #define MAX_THREADS 825 27 28 29 static my_perror(const char *title, pj_status_t status) 30 { 31 char errmsg[PJ_ERR_MSG_SIZE]; 32 pj_strerror(status, errmsg, sizeof(errmsg)); 33 34 PJ_LOG(3,(THIS_FILE, "%s: %s", title, errmsg)); 35 } 36 37 static pj_status_t on_send_msg(pj_stun_tx_data *tdata, 38 const void *pkt, 39 pj_size_t pkt_size, 40 unsigned addr_len, 41 const pj_sockaddr_t *dst_addr) 42 { 43 pj_sock_t sock; 44 pj_ssize_t len; 45 pj_status_t status; 46 47 sock = (pj_sock_t) pj_stun_session_get_user_data(tdata->sess); 48 49 len = pkt_size; 50 status = pj_sock_sendto(sock, pkt, &len, 0, dst_addr, addr_len); 51 52 if (status != PJ_SUCCESS) 53 my_perror("Error sending packet", status); 54 55 return status; 56 } 57 58 static void on_bind_response(pj_stun_session *sess, 59 pj_status_t status, 60 pj_stun_tx_data *request, 61 const pj_stun_msg *response) 62 { 63 my_perror("on_bind_response()", status); 64 } 65 66 int main() 67 { 68 pj_stun_endpoint *endpt = NULL; 69 pj_pool_t *pool = NULL; 70 pj_caching_pool cp; 71 pj_timer_heap_t *th = NULL; 72 pj_stun_session *sess; 73 pj_sock_t sock = PJ_INVALID_SOCKET; 74 pj_sockaddr_in addr; 75 pj_stun_session_cb stun_cb; 76 pj_stun_tx_data *tdata; 77 pj_str_t s; 78 pj_status_t status; 79 80 status = pj_init(); 81 status = pjlib_util_init(); 82 83 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); 84 85 pool = pj_pool_create(&cp.factory, NULL, 1000, 1000, NULL); 86 87 status = pj_timer_heap_create(pool, 1000, &th); 88 pj_assert(status == PJ_SUCCESS); 89 90 status = pj_stun_endpoint_create(&cp.factory, 0, NULL, th, &endpt); 91 pj_assert(status == PJ_SUCCESS); 92 93 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock); 94 pj_assert(status == PJ_SUCCESS); 95 96 status = pj_sockaddr_in_init(&addr, pj_cstr(&s, "127.0.0.1"), PJ_STUN_PORT); 97 pj_assert(status == PJ_SUCCESS); 98 99 pj_memset(&stun_cb, 0, sizeof(stun_cb)); 100 stun_cb.on_send_msg = &on_send_msg; 101 stun_cb.on_bind_response = &on_bind_response; 102 103 status = pj_stun_session_create(endpt, NULL, &stun_cb, &sess); 104 pj_assert(status == PJ_SUCCESS); 105 106 pj_stun_session_set_user_data(sess, (void*)sock); 107 108 status = pj_stun_session_create_bind_req(sess, &tdata); 109 pj_assert(status == PJ_SUCCESS); 110 111 status = pj_stun_session_send_msg(sess, 0, sizeof(addr), &addr, tdata); 112 pj_assert(status == PJ_SUCCESS); 113 114 while (1) { 115 pj_fd_set_t rset; 116 int n; 117 pj_time_val timeout; 118 119 if (kbhit()) { 120 if (_getch()==27) 121 break; 122 } 123 124 PJ_FD_ZERO(&rset); 125 PJ_FD_SET(sock, &rset); 126 127 timeout.sec = 0; timeout.msec = 100; 128 129 n = pj_sock_select(FD_SETSIZE, &rset, NULL, NULL, &timeout); 130 131 if (PJ_FD_ISSET(sock, &rset)) { 132 char pkt[512]; 133 pj_ssize_t len; 134 135 len = sizeof(pkt); 136 status = pj_sock_recv(sock, pkt, &len, 0); 137 if (status == PJ_SUCCESS) { 138 pj_stun_session_on_rx_pkt(sess, pkt, len, NULL); 139 } 140 } 141 142 pj_timer_heap_poll(th, NULL); 143 } 144 145 on_return: 146 if (sock != PJ_INVALID_SOCKET) 147 pj_sock_close(sock); 148 if (endpt) 149 pj_stun_endpoint_destroy(endpt); 150 if (th) 151 pj_timer_heap_destroy(th); 152 if (pool) 153 pj_pool_release(pool); 154 pj_caching_pool_destroy(&cp); 155 156 return 0; 157 } 158 159
Note: See TracChangeset
for help on using the changeset viewer.