Ejemplo 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()
}
Ejemplo 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
		}
	}
}