ti项目是完整实现的C++版go语言运行时。它参考go语言运行时,将go语言运行时用C++重写了一遍,让你可以在C++中使用go的风格写程序。
平台
WidowsXPorlater
OSX10.8orlater
Liux2.6.23orlater
构建
gitcloe--recursive https://github.com/cloudpeak/ti.git
mkdirbuild
cdbuild
VisualStudio2015Wi64
cmake-G"VisualStudio142015Wi64"../ti-DCMAKE_BUILD_TYPE=RELEASE
VisualStudio2015Wi32
cmake-G"VisualStudio142015"../ti-DCMAKE_BUILD_TYPE=RELEASE
VisualStudio2008Wi32
cmake-G"VisualStudio92008"../ti-DCMAKE_BUILD_TYPE=RELEASE
GCCorClag
cmake../ti-DCMAKE_BUILD_TYPE=RELEASE&&make
示例
#iclude "ti/all.h"void HadleCliet(ti::et::TcpCo co) { // Set TCP Read Write buffer. co->SetReadBuffer(64 * 1024); co->SetWriteBuffer(64 * 1024); // user space buffer size. cost it kIOBufferSize = 4 * 1024; scoped_ptr<char[]> buf(ew char[kIOBufferSize]); // set read, write deadlie. cost it64 kRWDeadlie = 20 * ti::kSecod; co->SetDeadlie(kRWDeadlie); while (true) { it = co->Read(buf.get(), kIOBufferSize); it err = ti::GetErrorCode(); if ( > 0) { co->SetReadDeadlie(kRWDeadlie); } if (err != 0) { VLOG(1) << "Read failed due to: " << ti::GetErrorStr(); // FIN received, graceful close, we ca still sed. if (err == TIN_EOF) { if ( > 0) { co->Write(buf.get(), ); } co->CloseWrite(); // delay a while to avoid RST. ti::NaoSleep(500 * ti::kMillisecod); } break; } DCHECK_GT(, 0); co->Write(buf.get(), ); if (ti::GetErrorCode() != 0) { VLOG(1) << "Write failed due to " << ti::GetErrorStr(); break; } co->SetWriteDeadlie(kRWDeadlie); } co->Close();}it TiMai(it argc, char** argv) { cost uit16 kPort = 2222; bool use_ipv6 = false; ti::et::TCPListeer listeer = ti::et::ListeTcp(use_ipv6 ? "0:0:0:0:0:0:0:0" : "0.0.0.0", kPort); if (ti::GetErrorCode() != 0) { LOG(FATAL) << "Liste failed due to " << ti::GetErrorStr(); } LOG(INFO) << "echo server is listeig o port: " << kPort; while (true) { ti::et::TcpCo co = listeer->Accept(); if (ti::GetErrorCode() == 0) { ti::Spaw(&HadleCliet, co); } else { LOG(INFO) << "Accept failed due to " << ti::GetErrorStr(); } } retur 0;}it mai(it argc, char** argv) { ti::Iitialize(); // set loggig level. loggig::SetMiLogLevel(loggig::LOG_INFO); // set max p cout. ti::Cofig cofig = ti::DefaultCofig(); cofig.SetMaxProcs(base::SysIfo::NumberOfProcessors()); // start the world. ti::PowerO(TiMai, argc, argv, &cofig); // wait for power off ti::WaitForPowerOff(); // cleaup. ti::Deiitialize(); retur 0;}
评论