0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

Seems to start to work

This commit is contained in:
Zack Scholl 2017-10-17 12:03:03 -06:00
parent ef3cfc26b5
commit 97bb4e1dc5
4 changed files with 33 additions and 19 deletions

View file

@ -4,11 +4,10 @@ import (
"fmt" "fmt"
"net" "net"
"strconv" "strconv"
"strings"
"sync" "sync"
) )
func runClient() { func runClient(connectionType string, codePhrase string) {
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(numberConnections) wg.Add(numberConnections)
for id := 0; id < numberConnections; id++ { for id := 0; id < numberConnections; id++ {
@ -21,14 +20,13 @@ func runClient() {
} }
defer connection.Close() defer connection.Close()
var messageByte []byte message := receiveMessage(connection)
var message string
messageByte = make([]byte, 64)
connection.Read(messageByte)
message = strings.Replace(string(messageByte), ":", "", -1)
fmt.Println(message) fmt.Println(message)
message = fillString("r.1-2-3", 64) sendMessage(connectionType+"."+codePhrase, connection)
connection.Write([]byte(message)) if connectionType == "s" {
message = receiveMessage(connection)
fmt.Println(message)
}
}(id) }(id)
} }

View file

@ -32,7 +32,7 @@ func main() {
if len(fileName) != 0 { if len(fileName) != 0 {
runServer() runServer()
} else if len(serverAddress) != 0 { } else if len(serverAddress) != 0 {
runClient() runClient(connectionTypeFlag, codePhraseFlag)
} else { } else {
fmt.Println("You must specify either -file (for running as a server) or -server (for running as a client)") fmt.Println("You must specify either -file (for running as a server) or -server (for running as a client)")
} }

View file

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

View file

@ -6,6 +6,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -79,26 +80,41 @@ func clientCommuncation(id int, connection net.Conn) {
connectionType := strings.Split(message, ".")[0] connectionType := strings.Split(message, ".")[0]
codePhrase := strings.Split(message, ".")[1] codePhrase := strings.Split(message, ".")[1]
// If reciever // If reciever
connections.Lock()
connections.reciever[codePhrase] = connection
connections.Unlock()
if connectionType == "s" { if connectionType == "s" {
// periodically check if the receiver has joined fmt.Println("Got sender")
connections.Lock()
connections.sender[codePhrase] = connection
connections.Unlock()
for {
fmt.Println("waiting for reciever")
connections.RLock()
if _, ok := connections.reciever[codePhrase]; ok {
break
}
connections.RUnlock()
time.Sleep(100 * time.Millisecond)
}
sendMessage("ok", connection)
} else {
fmt.Println("Got reciever")
connections.Lock()
connections.reciever[codePhrase] = connection
connections.Unlock()
} }
fmt.Println(message) fmt.Println(message)
return return
} }
func sendMessage(message string, connection net.Conn) { func sendMessage(message string, connection net.Conn) {
message = fillString(message, 64) message = fillString(message, BUFFERSIZE)
connection.Write([]byte(message)) connection.Write([]byte(message))
} }
func receiveMessage(connection net.Conn) string { func receiveMessage(connection net.Conn) string {
messageByte := make([]byte, 64) messageByte := make([]byte, BUFFERSIZE)
connection.Read(messageByte) n, err := connection.Read(messageByte)
fmt.Println(n, err)
return strings.Replace(string(messageByte), ":", "", -1) return strings.Replace(string(messageByte), ":", "", -1)
} }