Ejemplo n.º 1
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	c, err := goconfig.LoadConfigFile("conf/conf.ini")
	if err != nil {
		log.Fatal(err)
	}

	HTTP_PORT, err = c.GetValue("Server", "port")
	checkError(err)

	// creates a tcp listener
	tcpAddr, err := net.ResolveTCPAddr("tcp4", ":"+HTTP_PORT)
	checkError(err)
	listener, err := net.ListenTCP("tcp", tcpAddr)
	checkError(err)

	sendChan, err := c.Int("Server", "sendChan")
	checkError(err)
	receiveChan, err := c.Int("Server", "receiveChan")
	checkError(err)

	// creates a server
	config := &gametcp.Config{
		PacketSendChanLimit:    uint32(sendChan),
		PacketReceiveChanLimit: uint32(receiveChan),
	}
	srv := gametcp.NewServer(config, &Callback{})

	// starts service
	go srv.Start(listener, time.Second*5)
	alog.Info("listening:" + listener.Addr().String())

	// catchs system signal
	chSig := make(chan os.Signal)
	signal.Notify(chSig, syscall.SIGINT, syscall.SIGTERM)
	sig := <-chSig
	alog.Error("listening:" + sig.String())

	// stops service
	srv.Stop()
}
Ejemplo n.º 2
0
func (this *Callback) OnClose(c *gametcp.Conn) {
	alog.Info("OnClose:" + c.GetExtraData().(net.Addr).String())
}
Ejemplo n.º 3
0
func (this *Callback) OnConnect(c *gametcp.Conn) bool {
	addr := c.GetRawConn().RemoteAddr()
	c.PutExtraData(addr)
	alog.Info("OnConnect:" + addr.String())
	return true
}