浏览代码

created deamon

marius.rescheleit 5 年之前
父节点
当前提交
356b02fb13
共有 3 个文件被更改,包括 101 次插入0 次删除
  1. 9 0
      daemon/CMakeLists.txt
  2. 11 0
      daemon/ccats.service
  3. 81 0
      daemon/src/main.cpp

+ 9 - 0
daemon/CMakeLists.txt

@@ -0,0 +1,9 @@
+cmake_minimum_required(VERSION 2.8)
+
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
+
+project(ccats)
+
+add_executable(ccats src/main.cpp)
+
+target_link_libraries(ccats PRIVATE -lboost_system)

+ 11 - 0
daemon/ccats.service

@@ -0,0 +1,11 @@
+[Unit]
+Description=CCats
+
+[Service]
+Type=simple
+Restart=always
+RestartSec=1
+ExecStart=/usr/bin/ccats
+
+[Install]
+WantedBy=multi-user.target

+ 81 - 0
daemon/src/main.cpp

@@ -0,0 +1,81 @@
+#include <iostream>
+#include <boost/asio.hpp>
+#include <boost/bind.hpp>
+#include <boost/enable_shared_from_this.hpp>
+
+using namespace boost::asio;
+using ip::tcp;
+using std::cout;
+using std::endl;
+
+
+class con_handler : public boost::enable_shared_from_this<con_handler> {
+private:
+  tcp::socket sock;
+  std::string message="Hello From Server!";
+  enum { max_length = 1024 };
+  char data[max_length];
+public:
+  typedef boost::shared_ptr<con_handler> pointer;
+  con_handler(boost::asio::io_service& io_service): sock(io_service){}
+
+  static pointer create(boost::asio::io_service& io_service) {
+    return pointer(new con_handler(io_service));
+  }
+
+  tcp::socket& socket(){return sock;}
+
+  void start() {
+    sock.async_read_some(
+        boost::asio::buffer(data, max_length),
+        boost::bind(&con_handler::handle_read, shared_from_this(),
+                    boost::asio::placeholders::error,
+                    boost::asio::placeholders::bytes_transferred));
+  
+    sock.async_write_some(
+        boost::asio::buffer(message, max_length),
+        boost::bind(&con_handler::handle_write, shared_from_this(),
+                  boost::asio::placeholders::error,
+                  boost::asio::placeholders::bytes_transferred));
+  }
+
+  void handle_read(const boost::system::error_code& err, size_t bytes_transferred) {
+    if (!err) {cout << data << endl;}
+    else {std::cerr << "error: " << err.message() << std::endl; sock.close();}
+  }
+
+  void handle_write(const boost::system::error_code& err, size_t bytes_transferred) {
+    if (!err) {cout << "Hello World!"<< endl;} 
+    else {std::cerr << "error: " << err.message() << endl; sock.close();}
+  }
+};
+
+
+class Server {
+private:
+   tcp::acceptor acceptor_;
+   void start_accept() {
+     con_handler::pointer connection = con_handler::create(acceptor_.get_io_service());
+     acceptor_.async_accept(connection->socket(), boost::bind(&Server::handle_accept, this, connection,
+        boost::asio::placeholders::error));
+  }
+public:
+  Server(boost::asio::io_service& io_service): acceptor_(io_service, tcp::endpoint(tcp::v4(), 1234)) {
+     start_accept();
+  }
+  void handle_accept(con_handler::pointer connection, const boost::system::error_code& err) {
+    if (!err) {connection->start();}
+    start_accept();
+  }
+};
+
+
+int main(int argc, char *argv[]) {
+  try {
+    boost::asio::io_service io_service;  
+    Server server(io_service);
+    io_service.run();
+  }
+  catch(std::exception& e) {std::cerr << e.what() << endl;}
+  return 0;
+}