Coder Social home page Coder Social logo

gonet's Introduction

gonet

network library based on golang.

Wiki

基于golang标准库封装的网络库,目前只支持tcp模式.

设计**:

提供语义更清晰,更简洁的以下3个抽象模块:
  网络应用层抽象:
      连接器Dialer 和 监听器Listener.

  网络连接层抽象:
      网络连接的封装Conn,提供读写缓存区能力,读写流程在不同goroutine中处理 .

  以及业务层抽象:
	  type Receiver interface {
		OnConnected(s Sender) error //连接建立
		OnMessage(s Sender, b []byte) (n int, err error) //接收消息
		OnClosed(s Sender) error //连接断开前
	  }
	  使用者只需要在创建Dialer或Listener时指定Receiver接口的实例

下面是模块间关系流程图:

flowchart

使用流程:

Dialer(前端):
  启动:
    //newClientConnReceiver为Receiver实例的工厂函数
    d,err := gonet.NewDialer(serverURL, newClientConnReceiver, dialOptions...)
    //省略异常处理
    d.Start() //注意:这里不会阻塞,只会返回连接成功或失败.
              //成功后自动开启断线重连机制,并根据dialOptions控制重试相关参数
    d.Send("xxxx") //发送消息到Server端
  退出:
    d.Shutdown()

Listener(后端):
  启动:
    //newServerConnReceiver为Receiver实例的工厂函数
    l,err := gonet.NewListener(bindURL, newServerConnReceiver)
    //省略异常处理
    l.Serve() //注意:这里会阻塞在socket的Accept循环,为每个新连接启动单独Goroutine处理
  退出:
    l.GracefulStop() //其实目前这个退出并不优雅(不保证所有Accept出的连接网络流缓存都读写完,只是暴力的断开连接),
                     //但保证一旦执行了GracefulStop,则Serve循环一定在其执行完成后才退出

前置环境

golang

支持平台

Linux/Windows

安装

1. git clone https://github.com/xingshuo/gonet.git
2. 
  Windows:
      cd examples\helloworld && .\build.bat
  Linux:
      cd examples/helloworld && sh build.sh

运行

Windows:
   DIR:
       examples\helloworld
   CMD:
      .\server.exe
      .\client.exe (可启多个)
Linux:
   DIR:
       examples/helloworld
   CMD:
      ./server.exe
      ./client.exe (可启多个)

测试

1.Dialer断线重连机制:
    Ctrl + C 关闭Server端,观察Client端断线重连失败消息.
    失败几次后,重新启动Server端,观察断线重连成功消息以及后续前后端心跳包输出
2.退出流程(Dialer Shutdown与Listener GracefulStop)
    前后端皆由Ctrl + C触发,观察输出log是否异常

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.