예제 #1
0
func connectServer() {
	//接通
	conn, err := net.Dial("tcp", "localhost:8260")
	defer func() {
		conn.Close()
	}()

	if err != nil {
		glog.Errorln("Escort slave can't connect to master!")
		netErrCh <- 1
		//return new(NetConnectErr)
	} else {
		glog.Infoln("Escort slave connected successfully!")
		// 定时发送ping
		go func() {
			timer := time.NewTicker(10 * time.Second)
			for {
				select {
				case <-timer.C:
					if err := ping(conn); err != nil {
						glog.Errorf("[ERROR] Escort slave send ping err: %s", err.Error())
						// ping发生错误,切换
						doSwitch()
					}
				}
			}
		}()

		handler(conn)
	}
}
예제 #2
0
func ping(conn net.Conn) error {
	select {
	case pingAtCh <- 1:
		glog.Infoln("Escort slave send ping!")
	default:
		// 没有收到上一次ping的pong,超时
		glog.Warningln("Escort slave doesn't get pong!")
		return new(PingTimeOutErr)
	}

	pingcmd := new(Packet)
	pingcmd.Version = V1
	pingcmd.Flags = FLAG_REQUEST
	pingcmd.Length = 0
	pingcmd.Sequence = 0

	data, err := pingcmd.Encode()
	_, err = conn.Write(data)
	if err != nil {
		glog.Errorf("[ERROR] %s", err.Error())
		return err
	}

	return nil
}
예제 #3
0
파일: sdk.go 프로젝트: jiedo/slingo
func is_channel_ok(command_chan chan ui.Command, name string, function_name string) bool {
	if command_chan == nil {
		glog.Errorf("AI[%s] want %s, but command channel is nil.",
			name, function_name)
		return false
	}
	return true
}
예제 #4
0
func ReadFromConn(conn net.Conn) ([]byte, error) {
	buf := make([]byte, 1024)
	_, err := conn.Read(buf)
	if err != nil {
		glog.Errorf("[ERROR] %s", err.Error())
		return nil, err
	}

	return buf, nil
}
예제 #5
0
func startServer() {
	listener, err := net.Listen("tcp", "localhost:8260")
	if err != nil {
		glog.Errorf("[ERROR] %s", err.Error())
		os.Exit(1)
	}

	glog.Infoln("Escort server is up!")

	for {
		conn, err := listener.Accept()
		if err != nil {
			glog.Errorf("[ERROR] %s", err.Error())
			continue
		}

		go doService(conn)
	}
}
예제 #6
0
func doService(conn net.Conn) {
	glog.Infoln("Escort server is connected!")
	defer func() {
		conn.Close()
	}()

	for {
		buf, err := ReadFromConn(conn)
		if err != nil {
			glog.Errorf("[ERROR] %s", err.Error())
			break
		}

		p := new(Packet)
		p.Decode(buf)

		if p.Flags == FLAG_REQUEST && p.Length == 0 { // ping命令
			glog.Infoln("Escort server get client ping cmd!")

			pong := new(Packet)
			pong.Version = V1
			pong.Flags = FLAG_RESPONSE
			pong.Sequence = 0
			pong.Length = 0

			var data []byte
			var err error
			data, err = pong.Encode()
			if err != nil {
				glog.Errorf("[ERROR] %s", err.Error())
				continue
			}
			_, err = conn.Write(data)
			if err == nil {
				glog.Infoln("Escort server send pong successfully!")
			} else {
				glog.Errorf("[ERROR] %s", err.Error())
			}
		}
	}

}
예제 #7
0
/**
 * 处理从对端获取的响应或请求
 **/
func handler(conn net.Conn) {
	glog.Infoln("Escort slave handler")
	for {
		data, err := ReadFromConn(conn)
		if err != nil {
			glog.Errorf("[ERROR] %s", err.Error())
			break
		} else {
			packet := new(Packet)
			packet.Decode(data)

			if packet.Flags == FLAG_REQUEST {
				// master 的请求
			} else {
				// master 的响应
				if packet.Length == 0 {
					// pong
					pong()
				}
			}
		}
	}

}
예제 #8
0
func CheckError(err error) {
	if err != nil {
		glog.Errorf("[ERROR] %s", err.Error())
	}
}