Skip to content

Commit

Permalink
Add Go implementation of checkerlib
Browse files Browse the repository at this point in the history
  • Loading branch information
rudis committed Jun 11, 2020
1 parent ae88eed commit b6b5bed
Show file tree
Hide file tree
Showing 6 changed files with 474 additions and 0 deletions.
6 changes: 6 additions & 0 deletions go/checkerlib/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module github.com/fausecteam/ctf-gameserver/go/checkerlib

require (
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9
golang.org/x/sys v0.0.0-20200610111108-226ff32320da // indirect
)
10 changes: 10 additions & 0 deletions go/checkerlib/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 h1:vEg9joUBmeBcK9iSJftGNf3coIG4HqZElCPehJsfAYM=
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200610111108-226ff32320da h1:bGb80FudwxpeucJUjPYJXuJ8Hk91vNtfvrymzwiei38=
golang.org/x/sys v0.0.0-20200610111108-226ff32320da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
69 changes: 69 additions & 0 deletions go/checkerlib/ipc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Communication with checkermaster

package checkerlib

import (
"bufio"
"bytes"
"encoding/json"
"io"
"sync"
)

type ipcData struct {
sync.Mutex

in *bufio.Scanner
out io.Writer
}

func (i *ipcData) Send(action string, param interface{}) {
ipc.Lock()
defer ipc.Unlock()

i.send(action, param)
}
func (i *ipcData) SendRecv(action string, param interface{}) interface{} {
ipc.Lock()
defer ipc.Unlock()

i.send(action, param)
return i.recv()
}

func (i *ipcData) send(action string, param interface{}) {
data := struct {
Action string `json:"action"`
Param interface{} `json:"param"`
}{
action,
param,
}

x, err := json.Marshal(data)
if err != nil {
panic(err)
}
// Make sure that our JSON consists of just a single line as required
// by IPC protocol
x = append(bytes.Replace(x, []byte{'\n'}, nil, -1), '\n')

_, err = i.out.Write(x)
if err != nil {
panic(err)
}
}

func (i *ipcData) recv() interface{} {
if !i.in.Scan() {
panic(i.in.Err())
}
var x struct {
Response interface{} `json:"response"`
}
err := json.Unmarshal(i.in.Bytes(), &x)
if err != nil {
panic(err)
}
return x.Response
}
Loading

0 comments on commit b6b5bed

Please sign in to comment.