当前位置: 首页>>代码示例>>Golang>>正文


Golang TCPListener.SetDeadline方法代码示例

本文整理汇总了Golang中net.TCPListener.SetDeadline方法的典型用法代码示例。如果您正苦于以下问题:Golang TCPListener.SetDeadline方法的具体用法?Golang TCPListener.SetDeadline怎么用?Golang TCPListener.SetDeadline使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net.TCPListener的用法示例。


在下文中一共展示了TCPListener.SetDeadline方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: loop

func (s *Server) loop(listener *net.TCPListener) {
	defer s.waitGroup.Done()
	log.Printf("[DEBUG] waiting for connections...")
	for {
		select {
		case <-s.ch:
			log.Printf("[DEBUG] stopping listening on %s", listener.Addr())
			_ = listener.Close()
			return
		default:
		}
		_ = listener.SetDeadline(time.Now().Add(s.Deadline))
		conn, err := listener.AcceptTCP()
		if nil != err {
			if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
				continue
			}
			log.Printf("[ERROR] error on loop: %s", err)
		}
		log.Printf("[DEBUG] connected to %s", conn.RemoteAddr())
		c := &connection{
			conn:          conn,
			protocol:      s.protocolCtor(conn),
			waitGroup:     s.waitGroup,
			deadline:      s.Deadline,
			readBuffSize:  s.ReadBuffSize,
			writeBuffSize: s.WriteBuffSize,
		}
		s.waitGroup.Add(1)
		go c.handle(s.ch)
	}
}
开发者ID:larixsource,项目名称:atto,代码行数:32,代码来源:server.go

示例2: Start

func (this *Server) Start(listener *net.TCPListener) {
	log.Printf("Start listen on %v", listener.Addr())
	this.waitGroup.Add(1)
	defer func() {
		listener.Close()
		this.waitGroup.Done()
	}()

	for {
		select {
		case <-this.exitCh:
			log.Printf("Stop listen on %v", listener.Addr())
			return
		default:
		}

		listener.SetDeadline(time.Now().Add(this.acceptTimeout))
		conn, err := listener.AcceptTCP()
		if err != nil {
			if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
				// log.Printf("Accept timeout: %v", opErr)
				continue
			}
			log.Printf("Accept error: %v", err)
			continue
		}

		log.Printf("Accept: %v", conn.RemoteAddr())
		go this.handleClientConn(conn)
	}
}
开发者ID:ningye,项目名称:gotcp,代码行数:31,代码来源:server.go

示例3: Start

// Start starts service
func (s *Server) Start(listener *net.TCPListener, acceptTimeout time.Duration) {
	s.waitGroup.Add(1)
	defer func() {
		listener.Close()
		s.waitGroup.Done()
	}()

	for {
		select {
		case <-s.exitChan:
			return

		default:
		}

		listener.SetDeadline(time.Now().Add(acceptTimeout))

		conn, err := listener.AcceptTCP()
		if err != nil {
			continue
		}

		go newConn(conn, s).Do()
	}
}
开发者ID:ciaos,项目名称:gotcp,代码行数:26,代码来源:server.go

示例4: listen

// listen starts listening for a video connection on a socket for the given
// player. This video will be streamed to the partner.
func listen(ln *net.TCPListener, p *player, partner *player, pairs *[]playerPair) {
	var err error

	// Wait for a TCP connection
	for {
		p.Lock()
		ln.SetDeadline(time.Now().Add(time.Second * 5))
		p.conn, err = ln.AcceptTCP()
		if err == nil {
			break
		}
		p.Unlock()
	}

	log.Println("connected to player", p.id)
	p.conn.SetKeepAlive(true)
	p.conn.SetKeepAlivePeriod(time.Second / 2)
	p.Unlock()
	streamVideo(p, partner)
	removePlayer(pairs, p)
	p.Lock()
	log.Println("lost connection to player", p.id)
	p.active = false
	p.Unlock()
}
开发者ID:cardboardcrusaders,项目名称:cardboard-calamity-server,代码行数:27,代码来源:main.go

示例5: Start

// Start starts service
func (s *Server) Start(listener *net.TCPListener, acceptTimeout time.Duration) {
	s.waitGroup.Add(1)
	defer func() {
		listener.Close()
		s.waitGroup.Done()
	}()

	var index uint32
	index = 0
	for {
		select {
		case <-s.exitChan:
			return

		default:
		}

		listener.SetDeadline(time.Now().Add(acceptTimeout))

		conn, err := listener.AcceptTCP()
		if err != nil {
			continue
		}

		myconn := newConn(conn, s, index)
		s.mqhub.conns[index] = myconn
		index += 1

		go myconn.Do()
	}
}
开发者ID:GisKook,项目名称:gobed,代码行数:32,代码来源:server.go

示例6: Serve

func (self *Service) Serve(listener *net.TCPListener) {
	defer self.wg.Done()

	log.Println("initializing...")
	var err error
	self.repo, err = repository.Initialize(self.dataDir)
	log.Println("data directory: ", self.dataDir)
	if err != nil {
		log.Fatal(err)
	}

	for {
		select {
		case <-self.ch:
			log.Println("stopping listening on", listener.Addr())
			listener.Close()
			return
		default:
		}
		listener.SetDeadline(time.Now().Add(1e9))
		conn, err := listener.AcceptTCP()
		if nil != err {
			if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
				continue
			}
			log.Println(err)
		}
		self.wg.Add(1)
		go self.HandleConnection(conn)
	}
}
开发者ID:gitter-badger,项目名称:siberite,代码行数:31,代码来源:service.go

示例7: accept_workers

func accept_workers(listener *net.TCPListener, wg *sync.WaitGroup) {
	defer wg.Done()
	for {
		if server_state == STOPPED {
			return
		}
		listener.SetDeadline(time.Now().Add(time.Duration(time.Second)))
		conn, err := listener.Accept()
		if err != nil {
			netErr, ok := err.(net.Error)
			if ok && netErr.Timeout() && netErr.Temporary() {
				continue
			} else {
				logger.Printf("accept worker error: %v\n", err)
				server_state = STOPPED
				return
			}
		}
		worker := &tsp_task_manager.WorkerInfo{new_worker_id, &conn, -1}
		tsp_task_manager.AddNewWorker(worker)
		logger.Println("I'm accept worker #", new_worker_id)
		go listen_worker(worker)
		new_worker_id++
	}
}
开发者ID:Alnen,项目名称:university_distributed_os_course,代码行数:25,代码来源:main_server.go

示例8: accept_clients

func accept_clients(listener *net.TCPListener, wg *sync.WaitGroup) {
	defer wg.Done()
	for {
		if server_state == STOPPED {
			return
		}
		listener.SetDeadline(time.Now().Add(time.Duration(time.Second)))
		conn, err := listener.Accept()
		if err != nil {
			netErr, ok := err.(net.Error)
			if ok && netErr.Timeout() && netErr.Temporary() {
				continue
			} else {
				logger.Printf("accept client error: %v\n", err)
				server_state = STOPPED
				return
			}
		}
		client := tsp_task_manager.ClientInfo{new_client_id, &conn}
		new_client_id++
		tsp_task_manager.AddNewClient(client)
		logger.Println("I'm accept client #", client.ID)
		go listen_client(client)
	}
}
开发者ID:Alnen,项目名称:university_distributed_os_course,代码行数:25,代码来源:main_server.go

示例9: Start

// Start starts service
func (s *Server) Start(listener *net.TCPListener, acceptTimeout time.Duration) {
	s.waitGroup.Add(1)
	defer func() {
		listener.Close()
		s.waitGroup.Done()
	}()

	for {
		select {
		case <-s.exitChan:
			return

		default:
		}

		listener.SetDeadline(time.Now().Add(acceptTimeout))

		conn, err := listener.AcceptTCP()

		if e, ok := err.(net.Error); ok && e.Timeout() {
			continue
			// This was a timeout
		} else if err != nil {
			l4g.Info("listener accepttcp continue and found a error: %v", err)
			return
			// This was an error, but not a timeout
		}

		go newConn(conn, s).Do()
	}
}
开发者ID:whiskerman,项目名称:gotcp,代码行数:32,代码来源:server.go

示例10: Serve

// Accept connections and spawn a goroutine to serve each one.  Stop listening
// if anything is received on the service's channel.
func (self *Service) Serve(listener *net.TCPListener) {
	defer self.waitGroup.Done()
	for {
		select {
		case <-self.done:
			log.Println("Stopping listening on", listener.Addr())
			listener.Close()
			return
		default:
		}

		listener.SetDeadline(time.Now().Add(1e9))
		conn, err := listener.AcceptTCP()
		if err != nil {
			if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
				continue
			}
			log.Println(err)
		}

		log.Println(conn.RemoteAddr(), "connected")

		connection := NewConnection(conn, make(chan []byte))
		self.dataMap[connection.Id] = connection
		self.waitGroup.Add(1)
		go self.serve(connection)
	}
}
开发者ID:voxadam,项目名称:cascades-sockets,代码行数:30,代码来源:service.go

示例11: Run

func (this *Server) Run(listener *net.TCPListener) {
	defer func() {
		listener.Close()
	}()

	//go this.dealSpamConn()
	log.Infof("Starting comet server on: %s", listener.Addr().String())
	if err := storage.Instance.AddComet(this.Name); err != nil {
		log.Errorf("failed to add comet to Redis: %s", err.Error())
	}

	// keep the data of this node not expired on redis
	this.startRefreshRoutine()
	this.startWorkers()

	for {
		select {
		case <-this.ctrl:
			log.Infof("ask me to quit")
			this.stopWorkers()
			this.stopRefreshRoutine()
			return
		default:
		}

		listener.SetDeadline(time.Now().Add(this.acceptTimeout * time.Second))
		conn, err := listener.AcceptTCP()
		if err != nil {
			if e, ok := err.(*net.OpError); ok && e.Timeout() {
				continue
			}
			log.Errorf("accept failed: %v\n", err)
			continue
		}
		/*
			// first packet must sent by client in specified seconds
			if err = conn.SetReadDeadline(time.Now().Add(20)); err != nil {
				glog.Errorf("conn.SetReadDeadLine() error(%v)", err)
				conn.Close()
				continue
			}*/
		go this.handleConnection(conn)
	}
}
开发者ID:houcy,项目名称:push,代码行数:44,代码来源:server.go

示例12: Start

func (this *Server) Start(listener *net.TCPListener) {
	log.Printf("Start listen on %v\r\n", listener.Addr())
	this.waitGroup.Add(1)
	defer func() {
		listener.Close()
		this.waitGroup.Done()
	}()

	// 防止恶意连接
	go this.dealSpamConn()

	// report记录,定时发送邮件
	go report.Work()

	for {
		select {
		case <-this.exitCh:
			log.Printf("Stop listen on %v\r\n", listener.Addr())
			return
		default:
		}

		listener.SetDeadline(time.Now().Add(this.acceptTimeout))
		conn, err := listener.AcceptTCP()

		if err != nil {
			if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
				// log.Printf("Accept timeout: %v\r\n", opErr)
				continue
			}
			report.AddCount(report.TryConnect, 1)
			log.Printf("Accept error: %v\r\n", err)
			continue
		}

		report.AddCount(report.SuccessConnect, 1)

		// 连接后等待登陆验证
		handlers.ConnMapLoginStatus.Set(conn, time.Now())
		log.Printf("Accept: %v\r\n", conn.RemoteAddr())

		go this.handleClientConn(conn)
	}
}
开发者ID:jackwanger,项目名称:chatserver,代码行数:44,代码来源:server.go

示例13: acceptor

// Accepts inbound connections till the service is terminated. For each one it
// starts a new handler and hands the socket over.
func (r *Relay) acceptor(listener *net.TCPListener) {
	// Accept connections until termination request
	var errc chan error
	for errc == nil {
		select {
		case errc = <-r.quit:
			break
		case client := <-r.done:
			// A client terminated, remove from active list
			delete(r.clients, client)
			if err := client.report(); err != nil {
				log.Printf("relay: closing client error: %v.", err)
			}
		default:
			// Accept an incoming connection but without blocking for too long
			listener.SetDeadline(time.Now().Add(acceptPollRate))
			if sock, err := listener.Accept(); err == nil {
				if rel, err := r.acceptRelay(sock); err != nil {
					log.Printf("relay: accept failed: %v.", err)
				} else {
					r.clients[rel] = struct{}{}
				}
			} else if !err.(net.Error).Timeout() {
				log.Printf("relay: accept failed: %v, terminating.", err)
			}
		}
	}
	// In case of failure, wait for termination request
	if errc == nil {
		errc = <-r.quit
	}
	// Forcefully close all active client connections
	for rel, _ := range r.clients {
		rel.drop()
		<-r.done
	}
	for rel, _ := range r.clients {
		rel.report()
	}
	// Clean up and report
	errc <- listener.Close()
}
开发者ID:simia-tech,项目名称:iris,代码行数:44,代码来源:service.go

示例14: serve

func serve(listener *net.TCPListener) (err error) {
	for {
		select {
		default:
			listener.SetDeadline(time.Now().Add(1e9 * time.Nanosecond))
			var c *net.TCPConn
			c, err = listener.AcceptTCP()
			if err != nil {
				if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
					continue
				}
				log.WithField("error", err).Warn("AcceptTCP")
				return
			}
			log.WithField("client", c.RemoteAddr()).Debug("Connected")
			go func(c *net.TCPConn) {
				handleConnection(c)
			}(c)
		}
	}
}
开发者ID:the-control-group,项目名称:nats-publisher,代码行数:21,代码来源:main.go

示例15: Run

// Run opens the server socket and runs the telnet server which spawns go
// routines for every connecting client.
func (s *Server) Run(listener *net.TCPListener) error {
	// we want to close the listener when we'll be quitting
	s.waitGroup.Add(1)
	defer func() {
		listener.Close()
		// wait that all opened goroutines are closed
		s.waitGroup.Done()
	}()

	for {
		// loop forever...
		select {
		case <-s.quitChan:
			// ...until the server quits
			return nil

		default:
		}

		acceptTimeout := time.Second

		// listening for incoming TCP connections
		listener.SetDeadline(time.Now().Add(acceptTimeout))
		conn, err := listener.AcceptTCP()
		if err != nil {
			// wrong connection, leave it
			continue
		}

		s.waitGroup.Add(1)
		go func() {
			c := newClient(conn, s.prompt, s.cmdHandler, s.userdata)
			c.handle()
			s.waitGroup.Done()
		}()
		// start a new goroutine, one more to wait for when we'll be quitting
	}
}
开发者ID:RookieGameDevs,项目名称:surviveler,代码行数:40,代码来源:telgo.go


注:本文中的net.TCPListener.SetDeadline方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。