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


Golang PacketStd.Name方法代码示例

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


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

示例1: pullHandler

func (fs *FileSession) pullHandler(incomingPacket packet.PacketStd) {

	if incomingPacket.Name == "REQ_FILELIST" {
		//db initializing
		db, error := sql.Open("sqlite3", "../gypo.db")

		//tells client pull request recieved
		newPacket := packet.PacketStd{packet.Packet{"pull", "REQ_FILELIST_RECV"}, ""}
		go newPacket.SendPacket(fs.Connection)

		//creates list of server files and sends back to client
		if error != nil {
			fmt.Println(error.Error())
		} else {
			//PacketList instance for server files(db)
			var server_fileList packet.PacketList

			//get number of files from db to use for file server_fileList.Data size
			var dbFileCount int
			error = db.QueryRow("SELECT count(*) AS number FROM files WHERE user_id = ?", fs.User.Id).Scan(&dbFileCount)

			if error != nil {
				fmt.Println(error.Error())
			} else {
				//sets PacketList instance variables
				server_fileList.Data = make([]packet.PacketStd, dbFileCount)
				server_fileList.ContentType = "pull"
				server_fileList.Name = "RESP_FILELIST"

				//get fileList from db
				rows, error := db.Query("SELECT filename, path, hash, is_dir FROM files WHERE user_id = ?", fs.User.Id)

				if error != nil {
					fmt.Println(error.Error())
				} else {
					var rowPacket packet.PacketStd
					var filename, path, hash string
					var is_dir int
					i := 0

					for rows.Next() {
						error = rows.Scan(&filename, &path, &hash, &is_dir)
						if error != nil {
							fmt.Println(error.Error())
						} else {
							if is_dir == 1 {
								rowPacket.ContentType = "folder"
								rowPacket.Name = path
								rowPacket.Data = ""
							} else {
								rowPacket.ContentType = "hash"
								rowPacket.Name = path + filename
								rowPacket.Data = hash
							}
						}
						server_fileList.Data[i] = rowPacket
						i++
					}
					go server_fileList.SendPacket(fs.Connection)
					fmt.Println("server_fileList size: ", len(server_fileList.Data))
				}
			}
		}
	} else if incomingPacket.Name == "REQ_FILE" {
		//wraps file in packet and sends it
		newPacket := packet.PacketStd{packet.Packet{"pull", "RESP_FILE"}, incomingPacket.Data}
		go newPacket.SendFilePacket(fs.Connection)
	}

}
开发者ID:Eriken,项目名称:gypo,代码行数:70,代码来源:session.go

示例2: pushHandler

func (fs *FileSession) pushHandler(incomingPacket packet.PacketStd) {

	fmt.Println("processing push")

	//getting pushList from socket
	incomingBytes, _ := strconv.Atoi(incomingPacket.Data)
	pushListB := make([]byte, incomingBytes)
	i := 0
	for i < incomingBytes {
		chunkSize, _ := fs.Connection.Read(pushListB[i:])
		i = i + chunkSize
		fmt.Println("read so far: ", i)
	}

	//unmarshal data from socket to PacketList instance
	var client_pushList packet.PacketList
	client_pushList.ContentType = "push"
	client_pushList.Name = "client"
	flsize, _ := strconv.Atoi(incomingPacket.Name)
	flObj := make([]packet.PacketStd, flsize)
	client_pushList.Data = flObj
	error := json.Unmarshal(pushListB, &client_pushList)

	if error == nil {
		fmt.Println("push recieved, " + incomingPacket.Name + " entries\n")
	} else {
		fmt.Println(error.Error())
	}

	//tells client push was recieved
	newPacket := packet.PacketStd{packet.Packet{"push", ""}, "PUSH_RECIEVED"}
	go newPacket.SendPacket(fs.Connection)

	//PacketList instance for server files(db)
	var server_fileList packet.PacketList

	db, error := sql.Open("sqlite3", "../gypo.db")

	if error != nil {
		fmt.Println(error.Error())
	} else {
		//get number of files from db to use for file array size
		var dbFileCount int
		error = db.QueryRow("SELECT count(*) AS number FROM files WHERE user_id = ?", fs.User.Id).Scan(&dbFileCount)

		if error != nil {
			fmt.Println(error.Error())
		} else {
			server_fileList.Data = make([]packet.PacketStd, dbFileCount)

			//fetches files from db and adds them to server_fileList
			rows, error := db.Query("SELECT filename, path, hash, is_dir FROM files WHERE user_id = ?", fs.User.Id)
			if error != nil {
				fmt.Println(error.Error())
			} else {
				var is_dir int
				var rowContentType, rowName, rowData string
				var rowPacket packet.PacketStd

				//populates server_fileList.Data
				i = 0
				for rows.Next() {
					error = rows.Scan(&rowContentType, &rowName, &rowData, &is_dir)
					//fmt.Println(i, " | ", is_dir)
					if error != nil {
						fmt.Println(error.Error())
					}
					if is_dir == 1 {
						rowPacket.ContentType = "folder"
						rowPacket.Name = rowName
						rowPacket.Data = ""
					} else {
						rowPacket.ContentType = "hash"
						rowPacket.Name = rowName + rowContentType
						rowPacket.Data = rowData
					}
					server_fileList.Data[i] = rowPacket
					i++
				}

				fmt.Println("server_fileList size: ", len(server_fileList.Data))
				fmt.Println("client_pushList size: ", len(client_pushList.Data))

				/*
				   i = 0
				   for i < len(server_fileList.Data) {
				       fmt.Println("ContentType: " + server_fileList.Data[i].ContentType + " | Name: " + server_fileList.Data[i].Name + " | Data: " + server_fileList.Data[i].Data)
				       i++
				   }

				   fmt.Println("\n\n\n\n")

				   i = 0
				   for i < len(client_pushList.Data) {
				       fmt.Println("ContentType: " + client_pushList.Data[i].ContentType + " | Name: " + client_pushList.Data[i].Name + " | Data: " + server_fileList.Data[i].Data)
				       i++
				   }*/

				//compares client_pushList to server_fileList
				nilPacket := packet.PacketStd{packet.Packet{"", ""}, ""} //to flag list entries as solved
//.........这里部分代码省略.........
开发者ID:Eriken,项目名称:gypo,代码行数:101,代码来源:session.go


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