aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/world_messages.go (1592B)


      1 package main
      2 
      3 // Owners publish lifetime changes while the directory is running. Shutdown
      4 // joins owners and drains their messages before retiring the directory.
      5 type worldOwnership struct {
      6 	id   uint32
      7 	zone *zoneRuntime
      8 	done chan struct{}
      9 }
     10 
     11 func (world *worldRuntime) setOwner(id uint32, zone *zoneRuntime) {
     12 	change := worldOwnership{id: id, zone: zone, done: make(chan struct{})}
     13 	world.ownership <- change
     14 	<-change.done
     15 }
     16 
     17 type worldMessage struct {
     18 	entity   uint32
     19 	command  zoneCommand
     20 	accepted chan struct{}
     21 }
     22 
     23 type worldDelivery struct {
     24 	entity uint32
     25 	err    error
     26 }
     27 
     28 // Acceptance transfers the command to the directory. The source does not wait
     29 // for the receiving owner, which may itself have messages for the source.
     30 func (world *worldRuntime) send(entity uint32, command zoneCommand) {
     31 	message := worldMessage{entity: entity, command: command, accepted: make(chan struct{})}
     32 	world.messages <- message
     33 	<-message.accepted
     34 }
     35 
     36 func (world *worldRuntime) deliver(message worldMessage) error {
     37 	for {
     38 		route, err := world.lookup(message.entity)
     39 		if err != nil || route == nil {
     40 			// Retirement can race with another owner's already accepted message.
     41 			return err
     42 		}
     43 		err = route.zone.call(zoneEntityCommand{message.entity, message.command})
     44 		if err == errZoneOwnerChanged {
     45 			continue
     46 		}
     47 		return err
     48 	}
     49 }
     50 
     51 type zoneEntityCommand struct {
     52 	entity  uint32
     53 	command zoneCommand
     54 }
     55 
     56 func (command zoneEntityCommand) execute(state *server) error {
     57 	if state.characters[command.entity] == nil {
     58 		return errZoneOwnerChanged
     59 	}
     60 	return command.command.execute(state)
     61 }