not working dunno why

This commit is contained in:
Zack Scholl 2018-09-24 07:51:24 -07:00
parent 55b1fa35e4
commit 02f4b08929
4 changed files with 36 additions and 16 deletions

View File

@ -3,6 +3,7 @@ package comm
import (
"bytes"
"fmt"
"log"
"net"
"strconv"
"strings"
@ -39,20 +40,33 @@ func (c Comm) Write(b []byte) (int, error) {
func (c Comm) Read() (buf []byte, numBytes int, bs []byte, err error) {
// read until we get 5 bytes
bs = make([]byte, 5)
_, err = c.connection.Read(bs)
tmp := make([]byte, 5)
n, err := c.connection.Read(tmp)
if err != nil {
return
}
tmp := make([]byte, 1)
tmpCopy := make([]byte, n)
// Copy the buffer so it doesn't get changed while read by the recipient.
copy(tmpCopy, tmp[:n])
bs = tmpCopy
tmp = make([]byte, 1)
for {
// see if we have enough bytes
bs = bytes.Trim(bs, "\x00")
if len(bs) == 5 {
break
}
c.connection.Read(tmp)
bs = append(bs, tmp...)
n, err := c.connection.Read(tmp)
if err != nil {
return nil, 0, nil, err
}
tmpCopy = make([]byte, n)
// Copy the buffer so it doesn't get changed while read by the recipient.
copy(tmpCopy, tmp[:n])
bs = append(bs, tmpCopy...)
}
numBytes, err = strconv.Atoi(strings.TrimLeft(string(bs), "0"))
if err != nil {
return nil, 0, nil, err
@ -61,20 +75,27 @@ func (c Comm) Read() (buf []byte, numBytes int, bs []byte, err error) {
tmp = make([]byte, numBytes)
bufStart := 0
for {
_, err = c.connection.Read(tmp)
n, err := c.connection.Read(tmp)
if err != nil {
return nil, numBytes, bs, err
return nil, 0, nil, err
}
tmp = bytes.TrimRight(tmp, "\x00")
copy(buf[bufStart:bufStart+len(tmp)], tmp[:])
tmpCopy := make([]byte, n)
// Copy the buffer so it doesn't get changed while read by the recipient.
copy(tmpCopy, tmp[:n])
tmpCopy = bytes.TrimSpace(tmpCopy)
tmpCopy = bytes.Replace(tmpCopy, []byte(" "), []byte{}, -1)
tmpCopy = bytes.Trim(tmpCopy, "\x00")
copy(buf[bufStart:bufStart+len(tmpCopy)], tmpCopy[:])
bufStart = len(buf)
if bufStart < numBytes {
// shrink the amount we need to read
tmp = tmp[:numBytes-bufStart]
} else {
break
}
}
// log.Printf("wanted %d and got %d", numBytes, len(buf))
log.Printf("wanted %d and got %d", numBytes, len(buf))
return
}

View File

@ -47,7 +47,7 @@ func TestSendReceiveWebsockets(t *testing.T) {
sendAndReceive(t, 1)
}
func TestSendReceiveTCP(t *testing.T) {
sendAndReceive(t, 1)
sendAndReceive(t, 2)
}
func generateRandomFile(megabytes int) (fname string) {

View File

@ -207,6 +207,7 @@ func send(forceSend int, serverAddress, serverTCP string, isLocal bool, c *webso
return errors.New("recipient refused file")
}
buffer := make([]byte, models.WEBSOCKET_BUFFER_SIZE/8)
if !useWebsockets {
// connection to TCP
tcpConnection, err = connectToTCPServer(utils.SHA256(fmt.Sprintf("%x", sessionKey)), serverAddress+":"+serverTCP)
@ -214,15 +215,13 @@ func send(forceSend int, serverAddress, serverTCP string, isLocal bool, c *webso
log.Error(err)
return
}
buffer = make([]byte, models.TCP_BUFFER_SIZE/2)
}
fmt.Fprintf(os.Stderr, "\rSending (->%s)...\n", otherIP)
// send file, compure hash simultaneously
startTransfer = time.Now()
buffer := make([]byte, models.WEBSOCKET_BUFFER_SIZE/8)
if !useWebsockets {
buffer = make([]byte, models.TCP_BUFFER_SIZE/2)
}
bar := progressbar.NewOptions(
int(fstats.Size),
progressbar.OptionSetRenderBlankState(true),

View File

@ -126,7 +126,7 @@ func chanFromConn(conn net.Conn) chan []byte {
c := make(chan []byte)
go func() {
b := make([]byte, models.WEBSOCKET_BUFFER_SIZE)
b := make([]byte, models.TCP_BUFFER_SIZE)
for {
n, err := conn.Read(b)