func Main() { porterIPString := flag.String("porterIP", "127.0.0.1", "Porter Server IP") porterPortString := flag.String("porterPort", ":5269", "Porter Server Port") portstring := flag.String("port", ":8888", "Server port") tcpportstring := flag.String("tcpport", ":11199", "TCP socket port") logpath := flag.String("logpath", "/dev/stderr", "Logging location") flag.Parse() port = PortMgmt.NewPortInfo(*portstring) tcpPort = PortMgmt.NewPortInfo(*tcpportstring) porterPort = PortMgmt.NewPortInfo(*porterPortString) porterIP = PortMgmt.IPString(*porterIPString) logger.SetupLoggerHelper(*logpath) if !verifyEnvironment() { logger.Fatalf("Please set $ROOTIQUE to your project root") } setupGamehub() go gamehub.handleConnections() go socketListen(tcpPort.Port) logger.Debugf("Socket listening on port %s", tcpPort) http.HandleFunc("/ws", serveWs) logger.Debugf("Http server listening on port %s", port) err := http.ListenAndServe(port.Port, nil) if err != nil { logger.Fatalf("ListenAndServe: %s", err.Error()) } }
func (pc *clientConnection) wsReadPump() { defer func() { pc.conn.Close() }() for { msg, err := pc.conn.Read() if err != nil { if err == io.EOF || err == io.ErrUnexpectedEOF { // the client ID here is redundant... kc := killClient{pc.info.id} killcconn := CommandMarshal("killClient", kc) pc.currentHandler.handleWebsocket([]byte(killcconn), pc) } else { logger.Errorf("Error while reading from websocket: %s", err) } break } logger.Debugf("Received %s from websocket", msg) select { case newHandler := <-pc.handlers: pc.currentHandler = newHandler default: } pc.currentHandler.handleWebsocket(msg, pc) } }
func generateResponse(payload interface{}, status int) []byte { response, err := json.Marshal(api.ResponseType{Status: status, Payload: payload}) if err != nil { logger.Warnf("Could not generate response: %s", err.Error()) backupResponse, err := json.Marshal(api.ResponseType{ Status: api.STATUS_UNSERIALIZEABLE_RESPONSE, Payload: "problem"}) if err != nil { return []byte("Really bad") } logger.Infof("Generating response with status %d", status) logger.Debugf("Full message %s", string(response)) return backupResponse } logger.Infof("Generating response with status %d", status) logger.Debugf("Full message %s", string(response)) return response }
func (pc *clientConnection) wsWritePump() { for msg := range pc.toClient { logger.Debugf("Writing %s to websocket", msg) err := pc.conn.Write(msg) if err != nil { logger.Errorf("Error while writing to websocket: %s", err) break } } }
func handleConnection(netConn net.Conn) error { conn := connection.NewSocketConn(netConn) handler := request_handler.NewRequestHandler() for { logger.Infof("Handling a connection") request, err := conn.Read() if err != nil { logger.Warnf("Error reading request (%s)", err.Error()) conn.Close() return err } logger.Debugf("Got request %s", string(request)) response := handler.HandleRequest(request) logger.Debugf("Sent response %s", string(response)) err = conn.Write(response) if err != nil { logger.Warnf("Error writing response (%s)", err.Error()) conn.Close() return err } } }
func (p *proxy) serverReadPump() { defer func() { p.server.conn.Close() }() for { msg, err := p.server.conn.Read() if err != nil { logger.Errorf("Error while reading from socket: %s", err) break } logger.Debugf("Received %s from socket", msg) p.broadcast(msg) } }
func ValidMove(moves int, movementType *Movement, tiles []Terrain, locations []Location) error { if len(locations) < 2 { message := "Need at least two tiles, a start and an end" logger.Infof(message+" (got %d)", len(locations)) return errors.New(message) } err := assertAllTilesTouch(locations) if err != nil { return err } for _, tile := range tiles { moves -= int(movementType.costs[tile]) } logger.Debugf("Had this many moves left %d", moves) if moves >= 0 { return nil } else { message := fmt.Sprintf("Too much distance to cover, need %d less", moves) logger.Infof(message) return errors.New(message) } }
func calculateDamage(attack *attack, armor *armor) int { damage := attack.power / armor.strength logger.Debugf("Calculated damage to be %d", damage) return damage }