当前位置: 首页>>代码示例>>C++>>正文


C++ DeviceList::clear方法代码示例

本文整理汇总了C++中DeviceList::clear方法的典型用法代码示例。如果您正苦于以下问题:C++ DeviceList::clear方法的具体用法?C++ DeviceList::clear怎么用?C++ DeviceList::clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DeviceList的用法示例。


在下文中一共展示了DeviceList::clear方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: discover

int DeviceDiscoverer::discover(
   DeviceList& devices, const char* searchTarget, uint32_t timeout, int count)
{
   int rval = -1;

   // prepare device list
   devices->setType(Array);
   devices->clear();

   // create SSDP request
   HttpRequestHeader requestHeader;
   createRequest(searchTarget, &requestHeader);

   // create socket for sending request
   DatagramSocket socket;

   // bind to any available port
   InternetAddressRef localAddr = new InternetAddress("0.0.0.0", 0);
   if(socket.bind(&(*localAddr)))
   {
      // create the group address
      InternetAddressRef groupAddr = new InternetAddress(
         SSDP_MULTICAST_ADDRESS, SSDP_MULTICAST_PORT);

      // create and send discover request datagram
      DatagramRef request = new Datagram(groupAddr);
      request->assignString(requestHeader.toString().c_str());
      MO_CAT_DEBUG(MO_UPNP_CAT, "Sending UPnP request:\n%s",
         requestHeader.toString().c_str());
      if(socket.send(request))
      {
         // no devices yet
         rval = 0;

         // use timer to comply with user-supplied timeout
         Timer timer;
         timer.start();
         uint32_t remaining = timeout;
         InternetAddressRef addr = new InternetAddress();
         while(rval >= 0 && remaining > 0 && (count == 0 || rval < count))
         {
            // set receive timeout and try to get ssdp responses
            socket.setReceiveTimeout(remaining);

            DatagramRef response = new Datagram(addr);
            response->getBuffer()->resize(2048);
            if(!socket.receive(response))
            {
               // check last exception
               ExceptionRef e = Exception::get();
               if(e->isType("monarch.net.SocketTimeout"))
               {
                  MO_CAT_DEBUG(MO_UPNP_CAT, "UPnP request timed out.");

                  // exception indicates timed out
                  remaining = 0;
               }
               else
               {
                  MO_CAT_ERROR(MO_UPNP_CAT,
                     "UPnP request error: %s",
                     JsonWriter::writeToString(
                        Exception::getAsDynamicObject()).c_str());

                  // some error other than a timeout
                  rval = -1;
               }
            }
            else
            {
               // parse ssdp response
               MO_CAT_DEBUG(MO_UPNP_CAT, "Received UPnP response:\n%s",
                  response->getString().c_str());
               Device device = parseDevice(response->getString().c_str());
               if(device.isNull())
               {
                  MO_CAT_ERROR(MO_UPNP_CAT,
                     "UPnP response parse error: %s",
                     JsonWriter::writeToString(
                        Exception::getAsDynamicObject()).c_str());

                  // error in parsing
                  rval = -1;
               }
               else
               {
                  MO_CAT_DEBUG(MO_UPNP_CAT,
                     "Found UPnP device: %s",
                     JsonWriter::writeToString(device).c_str());

                  // another device found
                  ++rval;
                  devices->append(device);

                  // update remaining time (must be within 32-bit integer range)
                  remaining = (uint32_t)timer.getRemainingMilliseconds(timeout);
               }
            }
         }
      }
//.........这里部分代码省略.........
开发者ID:bsletten,项目名称:monarch,代码行数:101,代码来源:DeviceDiscoverer.cpp


注:本文中的DeviceList::clear方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。