func startLocalServer() { //连接DB db.Init() //连接Redis redisProxyErr := redisProxy.InitClient(cfg.GetValue("redis_ip"), cfg.GetValue("redis_port")) checkError(redisProxyErr) listener, err := link.Serve("tcp", "0.0.0.0:"+local_port, packet.New( binary.SplitByUint32BE, 1024, 1024, 1024, )) checkError(err) listener.Serve(func(session *link.Session) { session.AddCloseCallback(session, func() { session.Close() }) global.AddSession(session) var msg packet.RAW for { if err := session.Receive(&msg); err != nil { break } module.ReceiveMessage(session, msg) } }) }
//在LoginServer创建虚拟用户 func setSessionOnline(session *link.Session, protoMsg protos.ProtoMsg) { rev_msg := protoMsg.Body.(*systemProto.System_ClientSessionOnlineC2S) userConn := proxys.NewDummyConn(rev_msg.GetSessionID(), rev_msg.GetNetwork(), rev_msg.GetAddr(), transferClient) userSession := link.NewSessionByID(userConn, codecType.DummyCodecType{}, rev_msg.GetSessionID()) userSession.State = make(chan []byte, 100) userSession.AddCloseCallback(userSession, func() { close(userSession.State.(chan []byte)) }) global.AddSession(userSession) //接收游戏消息 go func() { var msg []byte for { if err := userSession.Receive(&msg); err != nil { break } gameProxy.MsgDispatch.Process(userSession, msg) } }() //接收DB消息 go func() { dbMsgChan := userSession.State.(chan []byte) for { data, ok := <-dbMsgChan if !ok { return } dbProxy.ClientDbMsgDispatchHandle.DealMsg(userSession, data) } }() }
func startGateway() { msgDispatch := dispatch.NewDispatch( dispatch.HandleFunc{ H: transferProxy.SendToGameServer, }, ) addr := "0.0.0.0:" + gateway_port err := global.Listener("tcp", addr, global.PackCodecType_UnSafe, func(session *link.Session) { //将此Session记录在缓存内,消息回传时使用 global.AddSession(session) //通知LoginServer用户上线 transferProxy.SetClientSessionOnline(session) //添加session关闭时回调 session.AddCloseCallback(session, func() { //通知LoginServer、GameServer用户下线 transferProxy.SetClientSessionOffline(session.Id()) }) }, msgDispatch, ) checkError(err) }
//在World服务器设置用户登录成功 func setClientLoginSuccess(session *link.Session, protoMsg protos.ProtoMsg) { rev_msg := protoMsg.Body.(*systemProto.System_ClientLoginSuccessC2S) userConn := proxys.NewDummyConn(rev_msg.GetSessionID(), rev_msg.GetNetwork(), rev_msg.GetAddr(), session) userSession := link.NewSessionByID(userConn, codecType.DummyCodecType{}, rev_msg.GetSessionID()) global.AddSession(userSession) go func() { var msg []byte for { if err := userSession.Receive(&msg); err != nil { break } gameProxy.MsgDispatch.Process(userSession, msg) } }() module.User.LoginSuccess(userSession, rev_msg.GetUserName(), rev_msg.GetUserID(), rev_msg.GetGameServerID()) }
func startLocalServer() { //连接Redis redisProxyErr := redisProxy.InitClient(cfg.GetValue("redis_ip"), cfg.GetValue("redis_port"), cfg.GetValue("redis_pwd")) checkError(redisProxyErr) //开启DB db.Init() //开启同步DB数据到数据库 dbProxy.StartSysDB() //开启客户端监听 addr := "0.0.0.0:" + local_port err := global.Listener("tcp", addr, global.PackCodecType_UnSafe, func(session *link.Session) { global.AddSession(session) }, gameProxy.MsgDispatch, ) checkError(err) }