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

我要开发同款
匿名用户2017年09月28日
74阅读
开发技术C/C++
所属分类开发工具、C/C++开发工具
授权协议GPL

作品详情

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

平台

WindowsXPorlater

OSX10.8orlater

Linux2.6.23orlater

构建

gitclone--recursive https://github.com/cloudpeak/tin.git

mkdirbuild

cdbuild

VisualStudio2015Win64

cmake-G"VisualStudio142015Win64"../tin-DCMAKE_BUILD_TYPE=RELEASE

VisualStudio2015Win32

cmake-G"VisualStudio142015"../tin-DCMAKE_BUILD_TYPE=RELEASE

VisualStudio2008Win32

cmake-G"VisualStudio92008"../tin-DCMAKE_BUILD_TYPE=RELEASE

GCCorClang

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;}
查看全文
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论