Ejemplo n.º 1
0
func (server *ConnectionServer) startListener(workerManager *coresupport.CoreWorkerManager) {
	if server.GetStatus() != ServerStatusOn {
		server.SetStatus(ServerStatusOn)
		options := (*server).option
		socket := options.Socket()
		listener, err := net.Listen("tcp", socket)
		if err == nil {
			defer listener.Close()
			defer (*server).connectionDataManager.Close()
			for server.isAcceptForConnection() {
				newConnection, err := listener.Accept()
				if err != nil {
					rllogger.Outputf(
						rllogger.LogTerminate, "Can't create connection to %s error: %s", socket, err)
					server.stat.SendMsg("lost_connection_count", 1)
				} else {
					clientAddr := fmt.Sprintf("connection:%s", newConnection.RemoteAddr())
					rllogger.Outputf(rllogger.LogDebug, "new %s", clientAddr)
					tcpConnection := newConnection.(*net.TCPConn)
					tcpConnection.SetKeepAlive(true)
					tcpConnection.SetKeepAlivePeriod(
						time.Duration(1+options.StatusCheckPeriod) * time.Second)
					server.stat.SendMsg("connection_count", 1)
					go server.connectionReadProcessing(
						newConnection,
						workerManager,
						clientAddr)
				}
			}
		} else {
			rllogger.Outputf(rllogger.LogTerminate, "Can't start server at %s error: %s", socket, err)
		}
	}
}
Ejemplo n.º 2
0
func upConnection(
	server *RlServer,
	toCoreBuffer *chan protocol.CoreMsg,
	answerDispatcher *AnswerDispatcher,
	statistic *RlStatistic) {
	// connections
	options := server.GetOption()
	socket := options.Socket()
	listener, err := net.Listen("tcp", socket)
	if err != nil {
		rllogger.Outputf(
			rllogger.LogTerminate,
			"Can't start server at %s error: %s",
			socket, err)
	}
	defer listener.Close()
	for server.IsActive() {
		newConnection, err := listener.Accept()
		if err != nil {
			rllogger.Outputf(
				rllogger.LogTerminate,
				"Can't start server at %s error: %s",
				socket, err)
		} else {
			clientAddr := fmt.Sprintf("connection:%s", newConnection.RemoteAddr())
			rllogger.Outputf(rllogger.LogInfo, "new %s", clientAddr)
			statistic.Append(StatConnectionCount, 1)
			server.ChangeConnectionCount(1)
			tcpConnection := newConnection.(*net.TCPConn)
			tcpConnection.SetKeepAlive(true)
			tcpConnection.SetKeepAlivePeriod(
				time.Duration(1+options.StatusCheckPeriod) * time.Second)
			//
			go connectionHandler(
				server,
				toCoreBuffer,
				newConnection,
				clientAddr,
				answerDispatcher,
				statistic)
		}
	}
}