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

📄 gameswf_xmlsocket.cpp

📁 一个开源的嵌入式flash播放器的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
}voidXMLSocket::push(as_object_interface *obj){  _nodes.push_back(obj);}voidXMLSocket::clear(){  int i;  for (i=0; i< _nodes.size(); i++) {    delete _nodes[i];  }}intXMLSocket::count(){  return _nodes.size();}intXMLSocket::checkSockets(void){  return checkSockets(_sockfd);}intXMLSocket::checkSockets(int fd){  fd_set                fdset;  int                   ret = 0;  struct timeval        tval;  //log_msg("%s:\n", __FUNCTION__);  FD_ZERO(&fdset);  FD_SET(fd, &fdset);    tval.tv_sec = 2;  tval.tv_usec = 10;    ret = ::select(fd+1, &fdset, NULL, NULL, &tval); // &tval    // If interupted by a system call, try again  if (ret == -1 && errno == EINTR) {    log_msg("The socket for fd #%d was interupted by a system call in this thread!\n",            fd);  }  if (ret == -1) {    log_error("The socket for fd #%d never was available!\n", fd);  }  if (ret == 0) {    printf("There is no data in the socket for fd #%d!\n", fd);  }  if (ret > 0) {    //printf("There is data in the socket for fd #%d!\n", fd);          }    return ret;}voidxmlsocket_connect(const fn_call& fn){  as_value	method;  as_value	val;  static bool first = true;     // This event handler should only be executed once.  bool          ret;  const array<with_stack_entry> with_stack;  if (!first) {    fn.result->set(true);    return;  }    log_msg("%s: nargs=%d\n", __FUNCTION__, fn.nargs);  xmlsocket_as_object*	ptr = (xmlsocket_as_object*) (as_object*) fn.this_ptr;  assert(ptr);  const tu_string host = fn.env->bottom(fn.first_arg_bottom_index).to_string();  tu_string port_str = fn.env->bottom(fn.first_arg_bottom_index-1).to_tu_string();  double port = atof(port_str.c_str());  ret = ptr->obj.connect(host, static_cast<int>(port));#if 0  // Push result onto stack for onConnect  if (ret) {    fn.env->push(as_value(true));  }  else {    fn.env->push(as_value(false));  }#endif  fn.env->push(as_value(true));  if (fn.this_ptr->get_member("onConnect", &method)) {    //    log_msg("FIXME: Found onConnect!\n");    as_c_function_ptr	func = method.to_c_function();    first = false;    //env->set_variable("success", true, 0);    if (func) {      // It's a C function.  Call it.      log_msg("Calling C function for onConnect\n");      (*func)(fn_call(&val, fn.this_ptr, fn.env, 0, 0));    }    else if (as_as_function* as_func = method.to_as_function()) {      // It's an ActionScript function.  Call it.      log_msg("Calling ActionScript function for onConnect\n");      (*as_func)(fn_call(&val, fn.this_ptr, fn.env, 2, 2));    } else {      log_error("error in call_method(): method is not a function\n");    }      } else {    //ptr->set_event_handler(event_id::SOCK_CONNECT, (as_c_function_ptr)&xmlsocket_event_connect);  }#if 1  movie*	mov = fn.env->get_target()->get_root_movie();  Timer *timer = new Timer;  as_c_function_ptr ondata_handler =    (as_c_function_ptr)&xmlsocket_event_ondata;  timer->setInterval(ondata_handler, 50, ptr, fn.env);  timer->setObject(ptr);  mov->add_interval_timer(timer);#endif  fn.env->pop();    fn.result->set(true);}voidxmlsocket_send(const fn_call& fn){  as_value	method;  as_value	val;    xmlsocket_as_object*	ptr = (xmlsocket_as_object*) (as_object*) fn.this_ptr;  assert(ptr);  const tu_string object = fn.env->bottom( fn.first_arg_bottom_index).to_string();  //  log_msg("%s: host=%s, port=%g\n", __FUNCTION__, host, port);  fn.result->set(ptr->obj.send(object));}voidxmlsocket_close(const fn_call& fn){  as_value	method;  as_value	val;    xmlsocket_as_object*	ptr = (xmlsocket_as_object*) (as_object*) fn.this_ptr;  assert(ptr);  // Since the return code from close() doesn't get used by Shockwave,  // we don't care either.  ptr->obj.close();}voidxmlsocket_xml_new(const fn_call& fn){  //log_msg("%s: nargs=%d\n", __FUNCTION__, nargs);    xml_new(fn);}voidxmlsocket_new(const fn_call& fn){  //log_msg("%s: nargs=%d\n", __FUNCTION__, nargs);    as_object*	xmlsock_obj = new xmlsocket_as_object;  //log_msg("\tCreated New XMLSocket object at 0x%X\n", (unsigned int)xmlsock_obj);  xmlsock_obj->set_member("connect", &xmlsocket_connect);  xmlsock_obj->set_member("send", &xmlsocket_send);  xmlsock_obj->set_member("close", &xmlsocket_close);  xmlsock_obj->set_member("Connected", true);  // swf_event*	ev = new swf_event;  // m_event_handlers.push_back(ev);  // Setup event handlers#if 0  xmlsock_obj->set_event_handler(event_id::SOCK_DATA,                                 (as_c_function_ptr)&xmlsocket_event_ondata);  xmlsock_obj->set_event_handler(event_id::SOCK_CLOSE,                                 (as_c_function_ptr)&xmlsocket_event_close);  // 							xmlsock_obj->set_event_handler(event_id::SOCK_CONNECT,  // 									       (as_c_function_ptr)&xmlsocket_event_connect);  xmlsock_obj->set_event_handler(event_id::SOCK_XML,                                 (as_c_function_ptr)&xmlsocket_event_xml);#endif  //periodic_events.set_event_handler(xmlsock_obj);    #if 1  //as_c_function_ptr int_handler = (as_c_function_ptr)&timer_setinterval;  //env->set_member("setInterval", int_handler);  fn.env->set_member("setInterval", timer_setinterval);    //as_c_function_ptr clr_handler = timer_clearinterval;  fn.env->set_member("clearInterval", timer_clearinterval);  //env->set_variable("setInterval", int_handler, 0);  //xmlsock_obj->set_event_handler(event_id::TIMER,  //       (as_c_function_ptr)&timer_expire);#if 0  Timer *timer = new Timer;  as_c_function_ptr ondata_handler =    (as_c_function_ptr)&xmlsocket_event_ondata;  timer->setInterval(ondata_handler, 10);  timer->setObject(xmlsock_obj);  current_movie->add_interval_timer(timer);#endif    fn.result->set(xmlsock_obj);  // Tune malloc for the best performance  //mallopt(M_MMAP_MAX,0);  //mallopt(M_TRIM_THRESHOLD,-1);  //mallopt(M_MMAP_THRESHOLD,16);#endif}voidxmlsocket_event_ondata(const fn_call& fn){  //log_msg("%s: nargs is %d\n", __FUNCTION__, nargs);      as_value	method;  as_value	val;  as_value      datain;  array<const char *> msgs;  char          *messages[200];  int           i;  as_c_function_ptr	func;  as_as_function*       as_func;  tu_string     data;   xmlsocket_as_object*	ptr = (xmlsocket_as_object*)fn.this_ptr;  assert(ptr);  if (ptr->obj.processingData()) {    log_msg("Still processing data!\n");    fn.result->set(false);    return;  }    memset(messages, 0, sizeof(char *)*200);  #ifndef USE_DMALLOC  //dump_memory_stats(__FUNCTION__, __LINE__, "memory checkpoint");#endif    if (ptr->obj.anydata(messages)) {    if (fn.this_ptr->get_member("onData", &method)) {      func = method.to_c_function();      as_func = method.to_as_function();      //log_msg("Got %d messages from XMLsocket\n", msgs.size());      //      for (i=0; i<msgs.size(); i++) {      for (i=0; messages[i] != 0; i++) {//          log_msg("Got message #%d, %d bytes long at %p: %s: \n", i,//                  strlen(messages[i]), messages[i], messages[i]);        datain = messages[i];        //fn.env->push(datain);#ifndef USE_DMALLOC        //dump_memory_stats(__FUNCTION__, __LINE__, "start");#endif        as_environment *env = new as_environment;        env->push(datain);        if (func) {          // It's a C function.  Call it.          //log_msg("Calling C function for onData\n");          (*func)(fn_call(&val, fn.this_ptr, env, 1, 0));        } else if (as_func) {          // It's an ActionScript function.  Call it.          //log_msg("Calling ActionScript function for onData, processing msg %d\n", i);          (*as_func)(fn_call(&val, fn.this_ptr, env, 1, 0));        } else {          log_error("error in call_method(): method is not a function\n");        }        env->pop();        delete env;#ifndef USE_DMALLOC        //dump_memory_stats(__FUNCTION__, __LINE__, "end");#endif          //log_msg("Deleting message #%d at %p\n", i, messages[i]);        //delete messages[i];        //fn.env->pop();        datain.set_undefined();      }      ptr->obj.processing(false);    } else {      log_error("Couldn't find onData!\n");    }    // Delete this in a batch for now so we can track memory allocation    for (i=0; messages[i] != 0; i++) {      //log_msg("Deleting message #%d at %p\n", i, messages[i]);      delete messages[i];    }  }  //malloc_trim(0);    //result->set(&data);  fn.result->set(true);}voidxmlsocket_event_close(const fn_call& fn){#if 0  as_value* result = fn.result;  as_object_interface* this_ptr = fn.this_ptr;  int nargs = fn.nargs;  int first_arg = fn.first_arg_bottom_index;#else  log_error("%s: unimplemented!\n", __FUNCTION__);#endif}voidxmlsocket_event_connect(const fn_call& fn){  as_value	method;  as_value	val;  tu_string     data;  static bool first = true;     // This event handler should only be executed once.  if (!first) {    fn.result->set(true);    return;  }    xmlsocket_as_object*	ptr = (xmlsocket_as_object*) (as_object*) fn.this_ptr;  assert(ptr);  log_msg("%s: connected = %d\n", __FUNCTION__, ptr->obj.connected());  if ((ptr->obj.connected()) && (first)) {    first = false;    //env->set_variable("success", true, 0);    //env->bottom(0) = true;    if (fn.this_ptr->get_member("onConnect", &method)) {      as_c_function_ptr	func = method.to_c_function();      if (func)        {          // It's a C function.  Call it.          //log_msg("Calling C function for onConnect\n");          (*func)(fn_call(&val, fn.this_ptr, fn.env, 0, 0));      }      else if (as_as_function* as_func = method.to_as_function())        {          // It's an ActionScript function.  Call it.          //log_msg("Calling ActionScript function for onConnect\n");          (*as_func)(fn_call(&val, fn.this_ptr, fn.env, 0, 0));        }      else        {          log_error("error in call_method(): method is not a function\n");        }        } else {      log_msg("FIXME: Couldn't find onConnect!\n");    }  }  fn.result->set(&val); }voidxmlsocket_event_xml(const fn_call& fn){#if 0  as_value* result = fn.result;  as_object_interface* this_ptr = fn.this_ptr;  int nargs = fn.nargs;  int first_arg = fn.first_arg_bottom_index;#else  log_error("%s: unimplemented!\n", __FUNCTION__);#endif  }static XMLSocket xs;intcheck_sockets(int x){  //log_msg("%s unimplemented\n", __FUNCTION__);  if (xml_fd == 0) {    return -1;  }    return xs.checkSockets(x);}} // end of gameswf namespace// HAVE_LIBXML#endif

⌨️ 快捷键说明

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