server/chat_runtime.go (6320B)
1 package main 2 3 import ( 4 "database/sql" 5 "log" 6 "net" 7 ) 8 9 type chatPerson struct { 10 id uint32 11 name, credential string 12 online bool 13 team uint32 14 revision uint64 15 buddies map[uint32]string 16 pending []string 17 } 18 19 type chatState struct { 20 db *sql.DB 21 writer *databaseWriter 22 people map[uint32]*chatPerson 23 clients map[uint32]*chatClient 24 } 25 26 type chatCommand interface { 27 execute(*chatState) error 28 } 29 30 type chatRequest struct { 31 command chatCommand 32 done chan error 33 } 34 35 // The mailbox owns only queued commands; its worker owns all social state. 36 // Admission never waits for SQL. Network callers wait for their completion 37 // outside the simulation, and shutdown drains accepted work before joining both. 38 type chatRuntime struct { 39 commands chan chatRequest 40 stopping chan struct{} 41 done chan struct{} 42 } 43 44 func newChatRuntime(db *sql.DB, writer *databaseWriter, people map[uint32]*chatPerson) (*chatRuntime, error) { 45 rows, err := db.Query("SELECT player_id, buddy_id, metadata FROM buddies") 46 if err != nil { 47 return nil, err 48 } 49 for rows.Next() { 50 var owner, buddy uint32 51 var metadata string 52 if err := rows.Scan(&owner, &buddy, &metadata); err != nil { 53 rows.Close() 54 return nil, err 55 } 56 if people[owner].buddies == nil { 57 people[owner].buddies = make(map[uint32]string) 58 } 59 people[owner].buddies[buddy] = metadata 60 } 61 err = rows.Err() 62 rows.Close() 63 if err != nil { 64 return nil, err 65 } 66 runtime := &chatRuntime{commands: make(chan chatRequest), stopping: make(chan struct{}), done: make(chan struct{})} 67 state := &chatState{db: db, writer: writer, people: people, clients: make(map[uint32]*chatClient)} 68 work, finished := make(chan chatRequest), make(chan struct{}) 69 go func() { 70 defer close(finished) 71 for request := range work { 72 err := request.command.execute(state) 73 if request.done != nil { 74 request.done <- err 75 } else if err != nil { 76 log.Printf("Chat publication: %v", err) 77 } 78 } 79 }() 80 go func() { 81 defer close(runtime.done) 82 requests, stopping := runtime.commands, runtime.stopping 83 var pending []chatRequest 84 for { 85 if stopping == nil && len(pending) == 0 { 86 close(work) 87 <-finished 88 return 89 } 90 var ready chan chatRequest 91 var next chatRequest 92 if len(pending) != 0 { 93 ready, next = work, pending[0] 94 } 95 select { 96 case request := <-requests: 97 pending = append(pending, request) 98 case ready <- next: 99 pending[0] = chatRequest{} 100 pending = pending[1:] 101 case <-stopping: 102 requests, stopping = nil, nil 103 } 104 } 105 }() 106 return runtime, nil 107 } 108 109 func (runtime *chatRuntime) call(command chatCommand) error { 110 request := chatRequest{command: command, done: make(chan error, 1)} 111 select { 112 case runtime.commands <- request: 113 case <-runtime.done: 114 return net.ErrClosed 115 } 116 select { 117 case err := <-request.done: 118 return err 119 case <-runtime.done: 120 select { 121 case err := <-request.done: 122 return err 123 default: 124 return net.ErrClosed 125 } 126 } 127 } 128 129 func (runtime *chatRuntime) post(command chatCommand) { 130 select { 131 case runtime.commands <- chatRequest{command: command}: 132 case <-runtime.done: 133 } 134 } 135 136 func (runtime *chatRuntime) stop() { 137 close(runtime.stopping) 138 <-runtime.done 139 } 140 141 func (server *server) chatPeople() map[uint32]*chatPerson { 142 people := make(map[uint32]*chatPerson) 143 for id, player := range server.characters { 144 if player.npc { 145 continue 146 } 147 person := &chatPerson{id: id, name: player.name, credential: player.browserTokenHash, 148 online: server.clients[id] != nil, revision: player.teamRevision} 149 if player.team != nil { 150 person.team = player.team.id 151 } 152 people[id] = person 153 } 154 return people 155 } 156 157 func (server *server) chat() (*chatRuntime, error) { 158 if server.chatService == nil { 159 runtime, err := newChatRuntime(server.db, server.databaseWriter, server.chatPeople()) 160 if err != nil { 161 return nil, err 162 } 163 server.chatService = runtime 164 } 165 return server.chatService, nil 166 } 167 168 type chatWorldPresence struct { 169 player uint32 170 name, credential string 171 online bool 172 } 173 174 func (update chatWorldPresence) execute(state *chatState) error { 175 person := state.people[update.player] 176 if person == nil { 177 person = &chatPerson{id: update.player} 178 state.people[update.player] = person 179 } 180 person.name, person.credential, person.online = update.name, update.credential, update.online 181 if !person.online { 182 person.pending = nil 183 } 184 if client := state.clients[update.player]; client != nil && (!person.online || client.browserTokenHash != "" && client.browserTokenHash != person.credential) { 185 client.disconnect(client.conn) 186 } 187 return nil 188 } 189 190 func (server *server) publishChatPerson(player *character) { 191 server.markPresence(player.id) 192 if server.chatService != nil { 193 server.chatService.post(chatWorldPresence{player: player.id, name: player.name, 194 credential: player.browserTokenHash, online: server.clients[player.id] != nil}) 195 } 196 } 197 198 type chatJoin struct { 199 client *chatClient 200 player uint32 201 } 202 203 func (command chatJoin) execute(state *chatState) error { 204 return state.initializeChat(command.client, command.player) 205 } 206 207 type chatLeave struct{ client *chatClient } 208 209 func (command chatLeave) execute(state *chatState) error { 210 if state.clients[command.client.id] != command.client { 211 return nil 212 } 213 delete(state.clients, command.client.id) 214 for client, packet := range state.buddyPresence(command.client.id, 0) { 215 client.send(packet) 216 } 217 return nil 218 } 219 220 type chatInput struct { 221 client *chatClient 222 kind uint16 223 body []byte 224 } 225 226 func (command chatInput) execute(state *chatState) error { 227 return state.handlePacket(command.client, command.kind, command.body) 228 } 229 230 type chatConnectionQuery struct { 231 player uint32 232 connected bool 233 } 234 235 func (query *chatConnectionQuery) execute(state *chatState) error { 236 query.connected = state.clients[query.player] != nil 237 return nil 238 } 239 240 type chatSystemMessages struct { 241 player uint32 242 messages []string 243 } 244 245 func (command chatSystemMessages) execute(state *chatState) error { 246 if client := state.clients[command.player]; client != nil { 247 for _, message := range command.messages { 248 client.send(chatPacket(36, appendChatString(nil, message))) 249 } 250 } else { 251 person := state.people[command.player] 252 if person != nil { 253 person.pending = append(person.pending, command.messages...) 254 } 255 } 256 return nil 257 }