mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
Proper abstraction of virtualPacketConn. It now supports other transport protocol encodings.
This commit is contained in:
@@ -240,7 +240,7 @@ func SendChatAll(text string) {
|
||||
// cmdTransfer handles an incoming transfer message
|
||||
func (peer *PeerInfo) cmdTransfer(msg *protocol.MessageTransfer, connection *Connection) {
|
||||
// Only UDT protocol is currently supported for file transfer.
|
||||
if msg.TransferProtocol != 0 {
|
||||
if msg.TransferProtocol != protocol.TransferProtocolUDT {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,9 @@ func (peer *PeerInfo) startFileTransferUDT(hash []byte, fileSize uint64, offset,
|
||||
limit = fileSize - offset
|
||||
}
|
||||
|
||||
virtualConnection := newVirtualPacketConn(peer, 0, hash, offset, limit)
|
||||
virtualConnection := newVirtualPacketConn(peer, func(data []byte, sequenceNumber uint32) {
|
||||
peer.sendTransfer(data, protocol.TransferControlActive, 0, hash, offset, limit, sequenceNumber)
|
||||
})
|
||||
|
||||
// register the sequence since packets are sent bi-directional
|
||||
virtualConnection.sequenceNumber = sequenceNumber
|
||||
@@ -67,7 +69,9 @@ func (peer *PeerInfo) startFileTransferUDT(hash []byte, fileSize uint64, offset,
|
||||
// The caller must call udtConn.Close() when done. Do not use any of the closing functions of virtualConn.
|
||||
// Limit is optional. 0 means the entire file.
|
||||
func (peer *PeerInfo) FileTransferRequestUDT(hash []byte, offset, limit uint64) (udtConn net.Conn, virtualConn *virtualPacketConn, err error) {
|
||||
virtualConn = newVirtualPacketConn(peer, 0, hash, offset, limit)
|
||||
virtualConn = newVirtualPacketConn(peer, func(data []byte, sequenceNumber uint32) {
|
||||
peer.sendTransfer(data, protocol.TransferControlActive, protocol.TransferProtocolUDT, hash, offset, limit, sequenceNumber)
|
||||
})
|
||||
|
||||
// new sequence
|
||||
sequence := networks.Sequences.NewSequenceBi(peer.PublicKey, &peer.messageSequence, virtualConn, transferSequenceTimeout, virtualConn.sequenceTerminate)
|
||||
@@ -84,7 +88,7 @@ func (peer *PeerInfo) FileTransferRequestUDT(hash []byte, offset, limit uint64)
|
||||
udtListener := udt.ListenUDT(udtConfig, virtualConn, virtualConn.incomingData, virtualConn.outgoingData, virtualConn.terminationSignal)
|
||||
|
||||
// request file transfer
|
||||
peer.sendTransfer(nil, protocol.TransferControlRequestStart, virtualConn.transferProtocol, hash, offset, limit, virtualConn.sequenceNumber)
|
||||
peer.sendTransfer(nil, protocol.TransferControlRequestStart, protocol.TransferProtocolUDT, hash, offset, limit, virtualConn.sequenceNumber)
|
||||
|
||||
// accept the connection
|
||||
udtConn, err = udtListener.Accept()
|
||||
|
||||
@@ -11,19 +11,14 @@ package core
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/PeernetOfficial/core/protocol"
|
||||
)
|
||||
|
||||
// virtualPacketConn is a virtual connection.
|
||||
type virtualPacketConn struct {
|
||||
peer *PeerInfo
|
||||
|
||||
// Transfer settings
|
||||
transferProtocol uint8 // 0 = UDT
|
||||
hash []byte // The requested hash.
|
||||
offset uint64 // Offset to start reading.
|
||||
limit uint64 // Limit of bytes to read at the offset.
|
||||
// function to send data to the remote peer
|
||||
sendData func(data []byte, sequenceNumber uint32)
|
||||
|
||||
// Sequence number from the first outgoing or incoming packet.
|
||||
sequenceNumber uint32
|
||||
@@ -40,13 +35,10 @@ type virtualPacketConn struct {
|
||||
}
|
||||
|
||||
// newVirtualPacketConn creates a new virtual connection (both incomign and outgoing).
|
||||
func newVirtualPacketConn(peer *PeerInfo, protocol uint8, hash []byte, offset, limit uint64) (v *virtualPacketConn) {
|
||||
func newVirtualPacketConn(peer *PeerInfo, sendData func(data []byte, sequenceNumber uint32)) (v *virtualPacketConn) {
|
||||
v = &virtualPacketConn{
|
||||
peer: peer,
|
||||
transferProtocol: protocol,
|
||||
hash: hash,
|
||||
offset: offset,
|
||||
limit: limit,
|
||||
sendData: sendData,
|
||||
incomingData: make(chan []byte, 100),
|
||||
outgoingData: make(chan []byte),
|
||||
terminationSignal: make(chan struct{}),
|
||||
@@ -62,7 +54,7 @@ func (v *virtualPacketConn) writeForward() {
|
||||
for {
|
||||
select {
|
||||
case data := <-v.outgoingData:
|
||||
v.peer.sendTransfer(data, protocol.TransferControlActive, v.transferProtocol, v.hash, v.offset, v.limit, v.sequenceNumber)
|
||||
v.sendData(data, v.sequenceNumber)
|
||||
|
||||
case <-v.terminationSignal:
|
||||
return
|
||||
|
||||
@@ -8,6 +8,14 @@ Offset Size Info
|
||||
0 1 Control
|
||||
1 1 Transfer Protocol
|
||||
2 32 File Hash
|
||||
|
||||
Control = 0: Request Start
|
||||
34 8 Offset to start reading in the file
|
||||
42 8 Limit of bytes to read at the offset
|
||||
|
||||
Offset + limit must not exceed the file size.
|
||||
|
||||
Control = 3: Active
|
||||
34 ? Embedded protocol data
|
||||
|
||||
*/
|
||||
@@ -40,6 +48,8 @@ const (
|
||||
TransferControlTerminate = 3 // Terminate
|
||||
)
|
||||
|
||||
const TransferProtocolUDT = 0 // Indicates that UDT is used as embedded transfer protocol.
|
||||
|
||||
const transferPayloadHeaderSize = 34
|
||||
|
||||
// DecodeTransfer decodes a transfer message
|
||||
|
||||
Reference in New Issue
Block a user