Beispiel #1
0
func main() {

	flag.Parse()

	if 1 != flag.NArg() {
		fmt.Printf("Usage:pp_server conf_file\n", flag.Arg(0))
		return
	}

	nettao.LoadConfig(flag.Arg(0))

	ipport := nettao.AppConf.String("ipport")
	maxconn, _ := nettao.AppConf.Int("maxconn")
	maxpkgcount, _ := nettao.AppConf.Int("maxpkgcount")

	runtime.GOMAXPROCS(2 * runtime.NumCPU())

	var cmdRange nettao.CmdRange

	cmdRange.CmdMin = 100
	cmdRange.CmdMax = 101
	cmdHandlerType := make(map[reflect.Type]nettao.CmdRange)
	cmdHandlerType[reflect.TypeOf(pp.PPHandler{})] = cmdRange
	nettao.RegisterCmdHandlers("pp", cmdHandlerType)

	ppHinfo := nettao.GetHandlerInfo("pp")
	if nil == ppHinfo {
		fmt.Fprintf(os.Stderr, "Fatal error: ppHinfo is nil\n")
		os.Exit(1)
	}

	var connCount int
	for {
		conn, err := net.Dial("tcp", ipport)
		if err != nil {
			continue
		}
		ctx := nettao.NewConnCtx(conn, ppHinfo)
		// run as a goroutine
		go beginPingPong(ctx)
		go nettao.HandleSession(ctx)
		connCount++
		if connCount >= maxconn {
			break
		}
	}

	timeout := time.NewTimer(time.Second)
	for {
		timeout.Reset(time.Second)
		select {
		case <-timeout.C:
			fmt.Printf("Client do RecvPkgCount=%d\n", pp.RecvPkgCount)
		}
		if pp.RecvPkgCount >= int32(maxpkgcount) {
			break
		}
	}
	fmt.Printf("Client finish\n")
}
Beispiel #2
0
func main() {

	flag.Parse()

	if 1 != flag.NArg() {
		fmt.Printf("Usage:pp_server conf_file\n", flag.Arg(0))
		return
	}

	nettao.LoadConfig(flag.Arg(0))

	runtime.GOMAXPROCS(2 * runtime.NumCPU())

	ipport := nettao.AppConf.String("ipport")
	//service := ":9190"
	listener, err := net.Listen("tcp", ipport)
	checkError(err)

	var cmdRange nettao.CmdRange

	cmdRange.CmdMin = 100
	cmdRange.CmdMax = 100
	cmdHandlerType := make(map[reflect.Type]nettao.CmdRange)
	cmdHandlerType[reflect.TypeOf(pp.PPHandler{})] = cmdRange
	nettao.RegisterCmdHandlers("pp", cmdHandlerType)

	ppHinfo := nettao.GetHandlerInfo("pp")
	if nil == ppHinfo {
		fmt.Fprintf(os.Stderr, "Fatal error: ppHinfo is nil\n")
		os.Exit(1)
	}

	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}
		ctx := nettao.NewConnCtx(conn, ppHinfo)
		// run as a goroutine
		go nettao.HandleSession(ctx)
	}
}