• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang packet.Packet类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/LilyPad/GoLilyPad/packet.Packet的典型用法代码示例。如果您正苦于以下问题:Golang Packet类的具体用法?Golang Packet怎么用?Golang Packet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Packet类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: HandlePacket

func (this *ConnectImpl) HandlePacket(packet packet.Packet) (err error) {
	switch packet.Id() {
	case connect.PACKET_KEEPALIVE:
		err = this.Write(packet)
	case connect.PACKET_RESULT:
		packetResult := packet.(*connect.PacketResult)
		err = this.DispatchResult(packetResult.SequenceId, packetResult.StatusCode, packetResult.Result)
	case connect.PACKET_MESSAGE_EVENT:
		this.DispatchEvent("message", WrapEventMessage(packet.(*connect.PacketMessageEvent)))
	case connect.PACKET_REDIRECT_EVENT:
		this.DispatchEvent("redirect", WrapEventRedirect(packet.(*connect.PacketRedirectEvent)))
	case connect.PACKET_SERVER_EVENT:
		this.DispatchEvent("server", WrapEventServer(packet.(*connect.PacketServerEvent)))
	}
	return
}
开发者ID:LivousCraftNetwork,项目名称:GoLilyPad,代码行数:16,代码来源:connectImpl.go


示例2: HandlePacket

func (this *Session) HandlePacket(packet packet.Packet) (err error) {
	switch this.state {
	case STATE_DISCONNECTED:
		if packet.Id() == minecraft.PACKET_SERVER_HANDSHAKE {
			handshakePacket := packet.(*minecraft.PacketServerHandshake)
			this.protocolVersion = handshakePacket.ProtocolVersion
			this.serverAddress = handshakePacket.ServerAddress
			supportedVersion := false
			for _, version := range minecraft.Versions {
				if version != this.protocolVersion {
					continue
				}
				supportedVersion = true
				break
			}
			if handshakePacket.State == 1 {
				if !supportedVersion {
					this.protocolVersion = minecraft.Versions[0]
				}
				this.pipeline.Replace("registry", minecraft.StatusPacketServerCodec)
				this.state = STATE_STATUS
			} else if handshakePacket.State == 2 {
				if !supportedVersion {
					err = errors.New("Protocol version does not match")
					return
				}
				this.protocol17 = this.protocolVersion < minecraft.Versions[0]
				if this.protocol17 {
					this.pipeline.Replace("registry", minecraft.LoginPacketServerCodec17)
				} else {
					this.pipeline.Replace("registry", minecraft.LoginPacketServerCodec)
				}
				this.state = STATE_LOGIN
			} else {
				err = errors.New("Unexpected state")
				return
			}
		} else {
			err = errors.New("Unexpected packet")
			return
		}
	case STATE_STATUS:
		if packet.Id() == minecraft.PACKET_SERVER_STATUS_REQUEST {
			samplePath := this.server.router.RouteSample(this.serverAddress)
			sampleTxt, sampleErr := ioutil.ReadFile(samplePath)
			sample := make([]map[string]interface{}, 0)
			if sampleErr == nil {
				lines := strings.Split(string(sampleTxt), "\n")
				for _, line := range lines {
					line = strings.Replace(line, "\r", "", -1)
					if len(strings.TrimSpace(line)) == 0 {
						continue
					}
					entry := make(map[string]interface{})
					entry["name"] = minecraft.Colorize(line)
					entry["id"] = "00000000-0000-0000-0000-000000000000"
					sample = append(sample, entry)
				}
			}
			icons := this.server.router.RouteIcons(this.serverAddress)
			iconPath := icons[RandomInt(len(icons))]
			icon, iconErr := ioutil.ReadFile(iconPath)
			var iconString string
			if iconErr == nil {
				iconString = "data:image/png;base64," + base64.StdEncoding.EncodeToString(icon)
			}
			version := make(map[string]interface{})
			version["name"] = minecraft.STRING_VERSION
			version["protocol"] = this.protocolVersion
			players := make(map[string]interface{})
			if this.server.SyncMaxPlayers() {
				players["max"] = this.server.connect.MaxPlayers()
			} else {
				players["max"] = this.server.MaxPlayers()
			}
			players["online"] = this.server.connect.Players()
			players["sample"] = sample
			description := make(map[string]interface{})
			motds := this.server.router.RouteMotds(this.serverAddress)
			motd := motds[RandomInt(len(motds))]
			description["text"] = minecraft.Colorize(motd)
			response := make(map[string]interface{})
			response["version"] = version
			response["players"] = players
			response["description"] = description
			if iconString != "" {
				response["favicon"] = iconString
			}
			var marshalled []byte
			marshalled, err = json.Marshal(response)
			if err != nil {
				return
			}
			err = this.Write(minecraft.NewPacketClientStatusResponse(string(marshalled)))
			if err != nil {
				return
			}
			this.state = STATE_STATUS_PING
		} else {
			err = errors.New("Unexpected packet")
//.........这里部分代码省略.........
开发者ID:LivousCraftNetwork,项目名称:GoLilyPad,代码行数:101,代码来源:session.go


示例3: HandlePacket

func (this *Session) HandlePacket(packet packet.Packet) (err error) {
	switch packet.Id() {
	case connect.PACKET_KEEPALIVE:
		if this.keepalive == nil {
			this.Close()
			return
		}
		if *this.keepalive != packet.(*connect.PacketKeepalive).Random {
			this.Close()
			return
		}
		this.keepalive = nil
	case connect.PACKET_REQUEST:
		request := packet.(*connect.PacketRequest).Request
		var result connect.Result
		var statusCode uint8
		switch request.Id() {
		case connect.REQUEST_AUTHENTICATE:
			if !this.Authorized() {
				username := request.(*connect.RequestAuthenticate).Username
				var ok bool
				ok, err = this.server.Authenticator().Authenticate(username, request.(*connect.RequestAuthenticate).Password, this.salt)
				if err != nil {
					return
				}
				if ok {
					this.RegisterAuthorized(username)
					fmt.Println("Connect server, authorized:", this.Id(), "ip:", this.RemoteIp())
					result = &connect.ResultAuthenticate{}
					statusCode = connect.STATUS_SUCCESS
				} else {
					fmt.Println("Connect server, failure to authorize:", username, "ip:", this.RemoteIp())
					statusCode = connect.STATUS_ERROR_GENERIC
				}
			} else {
				statusCode = connect.STATUS_ERROR_ROLE
			}
		case connect.REQUEST_AS_PROXY:
			if this.Authorized() {
				if this.RegisterProxy(request.(*connect.RequestAsProxy).Address, request.(*connect.RequestAsProxy).Port, request.(*connect.RequestAsProxy).Motd, request.(*connect.RequestAsProxy).Version, request.(*connect.RequestAsProxy).Maxplayers) {
					fmt.Println("Connect server, roled as proxy:", this.Id(), "ip:", this.RemoteIp())
					result = &connect.ResultAsProxy{}
					statusCode = connect.STATUS_SUCCESS
				} else {
					statusCode = connect.STATUS_ERROR_GENERIC
				}
			} else {
				statusCode = connect.STATUS_ERROR_ROLE
			}
		case connect.REQUEST_AS_SERVER:
			if this.Authorized() {
				if this.RegisterServer(request.(*connect.RequestAsServer).Address, request.(*connect.RequestAsServer).Port) {
					fmt.Println("Connect server, roled as server:", this.Id(), "ip:", this.RemoteIp())
					result = &connect.ResultAsServer{this.serverSecurityKey}
					statusCode = connect.STATUS_SUCCESS
				} else {
					statusCode = connect.STATUS_ERROR_GENERIC
				}
			} else {
				statusCode = connect.STATUS_ERROR_ROLE
			}
		case connect.REQUEST_GET_DETAILS:
			if this.Authorized() {
				networkCache := this.server.NetworkCache()
				result = &connect.ResultGetDetails{networkCache.Address(), networkCache.Port(), networkCache.Motd(), networkCache.Version()}
				statusCode = connect.STATUS_SUCCESS
			} else {
				statusCode = connect.STATUS_ERROR_ROLE
			}
		case connect.REQUEST_GET_PLAYERS:
			if this.Authorized() {
				networkCache := this.server.NetworkCache()
				players := networkCache.Players()
				if request.(*connect.RequestGetPlayers).List {
					result = &connect.ResultGetPlayers{true, uint16(len(players)), networkCache.MaxPlayers(), players}
				} else {
					result = &connect.ResultGetPlayers{false, uint16(len(players)), networkCache.MaxPlayers(), nil}
				}
				statusCode = connect.STATUS_SUCCESS
			} else {
				statusCode = connect.STATUS_ERROR_ROLE
			}
		case connect.REQUEST_GET_SALT:
			result = &connect.ResultGetSalt{this.salt}
			statusCode = connect.STATUS_SUCCESS
		case connect.REQUEST_GET_WHOAMI:
			result = &connect.ResultGetWhoami{this.Id()}
			statusCode = connect.STATUS_SUCCESS
		case connect.REQUEST_MESSAGE:
			if this.Authorized() {
				sessionRegistry := this.server.SessionRegistry(AUTHORIZED)
				recipients := request.(*connect.RequestMessage).Recipients
				messagePacket := &connect.PacketMessageEvent{this.Id(), request.(*connect.RequestMessage).Channel, request.(*connect.RequestMessage).Message}
				messageSent := false
				if len(recipients) == 0 {
					for _, recipient := range sessionRegistry.GetAll() {
						recipient.Write(messagePacket)
						messageSent = true
					}
				} else {
//.........这里部分代码省略.........
开发者ID:Jumla,项目名称:GoLilyPad,代码行数:101,代码来源:session.go


示例4: HandlePacket

func (this *SessionOutBridge) HandlePacket(packet packet.Packet) (err error) {
	if !this.session.Authenticated() {
		this.conn.Close()
		return
	}
	switch this.state {
	case STATE_LOGIN:
		if packet.Id() == minecraft.PACKET_CLIENT_LOGIN_SUCCESS {
			this.session.redirectMutex.Lock()
			this.state = STATE_INIT
			this.session.redirecting = true
			this.EnsureCompression()
			if this.session.protocol17 {
				this.pipeline.Replace("registry", minecraft.PlayPacketClientCodec17)
			} else {
				this.pipeline.Replace("registry", minecraft.PlayPacketClientCodec)
			}
		} else if packet.Id() == minecraft.PACKET_CLIENT_LOGIN_DISCONNECT {
			this.session.DisconnectJson(packet.(*minecraft.PacketClientLoginDisconnect).Json)
			this.conn.Close()
		} else if packet.Id() == minecraft.PACKET_CLIENT_LOGIN_SET_COMPRESSION {
			this.SetCompression(packet.(*minecraft.PacketClientLoginSetCompression).Threshold)
		} else {
			if this.session.Initializing() {
				this.session.Disconnect("Error: Outbound Protocol Mismatch")
			}
			this.conn.Close()
		}
	case STATE_INIT:
		if packet.Id() == minecraft.PACKET_CLIENT_PLAYER_POSITION_AND_LOOK {
			this.session.outBridge = this
			this.session.redirecting = false
			this.session.state = STATE_CONNECTED
			this.state = STATE_CONNECTED
			this.session.redirectMutex.Unlock()
		}
		fallthrough
	case STATE_CONNECTED:
		if packet.Id() == minecraft.PACKET_CLIENT_DISCONNECT {
			this.state = STATE_DISCONNECTED
		}
		if this.state == STATE_CONNECTED {
			this.session.redirectMutex.Lock()
			if this.session.outBridge != this {
				this.session.outBridge.EnsureCompression()
				this.conn.Close()
				this.session.redirectMutex.Unlock()
				break
			}
			this.session.redirectMutex.Unlock()
		}
		switch packet.Id() {
		case minecraft.PACKET_CLIENT_JOIN_GAME:
			joinGamePacket := packet.(*minecraft.PacketClientJoinGame)
			if this.session.clientSettings != nil {
				this.Write(this.session.clientSettings)
			}
			this.session.serverEntityId = joinGamePacket.EntityId
			if this.session.state == STATE_INIT {
				this.session.clientEntityId = joinGamePacket.EntityId
			} else {
				var swapDimension int32
				if joinGamePacket.Dimension == 0 {
					swapDimension = 1
				} else {
					swapDimension = 0
				}
				this.session.Write(minecraft.NewPacketClientRespawn(swapDimension, 2, 0, "DEFAULT"))
				this.session.Write(minecraft.NewPacketClientRespawn(int32(joinGamePacket.Dimension), joinGamePacket.Difficulty, joinGamePacket.Gamemode, joinGamePacket.LevelType))
				if len(this.session.playerList) > 0 {
					if this.session.protocol17 {
						for player, _ := range this.session.playerList {
							this.session.Write(minecraft.NewPacketClientPlayerList17Remove(player))
						}
					} else {
						items := make([]minecraft.PacketClientPlayerListItem, 0, len(this.session.playerList))
						for uuidString, _ := range this.session.playerList {
							items = append(items, minecraft.PacketClientPlayerListItem{UUID: uuid.UUID(uuidString)})
						}
						this.session.Write(minecraft.NewPacketClientPlayerList(minecraft.PACKET_CLIENT_PLAYER_LIST_ACTION_REMOVE, items))
					}
					this.session.playerList = make(map[string]struct{})
				}
				if len(this.session.scoreboards) > 0 {
					for scoreboard, _ := range this.session.scoreboards {
						this.session.Write(minecraft.NewPacketClientScoreboardObjectiveRemove(scoreboard))
					}
					this.session.scoreboards = make(map[string]struct{})
				}
				if len(this.session.teams) > 0 {
					for team, _ := range this.session.teams {
						this.session.Write(minecraft.NewPacketClientTeamsRemove(team))
					}
					this.session.teams = make(map[string]struct{})
				}
				if len(this.session.pluginChannels) > 0 {
					channels := make([][]byte, 0, len(this.session.pluginChannels))
					for channel, _ := range this.session.pluginChannels {
						channels = append(channels, []byte(channel))
					}
//.........这里部分代码省略.........
开发者ID:LivousCraftNetwork,项目名称:GoLilyPad,代码行数:101,代码来源:sessionOutBridge.go


示例5: HandlePacket

func (this *SessionOutBridge) HandlePacket(packet packet.Packet) (err error) {
	this.session.outMutex.Lock()
	defer this.session.outMutex.Unlock()
	if !this.session.Authenticated() {
		this.conn.Close()
		return
	}
	switch this.state {
	case STATE_LOGIN:
		if packet.Id() == minecraft.PACKET_CLIENT_LOGIN_SUCCESS {
			this.state = STATE_INIT
			this.session.redirecting = true
			this.codec.SetEncodeCodec(minecraft.PlayPacketServerCodec)
			this.codec.SetDecodeCodec(minecraft.PlayPacketClientCodec)
		} else if packet.Id() == minecraft.PACKET_CLIENT_LOGIN_DISCONNECT {
			this.session.DisconnectRaw(packet.(*minecraft.PacketClientLoginDisconnect).Json)
			this.conn.Close()
		} else {
			if this.session.Initializing() {
				this.session.Disconnect("Error: Outbound Protocol Mismatch")
			}
			this.conn.Close()
		}
	case STATE_INIT:
		if packet.Id() == 0x08 {
			oldOutBridge := this.session.outBridge
			if oldOutBridge != nil {
				oldOutBridge.conn.Close()
			}
			this.session.outBridge = this
			this.session.redirecting = false
			this.session.state = STATE_CONNECTED
			this.state = STATE_CONNECTED
		}
		fallthrough
	case STATE_CONNECTED:
		if packet.Id() == minecraft.PACKET_CLIENT_DISCONNECT {
			this.state = STATE_DISCONNECTED
		}
		if this.session.outBridge == this && this.session.redirecting {
			break
		}
		switch packet.Id() {
		case minecraft.PACKET_CLIENT_JOIN_GAME:
			joinGamePacket := packet.(*minecraft.PacketClientJoinGame)
			if this.session.clientSettings != nil {
				this.Write(this.session.clientSettings)
			}
			this.session.serverEntityId = joinGamePacket.EntityId
			if this.session.state == STATE_INIT {
				this.session.clientEntityId = joinGamePacket.EntityId
			} else {
				var swapDimension int32
				if joinGamePacket.Dimension == 0 {
					swapDimension = 1
				} else {
					swapDimension = 0
				}
				this.session.Write(&minecraft.PacketClientRespawn{swapDimension, 2, 0, "DEFAULT"})
				this.session.Write(&minecraft.PacketClientRespawn{int32(joinGamePacket.Dimension), joinGamePacket.Difficulty, joinGamePacket.Gamemode, joinGamePacket.LevelType})
				for player, _ := range this.session.playerList {
					this.session.Write(&minecraft.PacketClientPlayerListItem{player, false, 0})
				}
				this.session.playerList = make(map[string]bool)
				for scoreboard, _ := range this.session.scoreboards {
					this.session.Write(&minecraft.PacketClientScoreboardObjective{scoreboard, "", 1})
				}
				this.session.scoreboards = make(map[string]bool)
				for team, _ := range this.session.teams {
					this.session.Write(&minecraft.PacketClientTeams{team, 1, "", "", "", 0, nil})
				}
				this.session.teams = make(map[string]bool)
				return
			}
		case minecraft.PACKET_CLIENT_PLAYER_LIST_ITEM:
			playerListPacket := packet.(*minecraft.PacketClientPlayerListItem)
			if playerListPacket.Online {
				this.session.playerList[playerListPacket.Name] = true
			} else {
				delete(this.session.playerList, playerListPacket.Name)
			}
		case minecraft.PACKET_CLIENT_SCOREBOARD_OBJECTIVE:
			scoreboardPacket := packet.(*minecraft.PacketClientScoreboardObjective)
			if scoreboardPacket.Action == 0 {
				this.session.scoreboards[scoreboardPacket.Name] = true
			} else if scoreboardPacket.Action == 1 {
				delete(this.session.scoreboards, scoreboardPacket.Name)
			}
		case minecraft.PACKET_CLIENT_TEAMS:
			teamPacket := packet.(*minecraft.PacketClientTeams)
			if teamPacket.Action == 0 {
				this.session.teams[teamPacket.Name] = true
			} else if teamPacket.Action == 1 {
				delete(this.session.teams, teamPacket.Name)
			}
		case minecraft.PACKET_CLIENT_DISCONNECT:
			this.session.DisconnectRaw(packet.(*minecraft.PacketClientDisconnect).Json)
			return
		default:
			if genericPacket, ok := packet.(*minecraft.PacketGeneric); ok {
//.........这里部分代码省略.........
开发者ID:Jumla,项目名称:GoLilyPad,代码行数:101,代码来源:sessionOutBridge.go



注:本文中的github.com/LilyPad/GoLilyPad/packet.Packet类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang backend.View类代码示例发布时间:2022-05-23
下一篇:
Golang packet.WriteUint16函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap