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


Golang PacketStd.SendFilePacket方法代码示例

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


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


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