Exemple #1
0
func ProcessConnection(conn *net.TCPConn) {
	defer logger.CatchException()

	SocketBase := network.NewSocketBase(conn)
	defer SocketBase.Close()

	SocketBase.SetNoDelay(true)                                        //无延迟
	SocketBase.SetKeepAlive(true)                                      //保持激活
	SocketBase.SetReadBuffer(64 * 1024)                                //设置读缓冲区大小
	SocketBase.SetWriteBuffer(64 * 1024)                               //设置写缓冲区大小
	SocketBase.SetReadDeadline(time.Now().Add(30000000 * time.Second)) //设置读超时

	for {
		msgs, err := SocketBase.RecvMsgs()
		if err != nil {
			logger.Errorf("SocketBase.RecvMsgs failed! err=%v", err)
			return
		}

		if len(msgs) == 0 {
			continue
		}

		for _, msg := range msgs {
			ret := network.Dispatcher(SocketBase, msg)
			if ret == ID.MESSAGE_OK {
				return
			}
		}
	}
}
Exemple #2
0
//处理连接请求
func (this *server) handlerConnection(conn *net.TCPConn) {

	defer logger.CatchException()

	logger.Infof("New connection coming ... IP=%s ", conn.RemoteAddr())

	conn.SetNoDelay(true)                                        //无延迟
	conn.SetKeepAlive(true)                                      //保持激活
	conn.SetReadBuffer(64 * 1024)                                //设置读缓冲区大小
	conn.SetWriteBuffer(64 * 1024)                               //设置写缓冲区大小
	conn.SetReadDeadline(time.Now().Add(30000000 * time.Second)) //设置读超时

	session := network.NewSession(conn)
	defer session.Close()

	for {
		msg, err := session.RecvMSG()
		if err != nil {
			logger.Infof("RecvMsgs IP=%s err=%v", conn.RemoteAddr(), err.Error())
			return
		}

		ret := this.ProcessMessage(session, msg)
		if ret == false {
			return
		}
	}
}
Exemple #3
0
func OnRun(cmd []string) {

	defer logger.CatchException()

	logger.Infof("OnRun")

	InitPacketHandler()

	//执行循环
	logger.Infof("ip = %v port = %v", config.SERVER_IP, config.SERVER_PORT)
	listen, err := net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP(config.SERVER_IP), config.SERVER_PORT, ""})
	if err != nil {
		panic(err)
	}

	for {
		conn, e := listen.AcceptTCP()
		if e != nil {
			logger.Infof("[Main] => AcceptTCP failed! err=%v", e)
			continue
		}

		//协程处理
		go Server.handlerConnection(conn)
	}
}
Exemple #4
0
func registerZKNetwork() error {
	defer logger.CatchException()

	err := zkm.Server.Register(config.SERVER_GROUP, config.SERVER_TYPE, config.SERVER_INDEX, config.SERVER_IP, config.SERVER_PORT, "", 0, "www.lanstonetech.com:8080")
	if err != nil {
		return err
	}

	return nil
}
Exemple #5
0
func Run(index int) {
	defer logger.CatchException()

	logger.Errorf("index=%v", index)
	InitLog()
	InitConf(index)
	logger.Infof("server start...")

	InitZK()

	RunHttpServer()
}
Exemple #6
0
func Run(index int) {
	defer logger.CatchException()

	logger.Errorf("index=%v", index)
	InitLog()
	InitConf(index)
	logger.Infof("server start...")

	InitZK()

	luascript.Init("LoginServer")

	HandlerPackageFunc()

	tcpaddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", config.SERVER_IP, config.SERVER_PORT))
	if err != nil {
		logger.Errorf("net.ResolveTCPAddr failed! err=%v", err)
		return
	}

	listener, err := net.ListenTCP("tcp4", tcpaddr)
	if err != nil {
		logger.Errorf("net.Listen failed! err=%v", err)
		return
	}

	for {
		conn, err := listener.AcceptTCP()
		logger.Infof("=>%v connecting...", conn.RemoteAddr().String())
		if err != nil {
			logger.Errorf("listener.Accept failed! err=%v", err)
			continue
		}

		go ProcessConnection(conn)
	}

}
Exemple #7
0
func (this *SQLServer) requestDatabaseList(observer Observer) {
	defer logger.CatchException()

	for {
		sl, _, err := zkm.ChildrenW(zkm.MakeDatabaseGroup(DB_DRIVER_POSTGRES, observer.Group()))
		if err != nil {
			logger.Errorf("zkm.ChildrenW failed! err=%v", err)
			time.Sleep(5 * time.Second)
			continue
		}

		var master []zkm.DBServerInfo
		var slaves []zkm.DBServerInfo
		var node zkm.DBServerInfo

		for _, s := range sl {
			if err := json.Unmarshal([]byte(s), &node); err != nil {
				logger.Errorf("requestDatabaseList failed! parse.err=%v", err)
				continue
			}

			if len(node.Slaveof) == 0 {
				master = append(master, node)
			} else {
				slaves = append(slaves, node)
			}
		}

		this.Lock()
		defer this.Unlock()
		this.master = master
		this.slaves = slaves

		observer.OnUpdateDatabaseList(master, slaves)
		return
	}
}