mirror of
https://github.com/schollz/croc.git
synced 2024-11-28 09:35:14 +03:00
change buffer size
This commit is contained in:
parent
b2939a0452
commit
da9634c949
@ -1,11 +1,9 @@
|
||||
package comm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/schollz/croc/src/models"
|
||||
)
|
||||
|
||||
// Comm is some basic TCP communication
|
||||
@ -24,54 +22,48 @@ func (c Comm) Connection() net.Conn {
|
||||
}
|
||||
|
||||
func (c Comm) Write(b []byte) (int, error) {
|
||||
return c.connection.Write(b)
|
||||
bs := make([]byte, 2)
|
||||
binary.LittleEndian.PutUint16(bs, uint16(len(b)))
|
||||
c.connection.Write(bs)
|
||||
n, err := c.connection.Write(b)
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (c Comm) Read() (buf []byte, err error) {
|
||||
buf = make([]byte, models.WEBSOCKET_BUFFER_SIZE)
|
||||
n, err := c.connection.Read(buf)
|
||||
buf = buf[:n]
|
||||
bs := make([]byte, 2)
|
||||
_, err = c.connection.Read(bs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
numBytes := int(binary.LittleEndian.Uint16(bs[:2]))
|
||||
buf = []byte{}
|
||||
tmp := make([]byte, numBytes)
|
||||
for {
|
||||
n, err := c.connection.Read(tmp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tmp = bytes.TrimRight(tmp, "\x00")
|
||||
buf = append(buf, tmp...)
|
||||
if n < numBytes {
|
||||
numBytes -= n
|
||||
tmp = make([]byte, numBytes)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Send a message
|
||||
func (c Comm) Send(message string) (err error) {
|
||||
message = fillString(message, models.TCP_BUFFER_SIZE)
|
||||
_, err = c.connection.Write([]byte(message))
|
||||
_, err = c.Write([]byte(message))
|
||||
return
|
||||
}
|
||||
|
||||
// Receive a message
|
||||
func (c Comm) Receive() (s string, err error) {
|
||||
messageByte := make([]byte, models.TCP_BUFFER_SIZE)
|
||||
err = c.connection.SetReadDeadline(time.Now().Add(60 * time.Minute))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = c.connection.SetDeadline(time.Now().Add(60 * time.Minute))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = c.connection.SetWriteDeadline(time.Now().Add(60 * time.Minute))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = c.connection.Read(messageByte)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
s = strings.TrimRight(string(messageByte), ":")
|
||||
b, err := c.Read()
|
||||
s = string(b)
|
||||
return
|
||||
}
|
||||
|
||||
func fillString(returnString string, toLength int) string {
|
||||
for {
|
||||
lengthString := len(returnString)
|
||||
if lengthString < toLength {
|
||||
returnString = returnString + ":"
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
return returnString
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package models
|
||||
|
||||
const WEBSOCKET_BUFFER_SIZE = 1024 * 1024 * 32
|
||||
const TCP_BUFFER_SIZE = 1024
|
||||
const TCP_BUFFER_SIZE = 1024 * 16
|
||||
|
@ -161,6 +161,7 @@ func receive(serverAddress, serverTCP string, isLocal bool, c *websocket.Conn, c
|
||||
|
||||
// connect to TCP to receive file
|
||||
if !isLocal {
|
||||
log.Debugf("connecting to server")
|
||||
tcpConnection, err = connectToTCPServer(utils.SHA256(fmt.Sprintf("%x", sessionKey)), serverAddress+":"+serverTCP)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
@ -195,14 +196,15 @@ func receive(serverAddress, serverTCP string, isLocal bool, c *websocket.Conn, c
|
||||
} else {
|
||||
// read from TCP connection
|
||||
message, err = tcpConnection.Read()
|
||||
if bytes.Equal(message, []byte("end")) {
|
||||
break
|
||||
}
|
||||
// if bytes.Equal(message, []byte("end")) {
|
||||
// break
|
||||
// }
|
||||
}
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(message))
|
||||
|
||||
// // tell the sender that we recieved this packet
|
||||
// c.WriteMessage(websocket.BinaryMessage, []byte("ok"))
|
||||
@ -314,6 +316,7 @@ func receive(serverAddress, serverTCP string, isLocal bool, c *websocket.Conn, c
|
||||
}
|
||||
|
||||
func connectToTCPServer(room string, address string) (com comm.Comm, err error) {
|
||||
log.Debugf("connecting to %s", address)
|
||||
connection, err := net.Dial("tcp", address)
|
||||
if err != nil {
|
||||
return
|
||||
@ -323,6 +326,7 @@ func connectToTCPServer(room string, address string) (com comm.Comm, err error)
|
||||
connection.SetWriteDeadline(time.Now().Add(3 * time.Hour))
|
||||
|
||||
com = comm.New(connection)
|
||||
log.Debug("waiting for server contact")
|
||||
ok, err := com.Receive()
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -208,6 +208,9 @@ func send(serverAddress, serverTCP string, isLocal bool, c *websocket.Conn, fnam
|
||||
// send file, compure hash simultaneously
|
||||
startTransfer = time.Now()
|
||||
buffer := make([]byte, models.WEBSOCKET_BUFFER_SIZE/8)
|
||||
if !isLocal {
|
||||
buffer = make([]byte, models.TCP_BUFFER_SIZE/2)
|
||||
}
|
||||
bar := progressbar.NewOptions(
|
||||
int(fstats.Size),
|
||||
progressbar.OptionSetRenderBlankState(true),
|
||||
@ -249,9 +252,9 @@ func send(serverAddress, serverTCP string, isLocal bool, c *websocket.Conn, fnam
|
||||
if err != io.EOF {
|
||||
log.Error(err)
|
||||
}
|
||||
if !isLocal {
|
||||
tcpConnection.Write([]byte("end"))
|
||||
}
|
||||
// if !isLocal {
|
||||
// tcpConnection.Write([]byte("end"))
|
||||
// }
|
||||
break
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user