mirror of https://github.com/schollz/croc.git
Added send/receiev
This commit is contained in:
parent
a849e0cc50
commit
0bcd272f4e
|
@ -7,6 +7,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -34,29 +35,21 @@ func runClient(connectionType string, codePhrase string) {
|
||||||
message := receiveMessage(connection)
|
message := receiveMessage(connection)
|
||||||
logger.Infof("message: %s", message)
|
logger.Infof("message: %s", message)
|
||||||
sendMessage(connectionType+"."+codePhrase, connection)
|
sendMessage(connectionType+"."+codePhrase, connection)
|
||||||
if connectionType == "s" {
|
if connectionType == "s" { // this is a sender
|
||||||
logger.Info("waiting for ok from relay")
|
logger.Info("waiting for ok from relay")
|
||||||
message = receiveMessage(connection)
|
message = receiveMessage(connection)
|
||||||
logger.Info("got ok from relay")
|
logger.Info("got ok from relay")
|
||||||
// wait for pipe to be made
|
// wait for pipe to be made
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
// Send file name
|
|
||||||
logger.Info("sending filename")
|
// Write data from file
|
||||||
sendMessage("filename", connection)
|
sendFileToClient(id, connection)
|
||||||
// Send file size
|
|
||||||
time.Sleep(3 * time.Second)
|
|
||||||
logger.Info("sending filesize")
|
|
||||||
sendMessage("filesize", connection)
|
|
||||||
// TODO: Write data from file
|
|
||||||
|
|
||||||
// TODO: Release from connection pool
|
// TODO: Release from connection pool
|
||||||
// POST /release
|
// POST /release
|
||||||
} else {
|
} else { // this is a receiver
|
||||||
fileName := receiveMessage(connection)
|
// receive file
|
||||||
fileSize := receiveMessage(connection)
|
receiveFile(id, connection)
|
||||||
logger.Infof("fileName: %s", fileName)
|
|
||||||
logger.Infof("fileSize: %s", fileSize)
|
|
||||||
// TODO: Pull data and write to file
|
|
||||||
|
|
||||||
// TODO: Release from connection pool
|
// TODO: Release from connection pool
|
||||||
// POST /release
|
// POST /release
|
||||||
|
@ -67,6 +60,36 @@ func runClient(connectionType string, codePhrase string) {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func receiveFile(id int, connection net.Conn) {
|
||||||
|
bufferFileName := make([]byte, 64)
|
||||||
|
bufferFileSize := make([]byte, 10)
|
||||||
|
|
||||||
|
connection.Read(bufferFileSize)
|
||||||
|
fileSize, _ := strconv.ParseInt(strings.Trim(string(bufferFileSize), ":"), 10, 64)
|
||||||
|
|
||||||
|
connection.Read(bufferFileName)
|
||||||
|
fileName = strings.Trim(string(bufferFileName), ":")
|
||||||
|
os.Remove(fileName + "." + strconv.Itoa(id))
|
||||||
|
newFile, err := os.Create(fileName + "." + strconv.Itoa(id))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer newFile.Close()
|
||||||
|
|
||||||
|
var receivedBytes int64
|
||||||
|
for {
|
||||||
|
if (fileSize - receivedBytes) < BUFFERSIZE {
|
||||||
|
io.CopyN(newFile, connection, (fileSize - receivedBytes))
|
||||||
|
// Empty the remaining bytes that we don't need from the network buffer
|
||||||
|
connection.Read(make([]byte, (receivedBytes+BUFFERSIZE)-fileSize))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
io.CopyN(newFile, connection, BUFFERSIZE)
|
||||||
|
//Increment the counter
|
||||||
|
receivedBytes += BUFFERSIZE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func sendFileToClient(id int, connection net.Conn) {
|
func sendFileToClient(id int, connection net.Conn) {
|
||||||
logger := log.WithFields(log.Fields{
|
logger := log.WithFields(log.Fields{
|
||||||
"function": "sendFileToClient #" + strconv.Itoa(id),
|
"function": "sendFileToClient #" + strconv.Itoa(id),
|
||||||
|
|
Loading…
Reference in New Issue