refactor to use bufReader.ReadBytes when accepting incoming messages from the connection socket

This commit is contained in:
Tim Williams 2018-07-27 14:03:15 -04:00
parent 4833f428e5
commit 8cc8da8ea5

View file

@ -3,7 +3,8 @@ package main
//@NOTE SHYFT main func for api, sets up router and spins up a server //@NOTE SHYFT main func for api, sets up router and spins up a server
//to run server 'go run shyftRingWalletConn/*.go' //to run server 'go run shyftRingWalletConn/*.go'
import ( import (
"bytes" //"bytes"
"bufio"
"fmt" "fmt"
"github.com/ShyftNetwork/go-empyrean/common/hexutil" "github.com/ShyftNetwork/go-empyrean/common/hexutil"
"github.com/ShyftNetwork/go-empyrean/crypto" "github.com/ShyftNetwork/go-empyrean/crypto"
@ -13,9 +14,10 @@ import (
) )
const ( const (
CONN_HOST = "localhost" CONN_HOST = "localhost"
CONN_PORT = "3333" CONN_PORT = "3333"
CONN_TYPE = "tcp" CONN_TYPE = "tcp"
NEW_LINE_BYTE = 0x0a
) )
var testAddrHex = "14791697260E4c9A71f18484C9f997B308e59325" var testAddrHex = "14791697260E4c9A71f18484C9f997B308e59325"
@ -50,19 +52,17 @@ func main() {
// Handles incoming requests. // Handles incoming requests.
func handleRequest(conn net.Conn) { func handleRequest(conn net.Conn) {
// Make a buffer to hold incoming data.
// Read the incoming connection into the buffer.
go func() { go func() {
buf := make([]byte, 1024)
msgBuf := make([]byte, 0)
var prevMsg []byte var prevMsg []byte
var addressOfClient []byte var addressOfClient []byte
var signatureFromClient []byte var signatureFromClient []byte
var msgFromClient []byte var msgFromClient []byte
bufReader := bufio.NewReader(conn)
for { for {
msg, err := conn.Read(buf) msg, err := bufReader.ReadBytes(NEW_LINE_BYTE)
if err == io.EOF { if err == io.EOF {
fmt.Println("END OF FILE, CLOSING CONNECTION") fmt.Println("END OF FILE, CLOSING CONNECTION")
conn.Close() conn.Close()
@ -74,28 +74,23 @@ func handleRequest(conn net.Conn) {
break break
} }
msgBuf = append(msgBuf, buf[:msg]...) msg = msg[:len(msg)-1] // remove trailing new line byte
index := bytes.IndexByte(msgBuf, 0x0a)
for index != -1 { // similar to shift in bash
newMsg := msgBuf[:index] if prevMsg != nil {
rest := msgBuf[(index + 1):len(msgBuf)] s := string(prevMsg[:])
msgBuf = rest if s == "-- ADDRESS --" {
index = bytes.IndexByte(msgBuf, 0x0a) addressOfClient = msg
if prevMsg != nil {
s := string(prevMsg[:])
if s == "-- ADDRESS --" {
addressOfClient = newMsg
}
if s == "-- SIGNATURE --" {
signatureFromClient = newMsg
}
if s == "-- MESSAGE --" {
msgFromClient = newMsg
}
prevMsg = nil
} else {
prevMsg = newMsg
} }
if s == "-- SIGNATURE --" {
signatureFromClient = msg
}
if s == "-- MESSAGE --" {
msgFromClient = msg
}
prevMsg = nil
} else {
prevMsg = msg
} }
if addressOfClient != nil && signatureFromClient != nil && msgFromClient != nil { if addressOfClient != nil && signatureFromClient != nil && msgFromClient != nil {
@ -122,27 +117,27 @@ func handleRequest(conn net.Conn) {
} }
} }
}() }()
go func() {
key, _ := crypto.HexToECDSA(testPrivHex)
f_msg := "Hello World" key, _ := crypto.HexToECDSA(testPrivHex)
first_message := []byte(f_msg)
new_msg2 := crypto.Keccak256(first_message)
fmt.Println("HASH IS ::", hexutil.Encode(new_msg2))
//send_message := append(new_msg2, []byte{byte(10)}...) f_msg := "Hello World"
new_sig, err := crypto.Sign(new_msg2, key) first_message := []byte(f_msg)
if err != nil { new_msg2 := crypto.Keccak256(first_message)
fmt.Println("The crypto.Sign err is ", err) fmt.Println("HASH IS ::", hexutil.Encode(new_msg2))
}
hex_sig := hexutil.Encode(new_sig) //send_message := append(new_msg2, []byte{byte(10)}...)
fmt.Println("HEX SIG ::", hex_sig) new_sig, err := crypto.Sign(new_msg2, key)
if err != nil {
fmt.Println("The crypto.Sign err is ", err)
}
hex_sig := hexutil.Encode(new_sig)
fmt.Println("HEX SIG ::", hex_sig)
conn.Write([]byte("Broadcasting Message"))
conn.Write([]byte("\n"))
conn.Write([]byte(f_msg))
conn.Write([]byte("\n"))
conn.Write(new_sig)
conn.Write([]byte("\n"))
conn.Write([]byte("Broadcasting Message"))
conn.Write([]byte("\n"))
conn.Write([]byte(f_msg))
conn.Write([]byte("\n"))
conn.Write(new_sig)
conn.Write([]byte("\n"))
}()
} }