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