<commit_before>/*
 *  Charon: A game authentication server
 *  Copyright (C) 2014-2015  <NAME> <<EMAIL>>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package charon

import (
	"bytes"
	"crypto/rand"
	"encoding/binary"
	"errors"
	"log"
	"net"
)

// AuthApp contains all state for a single instance of the
// authentication server.
type AuthApp struct {
	database Database
}

type request struct {
	address *net.UDPAddr
	message []byte
}

type response struct {
	address *net.UDPAddr
	message []byte
}

type routeFunc func(*request) (response, error)

// NewAuthApp creates a new instance of the auth server app.
func NewAuthApp() (authApp *AuthApp, err error) {
	authApp = new(AuthApp)

	// Initialize database connection
	database, err := NewDatabase()
	if err != nil {
		return
	}
	authApp.database = *database
	return
}

// ListenAndServe starts the auth server app.
func (self *AuthApp) ListenAndServe(addr string) (err error) {
	listenaddr, err := net.ResolveUDPAddr("udp", addr)
	if err != nil {
		return
	}

	conn, err := net.ListenUDP("udp", listenaddr)
	if err != nil {
		return
	}

	for {
		message := make([]byte, 1024)

		msglen, msgaddr, msgerr := conn.ReadFromUDP(message)
		if msgerr != nil {
			log.Printf("[ERROR] %s", err.Error())
			continue
		}

		req := request{msgaddr, message[:msglen]}
		go self.requestHandler(&req)
	}
}

func (self *AuthApp) requestHandler(req *request) {
	// Select callback function to route to.
	route, err := self.router(req)
	if err != nil {
		log.Printf("[DEBUG] %s", err.Error())
	}

	// Route message to callback function.
	res, err := route(req)
	if err != nil {
		log.Printf("[DEBUG] %s", err.Error())
	}

	// Respond to sender.
	conn, err := net.ListenUDP("udp", req.address)
	if err != nil {
		log.Printf("[DEBUG] %s", err.Error())
	}<commit_msg>Immediately return on error in request handler.<commit_after>/*
 *  Charon: A game authentication server
 *  Copyright (C) 2014-2016  <NAME> <<EMAIL>>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package charon

import (
	"bytes"
	"crypto/rand"
	"encoding/binary"
	"errors"
	"log"
	"net"
)

// AuthApp contains all state for a single instance of the
// authentication server.
type AuthApp struct {
	database Database
}

type request struct {
	address *net.UDPAddr
	message []byte
}

type response struct {
	address *net.UDPAddr
	message []byte
}

type routeFunc func(*request) (response, error)

// NewAuthApp creates a new instance of the auth server app.
func NewAuthApp() (authApp *AuthApp, err error) {
	authApp = new(AuthApp)

	// Initialize database connection
	database, err := NewDatabase()
	if err != nil {
		return
	}
	authApp.database = *database
	return
}

// ListenAndServe starts the auth server app.
func (self *AuthApp) ListenAndServe(addr string) (err error) {
	listenaddr, err := net.ResolveUDPAddr("udp", addr)
	if err != nil {
		return
	}

	conn, err := net.ListenUDP("udp", listenaddr)
	if err != nil {
		return
	}

	for {
		message := make([]byte, 1024)

		msglen, msgaddr, msgerr := conn.ReadFromUDP(message)
		if msgerr != nil {
			log.Printf("[ERROR] %s", err.Error())
			continue
		}

		req := request{msgaddr, message[:msglen]}
		go self.requestHandler(&req)
	}
}

func (self *AuthApp) requestHandler(req *request) {
	// Select callback function to route to.
	route, err := self.router(req)
	if err != nil {
		log.Printf("[DEBUG] %s", err.Error())
		return
	}

	// Route message to callback function.
	res, err := route(req)
	if err != nil {
		log.Printf("[DEBUG] %s", err.Error())
		return
	}

	// Respond to sender.
	conn, err := net.ListenUDP("udp", req.address)
	if err != nil {
		log.Printf("[DEBUG] %s", err.Error())
		return
	}
{'fraction_non_alphanumeric': 0.11799230924913985, 'fraction_numerical': 0.005262092693786683, 'mean_word_length': 3.6143790849673203, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2023176', 'n_tokens_mistral': 1645, 'n_tokens_neox': 1585, 'n_words': 579}