Esempio n. 1
0
func process_client(session *socket.Tcpsession, p packet.Packet, _ error) {
	rpk := p.(*packet.RPacket)
	cmd, _ := rpk.Uint16()
	if cmd == request_file {
		if session.Ud() != nil {
			fmt.Printf("already in transfer session\n")
		} else {
			filename, _ := rpk.String()
			filecontent := filemap[filename]
			if filecontent == nil {
				fmt.Printf("%s not found\n", filename)
				session.Close()
			} else {
				fmt.Printf("request file %s\n", filename)
				tsession := &transfer_session{filecontent: filecontent, ridx: 0}
				session.SetUd(tsession)
				wpk := packet.NewWPacket(packet.NewByteBuffer(64))
				wpk.PutUint16(file_size)
				wpk.PutUint32(uint32(len(filecontent)))
				if nil == session.Send(wpk) {
					tsession.send_file(session)
				}
			}
		}
	}
	session.Close()
}
Esempio n. 2
0
func (this *transfer_session) send_file(session *socket.Tcpsession) {
	for {
		remain := len(this.filecontent) - this.ridx
		sendsize := 0
		if remain >= 16000 {
			sendsize = 16000
		} else {
			sendsize = remain
		}
		wpk := packet.NewWPacket(packet.NewByteBuffer(uint32(sendsize)))
		wpk.PutUint16(transfering)
		wpk.PutBinary(this.filecontent[this.ridx : this.ridx+sendsize])
		if nil == session.Send(wpk) {
			this.ridx += sendsize
			if this.ridx >= len(this.filecontent) {
				break
			}
		} else {
			break
		}
	}
}
Esempio n. 3
0
func process_client(session *socket.Tcpsession, p packet.Packet, _ error) {
	rpk := p.(*packet.RPacket)
	cmd, _ := rpk.Uint16()
	if cmd == file_size {
		if session.Ud() == nil {
			fmt.Printf("error\n")
			session.Close()
			return
		}
		tsession := session.Ud().(*transfer_session)
		filesize, _ := rpk.Uint32()
		fmt.Printf("file size:%d\n", filesize)
		tsession.widx = 0
		tsession.filesize = int(filesize)
		tsession.filecontent = make([]byte, filesize)

	} else if cmd == transfering {
		if session.Ud() == nil {
			fmt.Printf("close here\n")
			session.Close()
			return
		}
		tsession := session.Ud().(*transfer_session)
		if tsession.recv_file(rpk) {
			//传输完毕
			fmt.Printf("transfer finish\n")
			session.Close()
			return
		}
	} else {
		fmt.Printf("cmd error,%d\n", cmd)
		//session.Close()
	}
}