コード例 #1
0
func main() {

	if len(os.Args) < 3 {
		fmt.Printf("usage ./transferclient <filename> <savefilename\n")
		return
	}
	service := "127.0.0.1:8010"
	tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
	if err != nil {
		fmt.Printf("ResolveTCPAddr")
	}
	conn, err := net.DialTCP("tcp", nil, tcpAddr)
	if err != nil {
		fmt.Printf("DialTcp error,%s\n", err)
	} else {
		session := socket.NewTcpSession(conn)
		fmt.Printf("connect sucessful\n")
		//发出文件请求
		wpk := packet.NewWPacket(packet.NewByteBuffer(64))
		wpk.PutUint16(request_file)
		wpk.PutString(os.Args[1])
		session.Send(wpk)
		tsession := &transfer_session{filename: os.Args[2]}
		session.SetUd(tsession)
		socket.ProcessSession(session, packet.NewRPacketDecoder(65535), process_client)
	}
}
コード例 #2
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()
}
コード例 #3
0
ファイル: testpacket.go プロジェクト: xiaobodu/kendynet-go
func main() {

	wpk := packet.NewWPacket(packet.NewByteBuffer(64))
	wpk.PutString("中国")
	wpk.PutUint32(100)
	wpk.PutString("韶关")

	rpk1 := (*packet.RPacket)(unsafe.Pointer(wpk.MakeRead()))

	fmt.Printf("----------------rpk1-----------------\n")

	str, _ := rpk1.String()
	fmt.Printf("%s\n", str)

	rpk2 := (*packet.RPacket)(unsafe.Pointer(rpk1.Clone()))

	v, _ := rpk1.Uint32()
	fmt.Printf("%d\n", v)

	str, _ = rpk1.String()
	fmt.Printf("%s\n", str)

	fmt.Printf("----------------rpk2-----------------\n")

	v, _ = rpk2.Uint32()
	fmt.Printf("%d\n", v)

	str, _ = rpk2.String()
	fmt.Printf("%s\n", str)

}
コード例 #4
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
		}
	}
}
コード例 #5
0
ファイル: broadcli.go プロジェクト: xiaobodu/kendynet-go
func main() {
	var i uint32
	for i = 1; i <= 100; i++ {
		service := "127.0.0.1:8010"
		tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
		if err != nil {
			fmt.Printf("ResolveTCPAddr")
		}
		conn, err := net.DialTCP("tcp", nil, tcpAddr)
		if err != nil {
			fmt.Printf("DialTcp error,%s\n", err)
		} else {
			session := socket.NewTcpSession(conn)
			wpk := packet.NewWPacket(packet.NewByteBuffer(uint32(64)))
			wpk.PutUint32(uint32(i))
			session.Send(wpk)
			idx := i
			go socket.ProcessSession(session, packet.NewRPacketDecoder(1024),
				func(session *socket.Tcpsession, rpk packet.Packet, errno error) {
					if rpk == nil {
						fmt.Printf("error:%s\n", errno)
						session.Close()
						return
					}
					r := rpk.(*packet.RPacket)
					id, _ := r.Uint32()
					if id == idx {
						session.Send(r.MakeWrite())
					}
				})
		}
	}

	for {
		time.Sleep(10000000)
	}
}