Exemple #1
0
func server() {

	pipe := cellnet.NewEventPipe()

	qpsm := benchmark.NewQPSMeter(pipe, func(qps int) {

		log.Infof("QPS: %d", qps)

	})

	evq := socket.NewAcceptor(pipe).Start(benchmarkAddress)

	socket.RegisterSessionMessage(evq, "gamedef.TestEchoACK", func(content interface{}, ses cellnet.Session) {

		if qpsm.Acc() > benchmarkSeconds {
			signal.Done(1)
			log.Infof("Average QPS: %d", qpsm.Average())
		}

		ses.Send(&gamedef.TestEchoACK{})

	})

	pipe.Start()

}
Exemple #2
0
func runServer() {
	pipe := cellnet.NewEventPipe()

	p := socket.NewAcceptor(pipe).Start("127.0.0.1:7235")

	// 计数器, 应该按照connCount倍数递增
	var counter int

	socket.RegisterSessionMessage(p, coredef.TestEchoACK{}, func(ses cellnet.Session, content interface{}) {
		msg := content.(*coredef.TestEchoACK)

		counter++
		log.Printf("No. %d: server recv: %v", counter, msg.String())

		// 发包后关闭
		ses.Send(&coredef.TestEchoACK{
			Content: proto.String(msg.GetContent()),
		})

		if msg.GetContent() != "noclose" {
			ses.Close()
		}

	})

	pipe.Start()

	done <- 0

}
Exemple #3
0
// 开启前端侦听通道
func StartFrontendAcceptor(pipe cellnet.EventPipe, address string, peerName string) {

	FrontendAcceptor = socket.NewAcceptor(pipe)
	FrontendAcceptor.SetName(peerName)

	// 默认开启并发
	FrontendAcceptor.EnableConcurrenceMode(true)

	// 所有接收到的消息转发到后台
	FrontendAcceptor.InjectData(func(data interface{}) bool {

		if ev, ok := data.(*socket.SessionEvent); ok {

			// Socket各种事件不要往后台发
			switch ev.MsgID {
			case socket.Event_SessionAccepted,
				socket.Event_SessionConnected:
				return true
			}

			// TODO 非法消息直接掐线
			// TODO 心跳, 超时掐线

			// 广播到后台所有服务器
			if relayMethod == RelayMethod_BroardcastToAllBackend || ev.MsgID == socket.Event_SessionClosed {

				BackendAcceptor.IterateSession(func(ses cellnet.Session) bool {

					sendMessageToBackend(ses, ev)

					return true
				})

				// 按照白名单准确投递
			} else if relayMethod == RelayMethod_WhiteList {
				ses := getRelaySession(ev.MsgID)

				if ses != nil {

					sendMessageToBackend(ses, ev)

				} else {

					if DebugMode {
						log.Errorf("client->backend, msg: %s(%d) clientid: %d  relay target not found", getMsgName(ev.MsgID), ev.MsgID, ev.Ses.ID())
					}

				}

			}

		}

		return false
	})

	FrontendAcceptor.Start(address)

}
Exemple #4
0
// 开启客户端侦听通道
func StartClientAcceptor(pipe cellnet.EventPipe, address string) {

	ClientAcceptor = socket.NewAcceptor(pipe)

	// 所有接收到的消息转发到后台
	ClientAcceptor.InjectData(func(data interface{}) bool {

		if ev, ok := data.(*socket.SessionEvent); ok {

			// Socket各种事件不要往后台发
			switch ev.MsgID {
			case socket.Event_SessionAccepted,
				socket.Event_SessionConnected:
				return true
			}

			// 构建路由封包
			relaypkt := cellnet.BuildPacket(&coredef.UpstreamACK{
				MsgID:    proto.Uint32(ev.MsgID),
				Data:     ev.Data,
				ClientID: proto.Int64(ev.Ses.ID()),
			})

			// TODO 按照封包和逻辑固定分发
			// TODO 非法消息直接掐线
			// TODO 心跳, 超时掐线
			BackendAcceptor.IterateSession(func(ses cellnet.Session) bool {

				if DebugMode {
					log.Printf("client->backend, msgid: %d clientid: %d data: %v", ev.MsgID, ev.Ses.ID(), ev.Data)
				}

				ses.RawSend(relaypkt)

				return true
			})

		}

		return false
	})

	ClientAcceptor.Start(address)

}
Exemple #5
0
func server() {

	pipe := cellnet.NewEventPipe()

	evq := socket.NewAcceptor(pipe).Start("127.0.0.1:7201")

	socket.RegisterSessionMessage(evq, "gamedef.TestEchoACK", func(content interface{}, ses cellnet.Session) {
		msg := content.(*gamedef.TestEchoACK)

		log.Debugln("server recv:", msg.String())

		ses.Send(&gamedef.TestEchoACK{
			Content: msg.String(),
		})

	})

	pipe.Start()

}
Exemple #6
0
func server() {

	pipe := cellnet.NewEventPipe()

	evq := socket.NewAcceptor(pipe).Start("127.0.0.1:7234")

	socket.RegisterSessionMessage(evq, coredef.TestEchoACK{}, func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.TestEchoACK)

		log.Println("server recv:", msg.String())

		ses.Send(&coredef.TestEchoACK{
			Content: proto.String(msg.String()),
		})

	})

	pipe.Start()

}
Exemple #7
0
func server() {

	pipe := cellnet.NewEventPipe()

	p := socket.NewAcceptor(pipe).Start("127.0.0.1:7201")
	rpc.InstallServer(p)

	rpc.RegisterMessage(p, coredef.TestEchoACK{}, func(resp rpc.Response, content interface{}) {
		msg := content.(*coredef.TestEchoACK)

		log.Debugln("server recv:", msg.String())

		resp.Feedback(&coredef.TestEchoACK{
			Content: msg.String(),
		})

	})

	pipe.Start()

}
Exemple #8
0
func runServer() {
	pipe := cellnet.NewEventPipe()

	p := socket.NewAcceptor(pipe).Start("127.0.0.1:7201")

	socket.RegisterSessionMessage(p, coredef.TestEchoACK{}, func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.TestEchoACK)

		// 发包后关闭
		ses.Send(&coredef.TestEchoACK{
			Content: proto.String(msg.GetContent()),
		})

		if msg.GetContent() != "noclose" {
			ses.Close()
		}

	})

	pipe.Start()

}
Exemple #9
0
func runServer() {
	pipe := cellnet.NewEventPipe()

	p := socket.NewAcceptor(pipe).Start("127.0.0.1:7201")

	socket.RegisterSessionMessage(p, "gamedef.TestEchoACK", func(content interface{}, ses cellnet.Session) {
		msg := content.(*gamedef.TestEchoACK)

		// 发包后关闭
		ses.Send(&gamedef.TestEchoACK{
			Content: msg.Content,
		})

		if msg.Content != "noclose" {
			ses.Close()
		}

	})

	pipe.Start()

}
Exemple #10
0
func server() {

	pipe := cellnet.NewEventPipe()

	p := socket.NewAcceptor(pipe)
	p.SetName("server")
	p.Start("127.0.0.1:9201")

	rpc.RegisterMessage(p, "gamedef.TestEchoACK", func(content interface{}, resp rpc.Response) {
		msg := content.(*gamedef.TestEchoACK)

		log.Debugln("server recv:", msg.String())

		resp.Feedback(&gamedef.TestEchoACK{
			Content: msg.String(),
		})

	})

	pipe.Start()

}
Exemple #11
0
// 开启后台服务器的侦听通道
func StartBackendAcceptor(pipe cellnet.EventPipe, address string) {

	BackendAcceptor = socket.NewAcceptor(pipe)

	// 关闭客户端连接
	socket.RegisterSessionMessage(BackendAcceptor, coredef.CloseClientACK{}, func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.CloseClientACK)

		if msg.ClientID == nil {

			// 关闭所有客户端
			ClientAcceptor.IterateSession(func(ses cellnet.Session) bool {

				if DebugMode {
					log.Debugf("[gate] backend->client, close clientid %d", msg.GetClientID())
				}
				ses.Close()

				return true
			})

		} else {

			// 关闭指定客户端
			clientSes := ClientAcceptor.GetSession(msg.GetClientID())

			// 找到连接并关闭
			if clientSes != nil {

				if DebugMode {
					log.Debugf("[gate] backend->client, close clientid %d", msg.GetClientID())
				}

				clientSes.Close()
			} else if DebugMode {
				log.Debugf("[gate] backend->client, client not found, close failed, clientid %d", msg.GetClientID())
			}

		}

	})

	// 广播
	socket.RegisterSessionMessage(BackendAcceptor, coredef.DownstreamACK{}, func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.DownstreamACK)

		pkt := &cellnet.Packet{
			MsgID: msg.GetMsgID(),
			Data:  msg.Data,
		}

		if msg.ClientID == nil {

			// 广播给所有客户端
			ClientAcceptor.IterateSession(func(ses cellnet.Session) bool {

				if DebugMode {
					log.Debugf("[gate] backend->client, msgid: %d clientid %d", msg.GetMsgID(), msg.GetClientID())
				}

				ses.RawSend(pkt)

				return true
			})

		} else {

			// 指定客户端发送
			for _, clientid := range msg.ClientID {
				clientSes := ClientAcceptor.GetSession(clientid)

				if clientSes != nil {

					if DebugMode {
						log.Debugf("[gate] backend->client, msgid: %d clientid %d", msg.GetMsgID(), msg.GetClientID())
					}

					clientSes.RawSend(pkt)

				} else if DebugMode {

					log.Debugf("[gate] backend->client, client not found, msgid: %d clientid %d", msg.GetMsgID(), msg.GetClientID())
				}
			}
		}

	})

	BackendAcceptor.Start(address)
}
Exemple #12
0
// 开启后台服务器的侦听通道
func StartBackendAcceptor(pipe cellnet.EventPipe, address string, peerName string) {

	BackendAcceptor = socket.NewAcceptor(pipe)
	BackendAcceptor.SetName(peerName)

	// 默认开启并发
	BackendAcceptor.EnableConcurrenceMode(true)

	// 收到后端服务器发来的注册, 标示连接
	socket.RegisterSessionMessage(BackendAcceptor, "coredef.RegisterRouterBackendACK", func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.RegisterRouterBackendACK)

		registerBackend(ses, msg.Name)

	})

	// 断开连接时, 刷新路由
	socket.RegisterSessionMessage(BackendAcceptor, "coredef.SessionClosed", func(content interface{}, ses cellnet.Session) {

		closeBackend(ses)

	})

	// 关闭客户端连接
	socket.RegisterSessionMessage(BackendAcceptor, "coredef.CloseClientACK", func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.CloseClientACK)

		if msg.ClientID == 0 {

			// 关闭所有客户端
			FrontendAcceptor.IterateSession(func(ses cellnet.Session) bool {

				if DebugMode {
					log.Debugf("backend->client, close clientid %d", msg.ClientID)
				}
				ses.Close()

				return true
			})

		} else {

			// 关闭指定客户端
			clientSes := FrontendAcceptor.GetSession(msg.ClientID)

			// 找到连接并关闭
			if clientSes != nil {

				if DebugMode {
					log.Debugf("backend->client, close clientid %d", msg.ClientID)
				}

				clientSes.Close()
			} else if DebugMode {
				log.Debugf("backend->client, client not found, close failed, clientid %d", msg.ClientID)
			}

		}

	})

	// 广播
	socket.RegisterSessionMessage(BackendAcceptor, "coredef.DownstreamACK", func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.DownstreamACK)

		pkt := &cellnet.Packet{
			MsgID: msg.MsgID,
			Data:  msg.Data,
		}

		if len(msg.ClientID) == 0 {

			// 广播给所有客户端
			FrontendAcceptor.IterateSession(func(ses cellnet.Session) bool {

				if DebugMode {
					log.Debugf("backend->client, msgid: %d clientid %d", msg.MsgID, msg.ClientID)
				}

				ses.RawSend(pkt)

				return true
			})

		} else {

			// 指定客户端发送
			for _, clientid := range msg.ClientID {
				clientSes := FrontendAcceptor.GetSession(clientid)

				if clientSes != nil {

					if DebugMode {
						log.Debugf("backend->client, msg: %s(%d) clientid: %d", getMsgName(msg.MsgID), msg.MsgID, msg.ClientID)
					}

					clientSes.RawSend(pkt)

				} else if DebugMode {

					log.Debugf("backend->client, client not found, msg: %s(%d) clientid: %d", getMsgName(msg.MsgID), msg.MsgID, msg.ClientID)
				}
			}
		}

	})

	BackendAcceptor.Start(address)
}