Code Monkey home page Code Monkey logo

winsocktut's Introduction

WinsockTut

尝试使用不同的WindowsI/O多路复用模型编写网络测试代码。

这是我在阅读Windows网络编程后的实践项目

Overview

I/O Model Source File Description
select() poller src/SelectPoller.cpp Linux和Windows都支持的API,大多数的跨平台网络库都会采用
WSAAsyncSelect() poller src/AsyncSelectPoller.cpp 网络事件挂接到HWND消息
WSAEventSelect() poller src/AsyncEventPoller.cpp 通过事件对象接收网络事件通知,64限制
Alertable I/O N/A 本代码尚未实现
Overlapped I/O src/OverlappedIOService.cpp 重叠I/O,也是通过事件对象接收网络事件通知,也有64限制,鸡肋
I/O Completion Port src/CompletionPortService.cpp Windows下性能最好的网络I/O多路复用模型

Introduction

所有的网络多路复用根据使用方式分别采用ReactorProactor的方式,做了简单抽象,根据简单的参数应用层示例即可选择不同的模型。

Reactor

select, WSAAsyncSelect和WSAEventSelect采用了Reactor模型,API设计参考了zeromq 如下所示:

class PollerBase 
{
    int AddFd(SOCKET fd, IPollEvent* event);    // 添加一个套接字fd到事件循环
    void RemoveFd(SOCKET fd);                   // 从事件循环中删除一个套接字fd
    void SetPollIn(SOCKET fd);                  // 设置关注套接字的读事件
    void ResetPollIn(SOCKET fd);                // 设置取消关注套接字的读事件
    void SetPollOut(SOCKET fd);                 // 设置关注套接字的写事件
    void ResetPollOut(SOCKET fd);               // 设置取消关注套接字的写事件
    int Poll(int timeout);                      // 事件循环
}

Proactor

I/O Completion Port和Overlapped I/O采用Proactor模型,API设计参考了boost.asio

如下所示:

class IOServiceBase
{
    int AsyncConnect(OverlapContext* ctx, const addrinfo*); // 连接一个addrinfo指定的地址, ctx中指定回调
    int AsyncAccept(OverlapContext* ctx);                   // accpet一个新套接字fd, ctx中指定回调
    int AsyncRead(OverlapContext* ctx);                     // 异步读网络数据, ctx中指定回调
    int AsyncWrite(OverlapContext* ctx);                    // 异步写网络数据, ctx中指定回调
    int Run(int timeout);                                   // 事件循环
}

如何构建

  • 安装Visual Studio
  • 安装CMake
  • 使用CMake生成Visual Studio工程文件
  • 使用Visual Studio打开工程

Example

使用Reactor模型的echo示例

echo示例

使用Proactor模型的ping/pong示例

pingpong示例

测试

tests目录下是用golang实现的echo server和echo client

备注

本代码仅作学习参考或者教学目的

winsocktut's People

Contributors

ki7chen avatar qchencd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

winsocktut's Issues

winsock error

I get "undefined reference to" error while trying to initialize winsock. Could someone help please
winsock

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.