mirror of https://github.com/Askill/r_place.git
reduced number of structs, 2 endpoints, timeout due to no keepalive ping from server
This commit is contained in:
parent
7be7496944
commit
5177c4b601
65
go/server.go
65
go/server.go
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
|
@ -15,19 +14,29 @@ var upgrader = websocket.Upgrader{
|
||||||
ReadBufferSize: 2048,
|
ReadBufferSize: 2048,
|
||||||
WriteBufferSize: 2048,
|
WriteBufferSize: 2048,
|
||||||
}
|
}
|
||||||
var img = GetImage(10, 10)
|
var img = GetImage(1000, 1000)
|
||||||
|
|
||||||
func write(ticker time.Ticker, c *websocket.Conn, wg *sync.WaitGroup) {
|
func get(w http.ResponseWriter, r *http.Request) {
|
||||||
defer wg.Done()
|
c, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Print("error while upgrading", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer c.Close()
|
||||||
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
|
|
||||||
|
var refImage = GetImage(img.width, img.height)
|
||||||
var tmpImage = GetImage(img.width, img.height)
|
var tmpImage = GetImage(img.width, img.height)
|
||||||
|
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
diff := tmpImage.GetDiff(&img)
|
copy(refImage.pixels, img.pixels)
|
||||||
for i := 0; i < int(diff.Width*diff.Height); i++ {
|
diff := tmpImage.GetDiff(&refImage)
|
||||||
pix := diff.Pixels[i]
|
for i := 0; i < int(diff.width*diff.height); i++ {
|
||||||
if pix.UserID != 0 {
|
pix := diff.pixels[i]
|
||||||
x := uint16(i / int(diff.Width))
|
if pix.pixel.UserID != 0 {
|
||||||
y := uint16(i % int(diff.Height))
|
x := i / int(diff.width)
|
||||||
msg := Message{X: x, Y: y, Timestamp: pix.Timestamp, UserID: pix.UserID, Color: pix.Color}
|
y := i % int(diff.height)
|
||||||
|
msg := Message{X: uint32(x), Y: uint32(y), Timestamp: pix.pixel.Timestamp, UserID: pix.pixel.UserID, Color: pix.pixel.Color}
|
||||||
marshalMsg, err := json.Marshal(msg)
|
marshalMsg, err := json.Marshal(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error while writing image", err)
|
log.Println("error while writing image", err)
|
||||||
|
|
@ -36,12 +45,17 @@ func write(ticker time.Ticker, c *websocket.Conn, wg *sync.WaitGroup) {
|
||||||
err = c.WriteMessage(1, marshalMsg)
|
err = c.WriteMessage(1, marshalMsg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
copy(img.pixels, tmpImage.pixels)
|
copy(tmpImage.pixels, refImage.pixels)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func read(c *websocket.Conn, wg *sync.WaitGroup) {
|
func set(w http.ResponseWriter, r *http.Request) {
|
||||||
defer wg.Done()
|
c, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Print("error while upgrading", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer c.Close()
|
||||||
for {
|
for {
|
||||||
_, msg, err := c.ReadMessage()
|
_, msg, err := c.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -50,35 +64,20 @@ func read(c *websocket.Conn, wg *sync.WaitGroup) {
|
||||||
}
|
}
|
||||||
|
|
||||||
message := Message{}
|
message := Message{}
|
||||||
message.JsonToStruct(msg)
|
json.Unmarshal(msg, &message)
|
||||||
|
|
||||||
img.SetPixel(message)
|
img.SetPixel(message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func serve(w http.ResponseWriter, r *http.Request) {
|
|
||||||
c, err := upgrader.Upgrade(w, r, nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Print("error while upgrading", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer c.Close()
|
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
|
|
||||||
// end fucntion if either of the 2 functions is done
|
|
||||||
wg.Add(1)
|
|
||||||
go write(*ticker, c, &wg)
|
|
||||||
go read(c, &wg)
|
|
||||||
wg.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var addr = flag.String("addr", "localhost:8080", "http service address")
|
var addr = flag.String("addr", "localhost:8080", "http service address")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
log.SetFlags(0)
|
log.SetFlags(0)
|
||||||
log.Println("starting server on", *addr)
|
log.Println("starting server on", *addr)
|
||||||
http.HandleFunc("/", serve)
|
http.HandleFunc("/get", get)
|
||||||
|
http.HandleFunc("/set", set)
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(*addr, nil))
|
log.Fatal(http.ListenAndServe(*addr, nil))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
73
go/util.go
73
go/util.go
|
|
@ -1,71 +1,50 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
X uint16 `json:"x"`
|
X uint32 `json:"x"`
|
||||||
Y uint16 `json:"y"`
|
Y uint32 `json:"y"`
|
||||||
Color uint8 `json:"color"`
|
Color uint8 `json:"color"`
|
||||||
Timestamp int64 `json:"timestamp"`
|
Timestamp int64 `json:"timestamp"`
|
||||||
UserID uint64 `json:"userid"`
|
UserID uint64 `json:"userid"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (message *Message) JsonToStruct(input []byte) *Message {
|
|
||||||
json.Unmarshal(input, message)
|
|
||||||
return message
|
|
||||||
}
|
|
||||||
|
|
||||||
type pixel struct {
|
type pixel struct {
|
||||||
Color uint8 `json:"color"`
|
Color uint8 `json:"color"`
|
||||||
Timestamp int64 `json:"timestamp"`
|
Timestamp int64 `json:"timestamp"`
|
||||||
UserID uint64 `json:"userid"`
|
UserID uint64 `json:"userid"`
|
||||||
Mutex sync.Mutex
|
}
|
||||||
|
|
||||||
|
type pixelContainer struct {
|
||||||
|
pixel pixel
|
||||||
|
Mutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type image struct {
|
type image struct {
|
||||||
width uint16
|
width uint32
|
||||||
height uint16
|
height uint32
|
||||||
pixels []pixel
|
pixels []pixelContainer
|
||||||
}
|
}
|
||||||
|
|
||||||
type messagePixel struct {
|
func GetImage(w uint32, h uint32) image {
|
||||||
Color uint8 `json:"color"`
|
pixels := make([]pixelContainer, w*h)
|
||||||
Timestamp int64 `json:"timestamp"`
|
|
||||||
UserID uint64 `json:"userid"`
|
|
||||||
}
|
|
||||||
type messageImage struct {
|
|
||||||
Width uint16 `json:"width"`
|
|
||||||
Height uint16 `json:"height"`
|
|
||||||
Pixels []messagePixel `json:"pixel"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetMessageImage(w uint16, h uint16) messageImage {
|
|
||||||
pixels := make([]messagePixel, w*h)
|
|
||||||
for i := 0; i < int(w*h); i++ {
|
for i := 0; i < int(w*h); i++ {
|
||||||
pixels[i] = messagePixel{Color: 0, Timestamp: 0, UserID: 0}
|
pixels[i] = pixelContainer{pixel: pixel{Color: 0, Timestamp: 0, UserID: 0}, Mutex: sync.Mutex{}}
|
||||||
}
|
|
||||||
return messageImage{Width: w, Height: h, Pixels: pixels}
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetImage(w uint16, h uint16) image {
|
|
||||||
pixels := make([]pixel, w*h)
|
|
||||||
for i := 0; i < int(w*h); i++ {
|
|
||||||
pixels[i] = pixel{Color: 0, Timestamp: 0, UserID: 0, Mutex: sync.Mutex{}}
|
|
||||||
}
|
}
|
||||||
return image{width: w, height: h, pixels: pixels}
|
return image{width: w, height: h, pixels: pixels}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pixel) setColor(color uint8, timestamp int64, userid uint64) {
|
func (p *pixelContainer) setColor(color uint8, timestamp int64, userid uint64) {
|
||||||
p.Mutex.Lock()
|
p.Mutex.Lock()
|
||||||
defer p.Mutex.Unlock()
|
defer p.Mutex.Unlock()
|
||||||
if timestamp > p.Timestamp {
|
if timestamp > p.pixel.Timestamp {
|
||||||
p.Color = color
|
p.pixel.Color = color
|
||||||
p.Timestamp = timestamp
|
p.pixel.Timestamp = timestamp
|
||||||
p.UserID = userid
|
p.pixel.UserID = userid
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -83,17 +62,19 @@ func (img *image) SetPixel(message Message) *image {
|
||||||
return img
|
return img
|
||||||
}
|
}
|
||||||
|
|
||||||
func comparePixels(pixel1 *pixel, pixel2 *pixel) bool {
|
func comparePixels(pixel1 *pixelContainer, pixel2 *pixelContainer) bool {
|
||||||
return pixel1.Color == pixel2.Color && pixel1.Timestamp == pixel2.Timestamp && pixel1.UserID == pixel2.UserID
|
return pixel1.pixel.Color == pixel2.pixel.Color &&
|
||||||
|
pixel1.pixel.Timestamp == pixel2.pixel.Timestamp &&
|
||||||
|
pixel1.pixel.UserID == pixel2.pixel.UserID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (img *image) GetDiff(img2 *image) messageImage {
|
func (img *image) GetDiff(img2 *image) image {
|
||||||
diff := GetMessageImage(img.width, img.height)
|
diff := GetImage(img.width, img.height)
|
||||||
for i := 0; i < int(img.width*img.height); i++ {
|
for i := 0; i < int(img.width*img.height); i++ {
|
||||||
if !comparePixels(&img.pixels[i], &img2.pixels[i]) {
|
if !comparePixels(&img.pixels[i], &img2.pixels[i]) {
|
||||||
diff.Pixels[i].Color = img2.pixels[i].Color
|
diff.pixels[i].pixel.Color = img2.pixels[i].pixel.Color
|
||||||
diff.Pixels[i].UserID = img2.pixels[i].UserID
|
diff.pixels[i].pixel.UserID = img2.pixels[i].pixel.UserID
|
||||||
diff.Pixels[i].Timestamp = img2.pixels[i].Timestamp
|
diff.pixels[i].pixel.Timestamp = img2.pixels[i].pixel.Timestamp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return diff
|
return diff
|
||||||
|
|
|
||||||
|
|
@ -20,30 +20,32 @@ class pixel:
|
||||||
userid: int
|
userid: int
|
||||||
|
|
||||||
async def sender():
|
async def sender():
|
||||||
async with websockets.connect("ws://localhost:8080/") as websocket:
|
async with websockets.connect("ws://localhost:8080/set") as websocket:
|
||||||
while True:
|
while True:
|
||||||
message = pixel(
|
message = pixel(
|
||||||
x=random.randint(0, 9),
|
x=random.randint(0, 990),
|
||||||
y=random.randint(0, 9),
|
y=random.randint(0, 990),
|
||||||
color=random.randint(0,15),
|
color=random.randint(0,15),
|
||||||
timestamp=int(time.time()),
|
timestamp=int(time.time()),
|
||||||
userid=1,
|
userid=1,
|
||||||
)
|
)
|
||||||
await websocket.send(json.dumps(message.__dict__))
|
await websocket.send(json.dumps(message.__dict__))
|
||||||
|
|
||||||
await asyncio.sleep(0.1)
|
await asyncio.sleep(0.1)
|
||||||
|
|
||||||
async def client():
|
async def client():
|
||||||
async with websockets.connect("ws://localhost:8080/") as websocket:
|
async with websockets.connect("ws://localhost:8080/get") as websocket:
|
||||||
i= 0
|
i= 0
|
||||||
while True:
|
while True:
|
||||||
i+=1
|
i+=1
|
||||||
x = await websocket.recv()
|
x = await websocket.recv()
|
||||||
print(i, pixel(**json.loads(x)))
|
#print(i, x)
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
coros = [sender() for _ in range(100)]
|
coros = [sender() for _ in range(500)]
|
||||||
coros.append(client())
|
coros.append(client())
|
||||||
returns = await asyncio.gather(*coros)
|
returns = await asyncio.gather(*coros)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.get_event_loop().run_until_complete(main())
|
asyncio.get_event_loop().run_until_complete(main())
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue