⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mg_user.c

📁 一个Megaco实现源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
    print_info_ushort("protocol version", infoP->_u.protocolVersion);    break;  default:    fprintf(stdout, "UNDEFINED(%d)", infoP->_d);    break;  }  fprintf(stdout, "\n");  fflush(stdout);}static void print_conn_info(Megaco_ConnInfoValue* infoP) {    fprintf(stdout, "conn info - ");  switch (infoP->_d) {  case Megaco_ConnInfo_controlPid:    print_pid("control pid: ", &infoP->_u.controlPid);    break;  case Megaco_ConnInfo_sendHandle:    print_info_send_handle(&infoP->_u.sendHandle);    break;  case Megaco_ConnInfo_receiveHandle:    print_info_recv_handle(&infoP->_u.rh);    break;      case Megaco_ConnInfo_transId:    print_info_long("next transaction id", infoP->_u.transId);    break;      case Megaco_ConnInfo_maxTransId:    print_info_long("max transaction id", infoP->_u.maxTransId);    break;      case Megaco_ConnInfo_autoAck:    print_info_bool("auto ack", infoP->_u.autoAck);    break;      case Megaco_ConnInfo_requestTimer:    print_info_timer("request timer", &infoP->_u.requestTimer);    break;  case Megaco_ConnInfo_longRequestTimer:    print_info_timer("long request timer", &infoP->_u.longRequestTimer);    break;  case Megaco_ConnInfo_replyTimer:    print_info_timer("reply timer", &infoP->_u.replyTimer);    break;  case Megaco_ConnInfo_pendingTimer:    print_info_timer("pending timer", &infoP->_u.pendingTimer);    break;  case Megaco_ConnInfo_sendMod:    print_info_str("send module", infoP->_u.sendMod);    break;  case Megaco_ConnInfo_encodingMod:    print_info_str("encoding module", infoP->_u.encodingMod);    break;  case Megaco_ConnInfo_encodingConfig:    print_info_str("encoding config", infoP->_u.encodingConfig);    break;  case Megaco_ConnInfo_replyData:    print_info_str("reply data", infoP->_u.replyData);    break;  case Megaco_ConnInfo_protocolVersion:    print_info_ushort("protocol version", infoP->_u.protocolVersion);    break;  default:    fprintf(stdout, "UNDEFINED(%d)", infoP->_d);    break;  }  fprintf(stdout, "\n");  fflush(stdout);}/* ================================================================ */static void insert_pending(void* state) {    /* Find next free position in the pending_messages array   * and store the state there.   */  while(1) {    ref_counter++;    if (ref_counter >= MAXPENDING) {      ref_counter = 0;    }    if (pending_messages[ref_counter] == NULL) {      pending_messages[ref_counter] = state;      break;    }  }}static void* delete_pending(CORBA_long ref) {  void* state;  if ((ref < MAXPENDING) && (0 <= ref)) {    state = pending_messages[ref];    pending_messages[ref] = NULL;  } else {    state = NULL;    fprintf(stderr, "message reference out of range: %d\n", (int) ref);    mg_exit(send_env, receive_env);  }  return state;}static void prepare_factory(void* state) {    insert_pending(state);   strcpy(send_env->_regname, MEGACO_SESSION_FACTORY);  send_env->_to_pid   = NULL;  send_env->_from_pid = &session_user_pid;}static void prepare_udp(void* state) {  insert_pending(state);  strcpy(send_env->_regname, MEGACO_SESSION_UDP);  send_env->_to_pid   = NULL;  send_env->_from_pid = &session_user_pid;}static void prepare_tcp(void* state) {  insert_pending(state);  strcpy(send_env->_regname, MEGACO_SESSION_TCP);  send_env->_to_pid   = NULL;  send_env->_from_pid = &session_user_pid;}static void prepare_session(void* state, erlang_pid* to_pid) {  insert_pending(state);    send_env->_regname[0] = 0;    send_env->_to_pid   = to_pid;  send_env->_from_pid = &session_user_pid;}/************************************************************ * Initiate Session User ************************************************************/extern void mg_user_initiate(int                v,			     int                s,			     char*              device_name,			     char*              local_node,			     char*              mhost,			     char*              trans,			     char*              enc,			     CORBA_Environment* s_env,			     CORBA_Environment* r_env) {  char reason[] = R901;  verbose = v;  stop    = s;  V( ("mg user initiate\n") );    strcpy(mgc_host,  mhost);  strcpy(transport, trans);  strcpy(encoding,  enc);  send_env    = s_env;  receive_env = r_env;  session_user_pid.num      = 98;  session_user_pid.serial   = 0;  session_user_pid.creation = 0;  strcpy(session_user_pid.node, local_node);  /* Some "constant" setups */  reason_cold_boot._buffer  = malloc(strlen(reason));  if (reason_cold_boot._buffer == NULL)     mg_exit(send_env, receive_env);  memcpy(reason_cold_boot._buffer, reason, strlen(reason));  reason_cold_boot._length  = strlen(reason);  reason_cold_boot._maximum = reason_cold_boot._length;  strcpy(dname, device_name);  mid._d        = MegacoMessage_MIdChoice_deviceName;  mid._u.device = dname;  /* Send initial startSession message in order to get a valid session pid */  V( ("startSession\n") );  prepare_factory(&dummy);  Megaco_SessionFactory_startSession(NULL, 				     ref_counter, 				     &session_user_pid,				     TRUE,				     send_env);  ASSERT_SEND();  state = MgsInitiated;}/************************************************************ * Terminate Session User ************************************************************/extern void mg_user_terminate(CORBA_Environment* s_env,			      CORBA_Environment* r_env) {  I( ("mg user terminate\n") );}/************************************************************ * Setup Transport ************************************************************/static void setup_transport() {  MegacoSessionIp_IpOption  opts[2];  MegacoSessionIp_IpOptions ipOptions;  V( ("setup transport\n") );  recv_handle.localMid = mid;    /* MGC host */  opts[0]._d      = MegacoSessionIp_Ip_host;  opts[0]._u.host = mgc_host;  /* Setup text port for UDP / TCP */  opts[1]._d      = MegacoSessionIp_Ip_port;  // opts[1]._u.port = 2944 or 2945;  ipOptions._buffer  = opts;  ipOptions._length  = 2;  ipOptions._maximum = 2;  if (strcmp(transport,TRANSPORT_TCP) == 0) {        /**     *     * T C P  c o n n e c t i o n  t o  M G C     *     */    prepare_tcp(&dummy);    recv_handle.sendMod = "megaco_tcp";    if (strcmp(encoding,ENCODING_TEXT) == 0) {          V( ("text codec\n") );      opts[1]._u.port = 2944;      /* prepare receive handle for tcp text */      recv_handle.encodingMod    = "megaco_pretty_text_encoder";      recv_handle.encodingConfig = "";          } else { /* Assume binary encoding then */      V( ("binary codec\n") );      opts[1]._u.port = 2945;            /* prepare receive handle for tcp text */      recv_handle.encodingMod    = "megaco_binary_encoder";      recv_handle.encodingConfig = "[native]";            }        V( ("open TCP connection\n") );    MegacoSessionTcp_connect(NULL,			     ref_counter,			     &session_user_pid,			     &recv_handle,			     &ipOptions,			     send_env);    ASSERT_SEND();      } else { /* Assume UDP then */    /**     *     * U D P  c o n n e c t i o n  t o  M G C     *     */        prepare_udp(&dummy);        recv_handle.sendMod = "megaco_udp";    if (strcmp(encoding,ENCODING_TEXT) == 0) {          V( ("text codec\n") );      opts[1]._u.port = 2944;      /* prepare receive handle for tcp text */      recv_handle.encodingMod    = "megaco_pretty_text_encoder";      recv_handle.encodingConfig = "";          } else { /* Assume binary encoding then */      V( ("binary codec\n") );      opts[1]._u.port = 2945;            /* prepare receive handle for tcp text */      recv_handle.encodingMod    = "megaco_binary_encoder";      recv_handle.encodingConfig = "native";            }    V( ("open UDP connection\n") );    MegacoSessionUdp_open(NULL,			  ref_counter,			  &session_user_pid,			  &recv_handle,			  &ipOptions,			  send_env);    ASSERT_SEND();      }  V( ("requests sent - now await response\n") );}/* static void stop_transport() { *//*   V( ("stop transport\n") ); *//*   if (strcmp(transport,TRANSPORT_TCP) == 0) { */    /*     /\** *//*      * *//*      * T C P  c o n n e c t i o n  t o  M G C *//*      * *//*      *\/ *//*     prepare_tcp(&dummy); *//*     V( ("close TCP connection\n") ); *//*     MegacoSessionTcp_close(NULL, ref_counter, *//* 			   &session_user_pid, &send_handle, *//* 			   send_env); */    /*   } else { /\* Assume UDP then *\/ *//*     /\** *//*      * *//*      * U D P  c o n n e c t i o n  t o  M G C *//*      * *//*      *\/ */    /*     prepare_udp(&dummy); */    /*     V( ("close UDP connection\n") ); *//*     MegacoSessionUdp_close(NULL, ref_counter, *//* 			   &session_user_pid, &send_handle, *//* 			   send_env); *//*   } *//*   ASSERT_SEND(); */    /*   V( ("requests sent - now await response\n") ); *//* } *//************************************************************ * Session User Callback Functions ************************************************************/Megaco_SessionUser_startSessionResponse__rs* Megaco_SessionUser_startSessionResponse__cb(Megaco_SessionUser oe_obj, 					    CORBA_long *ref, 					    Megaco_Status *status, 					    erlang_pid *pid,  					    CORBA_Environment *oe_env) {  Megaco_UserInfoValues uiv;  V( ("(session) received startSessionResponse\n") );  ASSERT_OK(status);  delete_pending(*ref); // Ignore returned data  state = MgsSessionStarted;  /* Now we have got the id of the session process, */  session_pid.num      = pid->num;  session_pid.serial   = pid->serial;  session_pid.creation = pid->creation;  strcpy(session_pid.node, pid->node);  /* Create the user info values    * The following values should be set:   * reply_timer & resend_timer   */  uiv._maximum = 0;  uiv._length  = 0;  uiv._buffer  = NULL;  prepare_session(&dummy, pid);  V( ("send startUser\n") );  Megaco_Session_startUser(NULL, *ref, &session_user_pid, 			   &mid, &uiv, send_env);  ASSERT_SEND();  free((void*)status);  return (Megaco_SessionUser_startSessionResponse__rs*)NULL;}Megaco_SessionUser_sessionsResponse__rs* Megaco_SessionUser_sessionsResponse__cb(Megaco_SessionUser oe_obj, 					CORBA_long *ref, 					Megaco_Status *status, 					Megaco_SessionRefList *reflist, 					CORBA_Environment *oe_env) {  I( ("(session) received sessionResponse\n") );  ASSERT_OK(status);  delete_pending(*ref); // Ignore returned data  free((void*)status);  free((void*)reflist);  return (Megaco_SessionUser_sessionsResponse__rs*)NULL;}Megaco_SessionUser_stopSessionResponse__rs* Megaco_SessionUser_stopSessionResponse__cb(Megaco_SessionUser oe_obj, 					   CORBA_long *ref, 					   Megaco_Status *status, 					   CORBA_Environment *oe_env) {    V( ("(session) received stopSessionResponse\n") );  ASSERT_OK(status);  I( ("we are done\n") );  mg_exit(send_env, receive_env);  free((void*)status);  return (Megaco_SessionUser_stopSessionResponse__rs*)NULL;}/**  * When the mid of the MGC is set to preliminary_mid,  * this function gets called two times. First as a result * of the connect call, and secondly as a result of  * sending the transaction request (service change) * using the preliminary_mid. */Megaco_SessionUser_handleConnect__rs* Megaco_SessionUser_handleConnect__cb(Megaco_SessionUser    oe_obj, 				     CORBA_long*           ref, 				     erlang_pid*           replyTo, 				     Megaco_ConnHandle*    handle, 				     CORBA_unsigned_short* XXX, 				     CORBA_Environment*    oe_env) {  Megaco_Status result;  V( ("(session) received handleConnect\n") );  P( print_pid("replyTo: ", replyTo) );  P( print_conn_handle("handle:  ", handle) );  delete_pending(*ref); // Ignore returned data  if (state != MgsConnected)    state = MgsConnectInit;    mchcpy(&conn_handle, handle);  result._d = Megaco_ok;  prepare_session(&dummy, replyTo);    V( ("send handleConnectResponse\n") );  Megaco_Session_handleConnectResponse(NULL, *ref, &result, send_env);  ASSERT_SEND();  free((void*)handle);  return (Megaco_SessionUser_handleConnect__rs*)NULL;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -