完整实现的 C++ 版 Go 语言运行时 tin-project

码农软件 · 软件分类 · C/C++开发工具 · 2019-11-09 19:27:31

软件介绍

tin 项目是完整实现的 C++ 版 go 语言运行时。它参考 go 语言运行时,将 go 语言运行时用 C++ 重写了一遍, 让你可以在 C++ 中使用 go 的风格写程序。

平台

  • Windows XP or later

  • OS X 10.8 or later

  • Linux 2.6.23 or later

构建

  • git clone --recursive https://github.com/cloudpeak/tin.git

  • mkdir build

  • cd build

  • Visual Studio 2015 Win64

    • cmake -G "Visual Studio 14 2015 Win64" ../tin -DCMAKE_BUILD_TYPE=RELEASE

  • Visual Studio 2015 Win32

    • cmake -G "Visual Studio 14 2015" ../tin -DCMAKE_BUILD_TYPE=RELEASE

  • Visual Studio 2008 Win32

    • cmake -G "Visual Studio 9 2008" ../tin -DCMAKE_BUILD_TYPE=RELEASE

  • GCC or Clang

    • cmake ../tin -DCMAKE_BUILD_TYPE=RELEASE && make

示例

#include "tin/all.h"

void HandleClient(tin::net::TcpConn conn) {
  // Set TCP Read Write buffer.
  conn->SetReadBuffer(64 * 1024);
  conn->SetWriteBuffer(64 * 1024);

  // user space buffer size.
  const int kIOBufferSize = 4 * 1024;
  scoped_ptr<char[]> buf(new char[kIOBufferSize]);

  // set read, write deadline.
  const int64 kRWDeadline = 20 * tin::kSecond;
  conn->SetDeadline(kRWDeadline);
  while (true) {
    int n = conn->Read(buf.get(), kIOBufferSize);
    int err = tin::GetErrorCode();
    if (n > 0) {
      conn->SetReadDeadline(kRWDeadline);
    }
    if (err != 0) {
      VLOG(1) << "Read failed due to: " << tin::GetErrorStr();
      // FIN received, graceful close, we can still send.
      if (err == TIN_EOF) {
        if (n > 0) {
          conn->Write(buf.get(), n);
        }
        conn->CloseWrite();
        // delay a while to avoid RST.
        tin::NanoSleep(500 * tin::kMillisecond);
      }
      break;
    }
    DCHECK_GT(n, 0);
    conn->Write(buf.get(), n);
    if (tin::GetErrorCode() != 0) {
      VLOG(1) << "Write failed due to " << tin::GetErrorStr();
      break;
    }
    conn->SetWriteDeadline(kRWDeadline);
  }
  conn->Close();
}

int TinMain(int argc, char** argv) {
  const uint16 kPort = 2222;
  bool use_ipv6 = false;
  tin::net::TCPListener listener =
    tin::net::ListenTcp(use_ipv6 ? "0:0:0:0:0:0:0:0" : "0.0.0.0", kPort);
  if (tin::GetErrorCode() != 0) {
    LOG(FATAL) << "Listen failed due to " << tin::GetErrorStr();
  }
  LOG(INFO) << "echo server is listening on port: " << kPort;
  while (true) {
    tin::net::TcpConn conn = listener->Accept();
    if (tin::GetErrorCode() == 0) {
      tin::Spawn(&HandleClient, conn);
    } else {
      LOG(INFO) << "Accept failed due to " << tin::GetErrorStr();
    }
  }
  return 0;
}

int main(int argc, char** argv) {
  tin::Initialize();

  // set logging level.
  logging::SetMinLogLevel(logging::LOG_INFO);

  // set max p count.
  tin::Config config = tin::DefaultConfig();
  config.SetMaxProcs(base::SysInfo::NumberOfProcessors());

  // start the world.
  tin::PowerOn(TinMain, argc, argv, &config);

  // wait for power off
  tin::WaitForPowerOff();

  // cleanup.
  tin::Deinitialize();

  return 0;
}

本文地址:https://codercto.com/soft/d/18634.html

掘金大数据

掘金大数据

程新洲、朱常波、晁昆 / 机械工业出版社 / 2019-1 / 59.00元

在数据横向融合的时代,充分挖掘数据金矿及盘活数据资产,是企业发展和转型的关键所在。电信运营商以其数据特殊性,必将成为大数据领域的领航者、生力军。各行业的大数据从业者要如何从电信业的大数据中挖掘价值呢? 本书彻底揭开电信运营商数据的神秘面纱,系统介绍了大数据的发展历程,主要的数据挖掘方法,电信运营商在网络运行及业务运营方面的数据资源特征,基于用户、业务、网络、终端及内在联系的电信运营商大数据分......一起来看看 《掘金大数据》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具