GithubHelp home page GithubHelp logo

rhino's Introduction

rhino

go网络基础框架

使用领域

rhino励志打造一个可靠的游戏服务器框架,其核心内容在于

· actor 主要使用在应用层的逻辑部分(actor内部是单线程)例如:日志服务,db服务,游戏服务,网关等等

· network 用于tcp,http,udp的通信,封装读写包

· 其他部分是封装了一些重复劳动的工具

· rpc模块,目前还没有,以后会加入,考虑一个高可用的方案

actor模型

1 核心api在actor中,其主要actor处理逻辑部分在于process

2 process包括邮件(channel)和远程(retome)他们都提供对actor的支持

3 所以一切皆actor

network

net.Conn有个问题需要说下,就是SetReadDeadline/SetWriteDeadline这两个是读写超时的绝对时间

设置读写会超时,那么就需要处理read和write返回值n,查看源码发现net.Conn中write其实是一个write full,所以一般情况不需要设置SetWriteDeadline。

network.stream使用len+body的读取方法,一般处理read/write在单线程比较安全可靠,借助actor模型 由上级actor对其write,来更好的解决和应对分布式中环路堵塞和超时的问题

编写一个简单的服务器

func main(){
  network.StartTcpServer(":8088", network.OptionHandler(func(conn net.Conn) (err error) {
		Stage().ActorOf(WithRemoteStream(func(ctx ActorContext) {
			switch body := ctx.Any().(type) {
			case *Started:
				fmt.Println("connect addr:", conn.RemoteAddr().String())
			case *Stopped:
			case []byte:
				fmt.Println("message: ", network.ReadBegin(body))
				ctx.Send(ctx.Self(), network.WriteBegin(0x102).Flush())
			case error: //设置了心跳会被通知
			default:
				fmt.Printf("untreated type %T \n", body)
			}
		}, conn))
		return
	}))

	cli := Stage().ActorOf(WithRemoteAddr(func(ctx ActorContext) {
		switch body := ctx.Any().(type) {
		case *Started:
			fmt.Println("open ok")
		case *Stopped:
		case []byte:
			fmt.Println("cli respond: ", network.ReadBegin(body))
		case Failure: //最终错误退出的原因

		default:
			fmt.Printf("other object miss handle %T \n", body)
		}
	}, "localhost:8088"))

	psend := network.WriteBegin(0x101, "who's your daddy!")
	cli.Tell(psend.Flush())
}

(QQ技术群:75205017)

75205017.jpg

欢迎服务器开发者和爱好者入群交流!阿里,百度,腾讯大牛在线分享经验。

rhino's People

Contributors

okpub avatar mikefortouch 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.