Skip to content

Commit

Permalink
request: BUF_SIZE -> bufSize, improve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Jul 25, 2024
1 parent 95dd9ed commit 8801eca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
6 changes: 3 additions & 3 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/thiagokokada/hyprland-go/internal/assert"
)

const SEPARATOR = ">>"
const sep = ">>"

// Initiate a new client or panic.
// This should be the preferred method for user scripts, since it will
Expand Down Expand Up @@ -37,7 +37,7 @@ func NewEventClient(socket string) (*EventClient, error) {
// alternative.
// Experimental: WIP
func (c *EventClient) Receive() ([]ReceivedData, error) {
buf := make([]byte, BUF_SIZE)
buf := make([]byte, bufSize)
n, err := c.conn.Read(buf)
if err != nil {
return nil, err
Expand All @@ -52,7 +52,7 @@ func (c *EventClient) Receive() ([]ReceivedData, error) {
continue
}

split := strings.Split(event, SEPARATOR)
split := strings.Split(event, sep)
if split[0] == "" || split[1] == "" || split[1] == "," {
continue
}
Expand Down
38 changes: 21 additions & 17 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import (
"github.com/thiagokokada/hyprland-go/internal/assert"
)

const BUF_SIZE = 8192
const (
batch = "[[BATCH]]"
bufSize = 8192
)

var reqHeader = []byte{'j', '/'}

func prepareRequest(buf *bytes.Buffer, command string, param string) int {
buf.WriteString(command)
Expand All @@ -36,31 +41,30 @@ func prepareRequests(command string, params []string) (requests []RawRequest, er
switch len(params) {
case 0:
// +2 because of the 'j/'
if len(command)+2 > BUF_SIZE {
if len(command)+2 > bufSize {
return nil, fmt.Errorf(
"command is too long (%d>=%d): %s",
len(command),
BUF_SIZE,
bufSize,
command,
)
}
requests = append(requests, []byte(command))
case 1:
request := command + " " + params[0]
// +2 because of the 'j/'
if len(request)+2 > BUF_SIZE {
if len(request)+2 > bufSize {
return nil, fmt.Errorf(
"command is too long (%d>=%d): %s",
len(request),
BUF_SIZE,
bufSize,
request,
)
}
requests = append(requests, []byte(request))
default:
buf := bytes.NewBuffer(nil)

const batch = "[[BATCH]]"
// Add [[BATCH]] to the buffer
buf.WriteString(batch)
// Initialise current length of buffer
Expand All @@ -70,21 +74,21 @@ func prepareRequests(command string, params []string) (requests []RawRequest, er
// Get the current command + param length
// +4 because of the 'j/; '
cmdLen := len(command) + len(param) + 4
if len(batch)+cmdLen > BUF_SIZE {
if len(batch)+cmdLen > bufSize {
// If batch + command length is bigger than
// BUF_SIZE, return an error since it will not
// bufSize, return an error since it will not
// fit the socket
return nil, fmt.Errorf(
"command is too long (%d>=%d): %s%s %s;",
len(batch)+cmdLen,
BUF_SIZE,
bufSize,
batch,
command,
param,
)
} else if curLen+cmdLen <= BUF_SIZE {
} else if curLen+cmdLen <= bufSize {
// If the current length of the buffer +
// command + param is less than BUF_SIZE, the
// command + param is less than bufSize, the
// request will fit
curLen = prepareRequest(buf, command, param)
} else {
Expand Down Expand Up @@ -232,12 +236,12 @@ func (c *RequestClient) RawRequest(request RawRequest) (response RawResponse, er
}

// Send the request to the socket
request = append([]byte{'j', '/'}, request...)
if len(request) > BUF_SIZE {
request = append(reqHeader, request...)
if len(request) > bufSize {
return nil, fmt.Errorf(
"request too big (%d>%d): %s",
len(request),
BUF_SIZE,
bufSize,
request,
)
}
Expand All @@ -251,19 +255,19 @@ func (c *RequestClient) RawRequest(request RawRequest) (response RawResponse, er

// Get the response back
rbuf := bytes.NewBuffer(nil)
sbuf := make([]byte, BUF_SIZE)
sbuf := make([]byte, bufSize)
reader := bufio.NewReader(conn)
for {
n, err := reader.Read(sbuf)
if err != nil {
if err == io.EOF {
break
}
return nil, err
return nil, fmt.Errorf("error while reading from socket: %w", err)
}

rbuf.Write(sbuf[:n])
if n < BUF_SIZE {
if n < bufSize {
break
}
}
Expand Down
6 changes: 3 additions & 3 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,19 @@ func TestPrepareRequestsMass(t *testing.T) {

func TestPrepareRequestsError(t *testing.T) {
_, err := prepareRequests(
strings.Repeat("c", BUF_SIZE-1),
strings.Repeat("c", bufSize-1),
nil,
)
assert.Error(t, err)

_, err = prepareRequests(
strings.Repeat("c", BUF_SIZE-len("p ")-1),
strings.Repeat("c", bufSize-len("p ")-1),
genParams("p", 1),
)
assert.Error(t, err)

_, err = prepareRequests(
strings.Repeat("c", BUF_SIZE-len("[[BATCH]] p;")-1),
strings.Repeat("c", bufSize-len(batch+" p;")-1),
genParams("p", 5),
)
assert.Error(t, err)
Expand Down

0 comments on commit 8801eca

Please sign in to comment.