GithubHelp home page GithubHelp logo

xsjames / yasio Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yasio/yasio

0.0 0.0 0.0 32.97 MB

A multi-platform support c++11 library with focus on asio (asynchronous socket I/O) for any client application, support win32 & linux & apple & android & wp8 & wp8.1-universal & win10-universal.

License: Other

C++ 99.10% C 0.09% Batchfile 0.04% Makefile 0.07% CMake 0.55% Shell 0.14%

yasio's Introduction

yasio - Yet Another Socket IO service

Build Status Windows Build Status Release 996.icu LICENSE GitHub stars GitHub forks Language grade: C/C++ Total alerts

English

yasio 是一个轻量级跨平台的异步socket库,专注于客户端和基于各种游戏引擎的游戏客户端网络服务, 支持windows & linux & apple & android & win10-universal。

应用案例

  • 红警OL手游项目: 用于客户端网络传输,并且随着该项目于2018年10月17日由腾讯游戏发行正式上线后稳定运行于上百万移动设备上。
  • x-studio软件项目: 用于实现局域网UDP+TCP发现更新机制。
  • xlua集成: 将yasio集成到xlua, 使基于xlua的unity3d可以直接使用yasio的lua绑定接口。

用法

C++

#include "yasio/yasio.hpp"
#include "yasio/obstream.hpp"
using namespace yasio;
using namespace yasio::inet;

int main()
{
  io_service service({"www.ip138.com", 80});
  service.set_option(YOPT_S_DEFERRED_EVENT, 0); // 直接在网络线程分派网络事件
  service.start_service([&](event_ptr&& ev) {
    switch (ev->kind())
    {
      case YEK_PACKET: {
        auto packet = std::move(ev->packet());
        fwrite(packet.data(), packet.size(), 1, stdout);
        fflush(stdout);
        break;
      }
      case YEK_CONNECT_RESPONSE:
        if (ev->status() == 0)
        {
          auto transport = ev->transport();
          if (ev->cindex() == 0)
          {
            obstream obs;
            obs.write_bytes("GET /index.htm HTTP/1.1\r\n");

            obs.write_bytes("Host: www.ip138.com\r\n");

            obs.write_bytes("User-Agent: Mozilla/5.0 (Windows NT 10.0; "
                            "WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
                            "Chrome/79.0.3945.117 Safari/537.36\r\n");
            obs.write_bytes("Accept: */*;q=0.8\r\n");
            obs.write_bytes("Connection: Close\r\n\r\n");

            service.write(transport, std::move(obs.buffer()));
          }
        }
        break;
      case YEK_CONNECTION_LOST:
        printf("The connection is lost.\n");
        break;
    }
  });
  // open channel 0 as tcp client
  service.open(0, YCM_TCP_CLIENT);
  getchar();
}

Lua

local ip138 = "www.ip138.com"
local service = yasio.io_service.new({host=ip138, port=80})
local respdata = ""
-- 传入网络事件处理函数启动网络服务线程,网络事件有: 消息包,连接响应,连接丢失
service.start_service(function(ev)
        local k = ev.kind()
        if (k == yasio.YEK_PACKET) then
            respdata = respdata .. ev:packet():to_string()
        elseif k == yasio.YEK_CONNECT_RESPONSE then
            if ev:status() == 0 then -- status为0表示连接建立成功
                local transport = ev:transport()
                local obs = yasio.obstream.new()
                obs.write_bytes("GET / HTTP/1.1\r\n")

                obs.write_bytes("Host: " .. ip138 .. "\r\n")

                obs.write_bytes("User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36\r\n")
                obs.write_bytes("Accept: */*;q=0.8\r\n")
                obs.write_bytes("Connection: Close\r\n\r\n")

                service.write(transport, obs)
            end
        elseif k == yasio.YEK_CONNECTION_LOST then
            print("request finish, respdata: " ..  respdata)
        end
    end)
-- 将信道0作为TCP客户端打开,并向服务器发起异步连接,进行TCP三次握手
service.open(0, yasio.YCM_TCP_CLIENT)

-- 由于lua_State和渲染对象,不支持在其他线程操作,因此分派网络事件封装为全局Lua函数,并且以下函数应该在主线程或者游戏引擎渲染线程调用
function gDispatchNetworkEvent(...)
    service.dispatch(128) -- 每帧最多处理128个网络事件
end

_G.yservice = service -- Store service to global table as a singleton instance

使用g++快速运行tcptest测试程序

g++ tests/tcp/main.cpp --std=c++11 -DYASIO_HEADER_ONLY -lpthread -I./ -o tcptest && ./tcptest

使用CMake编译yasio的测试程序和示例程序

git clone https://github.com/simdsoft/yasio
cd yasio
git submodule update --init --recursive
cd build
cmake ..

# 使用CMake命令行编译
# 或者直接用VS打开 yasio.sln 解决方案
cmake --build . --config Debug

特性:

  • 支持IPv6_only网络。
  • 支持处理多个连接的所有网络事件。
  • 支持计时器。
  • 支持TCP粘包处理,业务完全不必关心。
  • 支持Lua绑定。
  • 支持Cocos2d-x jsb绑定。
  • 支持CocosCreator jsb2.0绑定。
  • 支持Unity3D C#绑定。
  • 支持组播。
  • 支持SSL客户端,基于OpenSSL。
  • 支持非阻塞域名解析,基于c-ares。

yasio核心设计,充分利用了多路io复用模型(服务器高并发的基石),以下是框架图

image

集成指南

https://github.com/simdsoft/yasio/wiki/Integrate-Guides

更多详细用法,请查看 文档

yasio's People

Contributors

anssser avatar binesiyu avatar gooderman avatar halx99 avatar huizhen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.