示例#1
0
文件: net.go 项目: codeslinger/Go25
func RunTCP(t TCPService) {
	l, err := net.ListenTCP("tcp", t.Addr())
	if err != nil {
		log.Error("failed to bind to local address %s", t.Addr())
		t.Shutdown()
		return
	}
	defer l.Close()

	log.Info("listening for connections on %s", t.Addr())
	for {
		conn, err := l.AcceptTCP()
		if err != nil {
			log.Error("failed to accept connection: %v", err)
			continue
		}
		if err := t.SetClientOptions(conn); err != nil {
			conn.Close()
			continue
		}
		log.Trace(func() string {
			return fmt.Sprintf("%s: client connected to %s", conn.RemoteAddr(), t.Addr())
		})
		go t.Handle(conn)
	}
}
示例#2
0
文件: main.go 项目: codeslinger/Go25
func trapSignals() chan int {
	exitChan := make(chan int)
	signalChan := make(chan os.Signal, 1)
	go func() {
		s := <-signalChan
		log.Info("received signal %d", s)
		exitChan <- 1
	}()
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
	return exitChan
}
示例#3
0
文件: main.go 项目: codeslinger/Go25
func main() {
	flag.Parse()
	cfg, err := LoadConfig(*configPath)
	if err != nil {
		log.Error("failed to load config: %s", err)
		return
	}
	log.Info("loaded config: %s", cfg)
	runtime.GOMAXPROCS(cfg.Cores())
	exitChan := trapSignals()
	go RunTCP(NewSMTPService(cfg, exitChan))
	<-exitChan
}