mysql - Online Game Login Design -
i working on online game , have design problem login server. each player must logged-in 1 client, if tries open client , tries login should tell account logged in client.
that works, problem when tries login 2 clients @ exact same time both clients go through, causing account logged-in in 2 different places. not wan't.
my current approach (i'm using golang):
var onlogin map[string]uint16 = make(map[string]uint16) func login(data []byte) []byte { username := cstring(data[8:23]) password := cstring(data[24:39]) // check if trying login _, loggingin := onlogin[username] if loggingin { fmt.println("possible double connect attempt", username) return nil } (...check if correct login info...) (...check if user logged in...) // remove list before returning delete(onlogin, username) (...build , send packet client if login success or fail...) it helps (a bit) when able press login button on 2 clients @ inhumanly exact same time still goes through. maybe design flawed? advice? cheers!
your onlogin needs mutex, if run code -race tell there's race.
race detector must read.
// edit example:
var onlogin = struct { sync.mutex m map[string]bool }{m: map[string]bool{}} func login(data []byte) []byte { username := cstring(data[8:23]) password := cstring(data[24:39]) onlogin.lock() defer onlogin.unlock() if _, loggingin := onlogin.m[username]; loggingin { fmt.println("possible double connect attempt", username) return nil } ///.... }
Comments
Post a Comment