Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added errors in ipc.Send #35

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"crypto/rand"
"encoding/json"
"fmt"
"github.com/dikey0ficial/rich-go/ipc"
"os"

"github.com/hugolgst/rich-go/ipc"
)

var logged bool

// Login sends a handshake in the socket and returns an error or nil
// Login sends a handshake in socket and returns an error or nil
func Login(clientid string) error {
var gerr error
if !logged {
payload, err := json.Marshal(Handshake{"1", clientid})
if err != nil {
Expand All @@ -25,11 +25,11 @@ func Login(clientid string) error {
}

// TODO: Response should be parsed
ipc.Send(0, string(payload))
_, gerr = ipc.Send(0, string(payload))
}
logged = true

return nil
return gerr
}

func Logout() {
Expand Down Expand Up @@ -60,11 +60,11 @@ func SetActivity(activity Activity) error {
}

// TODO: Response should be parsed
ipc.Send(1, string(payload))
return nil
_, err = ipc.Send(1, string(payload))
return err
}

func getNonce() string {
func getNonce() (string, error) {
buf := make([]byte, 16)
_, err := rand.Read(buf)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions client/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/dikey0ficial/rich-go/client

go 1.17

require github.com/hugolgst/rich-go v0.0.0-20210925091458-d59fb695d9c0

require gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/hugolgst/rich-go
module github.com/dikey0ficial/rich-go

go 1.17

Expand Down
20 changes: 11 additions & 9 deletions ipc/ipc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ipc
import (
"bytes"
"encoding/binary"
"fmt"
"net"
"os"
)
Expand Down Expand Up @@ -34,39 +33,42 @@ func CloseSocket() error {
}

// Read the socket response
func Read() string {
func Read() (string, error) {
buf := make([]byte, 512)
payloadlength, err := socket.Read(buf)
if err != nil {
//fmt.Println("Nothing to read")
return "", err
}

buffer := new(bytes.Buffer)
for i := 8; i < payloadlength; i++ {
buffer.WriteByte(buf[i])
err := buffer.WriteByte(buf[i])
if err != nil {
return "", err
}
}

return buffer.String()
return buffer.String(), nil
}

// Send opcode and payload to the unix socket
func Send(opcode int, payload string) string {
func Send(opcode int, payload string) (string, error) {
buf := new(bytes.Buffer)

err := binary.Write(buf, binary.LittleEndian, int32(opcode))
if err != nil {
fmt.Println(err)
return "", err
}

err = binary.Write(buf, binary.LittleEndian, int32(len(payload)))
if err != nil {
fmt.Println(err)
return "", err
}

buf.Write([]byte(payload))
_, err = socket.Write(buf.Bytes())
if err != nil {
fmt.Println(err)
return "", err
}

return Read()
Expand Down