+// WriteCounter counts the number of bytes written to it.
+type WriteCounter struct {
+ mux sync.Mutex
+ Total int64 // Total # of bytes written
+}
+
+// Write implements the io.Writer interface.
+//
+// Always completes and never returns an error.
+func (wc *WriteCounter) Write(p []byte) (int, error) {
+ wc.mux.Lock()
+ defer wc.mux.Unlock()
+ n := len(p)
+ wc.Total += int64(n)
+ return n, nil
+}
+
+func (cc *ClientConn) newFileTransfer(transferType int, fileName, filePath, size []byte) *FileTransfer {
+ var transactionRef [4]byte
+ rand.Read(transactionRef[:])
+
+ ft := &FileTransfer{
+ FileName: fileName,
+ FilePath: filePath,
+ ReferenceNumber: transactionRef[:],
+ refNum: transactionRef,
+ Type: transferType,
+ TransferSize: size,
+ ClientConn: cc,
+ bytesSentCounter: &WriteCounter{},
+ }
+
+ cc.transfersMU.Lock()
+ defer cc.transfersMU.Unlock()
+ cc.transfers[transferType][transactionRef] = ft
+
+ cc.Server.mux.Lock()
+ defer cc.Server.mux.Unlock()
+ cc.Server.fileTransfers[transactionRef] = ft
+
+ return ft
+}
+
+// String returns a string representation of a file transfer and its progress for display in the GetInfo window
+// Example:
+// MasterOfOrionII1.4.0. 0% 197.9M