コード例 #1
0
ファイル: avatar.go プロジェクト: archwyrm/ghack
func (a *avatar) control(ctrl <-chan Msg, input <-chan *protocol.Message) {
	for {
		select {
		case msg := <-ctrl:
			switch m := msg.(type) {
			// TODO: Handle MsgTick?
			case MsgQuit:
				a.player.Chan <- MsgSetState{Remove{true}}
				a.svc.Game <- MsgEntityRemoved{&a.player}
				return
			}
		case msg := <-input:
			switch *msg.Type {
			case protocol.Message_Type(protocol.Message_MOVE):
				dir := msg.Move.Direction
				vec := s3dm.NewV3(*dir.X, *dir.Y, *dir.Z)
				a.player.Chan <- MsgRunAction{Move{vec}, false}
			default:
				log.Println("Client sent unhandled message, ignoring:",
					protocol.Message_Type_name[int32(*msg.Type)])

			}
		}
	}
}
コード例 #2
0
ファイル: comm.go プロジェクト: archwyrm/ghack
// Receives messages from remote client and acts upon them if appropriate.
func (cl *client) RecvLoop(cs chan<- Msg) {
	defer logAndClose(cl.conn)
	for {
		msg, err := readMessage(cl.conn)
		if err != nil {
			// Remove client if something went wrong
			cs <- removeClientMsg{cl, "Reading message from client failed: " + err.String()}
			return
		}
		switch *msg.Type {
		case protocol.Message_Type(protocol.Message_DISCONNECT):
			cs <- removeClientMsg{cl, proto.GetString(msg.Disconnect.ReasonStr)}
			return
		default:
			// TODO: If no proper avatar has been started, this will block, fix?
			// We could check cl.avatar for nil
			cl.RecvQueue <- msg // Forward to avatar
		}
	}
}