apeskov commented on a change in pull request #7876:
URL: https://github.com/apache/tvm/pull/7876#discussion_r651293840



##########
File path: apps/ios_rpc/tvmrpc/rpc_server.h
##########
@@ -0,0 +1,318 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file rpc_server.h
+ * \brief RPC Server implementation.
+ */
+#ifndef TVM_APPS_IOS_RPC_SERVER_H_
+#define TVM_APPS_IOS_RPC_SERVER_H_
+
+#include <string>
+#include <future>
+#include <chrono>
+#include <dirent.h>
+
+#include "tvm/runtime/c_runtime_api.h"
+#include "runtime/rpc/rpc_endpoint.h"
+#include "runtime/rpc/rpc_socket_impl.h"
+#include "support/socket.h"
+#include "rpc_tracker_client.h"
+
+namespace tvm {
+namespace runtime {
+
+std::vector<std::string> ListDir(const std::string& dirname) {
+  std::vector<std::string> vec;
+  DIR* dp = opendir(dirname.c_str());
+  if (dp == nullptr) {
+    int errsv = errno;
+    LOG(FATAL) << "ListDir " << dirname << " error: " << strerror(errsv);
+  }
+  dirent* d;
+  while ((d = readdir(dp)) != nullptr) {
+    std::string filename = d->d_name;
+    if (filename != "." && filename != "..") {
+      std::string f = dirname;
+      if (f[f.length() - 1] != '/') {
+        f += '/';
+      }
+      f += d->d_name;
+      vec.push_back(f);
+    }
+  }
+  closedir(dp);
+  return vec;
+}
+
+/*!
+ * \brief CleanDir Removes the files from the directory
+ * \param dirname The name of the directory
+ */
+void CleanDir(const std::string& dirname) {
+  auto files = ListDir(dirname);
+  for (const auto& filename : files) {
+    std::string file_path = dirname + "/";
+    file_path += filename;
+    const int ret = std::remove(filename.c_str());
+    if (ret != 0) {
+      LOG(WARNING) << "Remove file " << filename << " failed";
+    }
+  }
+}
+
+// Runtime environment
+struct RPCEnv {
+ public:
+  RPCEnv(const std::string &base):base_(base) {}
+  // Get Path.
+  std::string GetPath(const std::string& file_name) { return base_ + 
file_name; }
+
+  void CleanUp() const {
+    CleanDir(base_);
+  }
+ private:
+  std::string base_;
+};
+
+
+/*!
+ * \brief RPCServer RPC Server class.
+ * \param host The hostname of the server, Default=0.0.0.0
+ * \param port The port of the RPC, Default=9090
+ * \param port_end The end search port of the RPC, Default=9099
+ * \param tracker The address of RPC tracker in host:port format e.g. 
10.77.1.234:9190 Default=""
+ * \param key The key used to identify the device type in tracker. Default=""
+ * \param custom_addr Custom IP Address to Report to RPC Tracker. Default=""
+ */
+class RPCServer {
+ public:
+  /*!
+   * \brief Constructor.
+   */
+  RPCServer(std::string host, int port, int port_end, std::string 
tracker_addr, std::string key,
+            std::string custom_addr, std::string work_dir)
+      : host_(std::move(host)),
+        port_(port),
+        my_port_(0),
+        port_end_(port_end),
+        tracker_addr_(std::move(tracker_addr)),
+        key_(std::move(key)),
+        custom_addr_(std::move(custom_addr)),
+        work_dir_(std::move(work_dir)),
+        tracker_(tracker_addr_, key_, custom_addr_) {}
+
+  /*!
+   * \brief Destructor.
+   */
+  ~RPCServer() {
+    try {
+      // Free the resources
+      listen_sock_.Close();
+      tracker_.Close();
+    } catch (...) {
+    }
+  }
+
+  /*!
+   * \brief Start Creates the RPC listen process and execution.
+   */
+  void Start() {
+    listen_sock_.Create();
+    my_port_ = listen_sock_.TryBindHost(host_, port_, port_end_);
+    LOG(INFO) << "bind to " << host_ << ":" << my_port_;
+    listen_sock_.Listen(1);
+    continue_processing = true;
+    proc_ = std::future<void>(std::async(std::launch::async, 
&RPCServer::ListenLoopProc, this));
+  }
+  
+  void Stop() {
+    continue_processing = false;
+    tracker_.Close();
+  }
+    
+  void setCompletionCallbacks(std::function<void()> callback_start, 
std::function<void()> callback_stop) {
+    completion_callback_start_ = callback_start;
+    completion_callback_stop_ = callback_stop;
+  }
+
+ private:
+  /*!
+   * \brief ListenLoopProc The listen process.
+   */
+  void ListenLoopProc() {
+    
+    while (continue_processing) {
+      support::TCPSocket conn;
+      support::SockAddr addr("0.0.0.0", 0);
+      std::string opts;
+      try {
+        // step 1: setup tracker and report to tracker
+        tracker_.TryConnect();
+        if (completion_callback_start_)
+          completion_callback_start_();
+        // step 2: wait for in-coming connections
+        AcceptConnection(&tracker_, &conn, &addr, &opts);
+      } catch (const char* msg) {
+        LOG(WARNING) << "Socket exception: " << msg;
+        // close tracker resource
+        tracker_.Close();
+        continue;
+      } catch (const std::exception& e) {
+        // close tracker resource
+        tracker_.Close();
+        LOG(WARNING) << "Exception standard: " << e.what();

Review comment:
       It was a copy-paste form neighbour "cpp_rpc" application. At last 
version of this patch I decided to rewrite this part of code in nonblocking 
style with Obj-C streams, RunLoop and other stuff. There is no more this 
exception line in iOS RPC project.   
   
   
https://github.com/apache/tvm/blob/1c251f50ee616507bdfd8866408e7acf9888cc3f/apps/cpp_rpc/rpc_server.cc#L160




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to