func (this *NetworkConnector) NetworkConnectTo(raddr string) bool {
	tcpaddr, err := net.ResolveTCPAddr("tcp", raddr)
	if err != nil {
		zwflog.LogError("resolve tcp address error! Desc:", err.Error())
		return false
	}
	this.remoteAddr = raddr

	conn, connerr := net.DialTCP("tcp", nil, tcpaddr)
	if connerr != nil {
		this.OnConnectError()
		zwflog.LogError("connect to %s failed! Desc:%s", tcpaddr.String(), connerr.Error())
		return false
	}
	if this.connector != nil {
		this.connector.Close()
	}
	this.connector = conn
	zwflog.LogTrace("connect to %s success.", raddr)

	defer this.networkConnectorClose()

	this.startKeepAliveTimer()

	this.networkConnectorStartRead()

	return true

}
// Attention:
// connector.Read() is Block-IO, so if protocol doesn't
// meet principle, read function  may be blocked until disconnection
func (this *NetworkConnector) networkConnectorStartRead() {
	if !this.isNetworkConnectorInit() {
		zwflog.LogError("connetor is not initialized!")
		return
	}

	reader := bufio.NewReader(this.connector)
	if reader == nil {
		zwflog.LogError("new reader failed!")
		return
	}

	defer this.networkConnectorClose()

	msg := new(message.Message)
	if msg == nil {
		zwflog.LogError("new meesage.Message obj failed!")
		return
	}

	buf := make([]byte, readBufSize)
	msgbody := bytes.NewBuffer(nil)
	headSize := message.BodySizeBytes + message.FlagsBytes + message.CmdBytes + message.UidBytes
	for {
		// read head
		_, err := this.connector.Read(buf[0:headSize])
		if err != nil {
			this.OnReadError()
			zwflog.LogError("network read error! Desc:", err.Error())
			return
		}
		msg.ParseHead(buf)

		// read body
		msgbody.Reset()
		leftBodyLen := msg.Head.BodySize
		for leftBodyLen > 0 {
			n, err := this.connector.Read(buf[0:])
			if err != nil {
				this.OnReadError()
				zwflog.LogError("network read error! Desc:", err.Error())
				return
			}

			leftBodyLen = leftBodyLen - uint32(n)
			msgbody.Write(buf[0:n])
		}
		msg.ParseBody(msgbody.Bytes())

		msg.ShowMessageDetail()
	}
}
Esempio n. 3
0
// clear resource
// this will be called if server receive os.Kill
func (this *Server) closeServer() {
	if this.listener != nil {
		this.listener.NetworkListenerClose()
	}

	zwflog.LogError("%s is closed!", this.ServerName)
}
Esempio n. 4
0
func (this *Server) RunServer() {
	if !this.initServer() {
		return
	}

	// TODO: run service at routine
	//this.listener = network.NewNetworkListener("127.0.0.1", 9100)
	//if this.listener == nil {
	//zwflog.LogError("new network listenr failed!")
	//return
	//}

	//go this.listener.NetworkListenerRun()

	this.connector = network.NewNetworkConnector(nil, true, "")
	if this.connector == nil {
		zwflog.LogError("new network connector failed!")
		return
	}
	go this.connector.NetworkConnectTo("127.0.0.1:9000")

	zwflog.LogInfo("%s is running....", this.ServerName)

	this.waitToExit()
}
Esempio n. 5
0
// "ini" file as default
func NewConfigReader(path string) *ConfigReader {
	iniconf, err := config.NewConfig("ini", path)
	if err != nil {
		zwflog.LogError("New config reader failed! Desc:%s", err.Error())
		return nil
	}

	return &ConfigReader{iniconf}
}
func (this *NetworkListener) NetworkListenerRun() bool {
	addr := fmt.Sprintf("%s:%d", this.ip, this.port)
	tcpaddr, err := net.ResolveTCPAddr("tcp", addr)
	if err != nil {
		zwflog.LogError("resolve tcp addr failed!")
		return false
	}

	lis, liserr := net.ListenTCP("tcp", tcpaddr)
	if liserr != nil {
		zwflog.LogError("listen %s:%d failed! Desc:%s", this.ip, this.port, liserr.Error())
		return false
	}
	this.listener = lis

	defer this.NetworkListenerClose()

	for {
		conn, conerr := this.listener.AcceptTCP()
		if conerr != nil {
			zwflog.LogError("accept error! Desc:", conerr.Error())
			continue
		}

		remoteAddr := conn.RemoteAddr()
		zwflog.LogTrace("client[%s] connected", remoteAddr.String())

		connector := NewNetworkConnector(conn, false, remoteAddr.String())
		if connector == nil {
			zwflog.LogError("new network connector failed")
			continue
		}
		go connector.networkConnectorStartRead()

		// TODO: start keep-alive timer

	}

	return true
}
Esempio n. 7
0
func (this *Server) initServer() bool {
	if !zwflog.InitLog() {
		fmt.Println("initServer failed!")
		return false
	}

	common.Inicfg = zwfconfig.NewConfigReader(common.IniPath)
	if common.Inicfg == nil {
		zwflog.LogError("read ini configuration failed!")
		return false
	}

	// TODO: regist msg handler

	return true
}