-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
207 lines (179 loc) · 4.04 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
"github.com/peternoyes/dodo-sim"
)
const (
pongWait = 20 * time.Second
pingPeriod = (pongWait * 9) / 10
)
var upgrader = websocket.Upgrader{}
func Server() {
http.HandleFunc("/stream", stream)
http.HandleFunc("/", home)
log.Fatal(http.ListenAndServe(":3000", nil))
}
type SocketRenderer struct {
Conn *websocket.Conn
}
func (s *SocketRenderer) Render(data [1024]byte) {
s.Conn.WriteMessage(websocket.BinaryMessage, data[:])
}
func stream(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("Upgrade: ", err)
return
}
defer c.Close()
c.SetPongHandler(func(string) error { c.SetReadDeadline(time.Now().Add(pongWait)); return nil })
input := make(chan string)
go func(input chan string) {
for {
_, p, err := c.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
fmt.Println("Unexpected Closer")
}
break
}
input <- string(p)
}
}(input)
s := new(dodosim.Simulator)
renderer := new(SocketRenderer)
renderer.Conn = c
s.Renderer = renderer
s.Ticker = time.NewTicker(pingPeriod)
s.Input = input
s.IntervalCallback = func() bool {
if err = c.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
return false
}
return true
}
s.Complete = func(cpu *dodosim.Cpu) {
}
s.CyclesPerFrame = func(cycles uint64) {
}
//firmware, err := ioutil.ReadFile("firmware")
firmware, err := Asset("data/firmware")
if err != nil {
fmt.Println(err)
return
}
game, err := ioutil.ReadFile("fram.bin")
if err != nil {
fmt.Println(err)
return
}
dodosim.Simulate(s, firmware, game)
fmt.Println("Complete...")
}
func home(w http.ResponseWriter, r *http.Request) {
homeTemplate.Execute(w, "ws://"+r.Host+"/stream")
}
var homeTemplate = template.Must(template.New("").Parse(`<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script>
window.addEventListener("load", function(evt) {
var ws;
var output = document.getElementById("output");
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var on = ctx.createImageData(1, 1);
var off = ctx.createImageData(1, 1);
var d = on.data;
d[0] = 0;
d[1] = 255;
d[2] = 0;
d[3] = 255;
d = off.data;
d[0] = 0;
d[1] = 0;
d[2] = 0;
d[3] = 255;
var print = function(message) {
var d = document.createElement("div");
d.innerHTML = message;
output.appendChild(d);
};
var keyState = {};
var sendKeyState = function() {
var s = "";
if (keyState[37]) s += "L";
if (keyState[38]) s += "U";
if (keyState[39]) s += "R";
if (keyState[40]) s += "D";
if (keyState[65]) s += "A";
if (keyState[66]) s += "B";
ws.send(s);
}
var processdown = function(evt) {
switch (evt.keyCode) {
case 37:
case 39:
case 38:
case 40:
case 65:
case 66:
if (!keyState[evt.keyCode]) {
keyState[evt.keyCode] = true;
sendKeyState();
}
break;
}
}
var processup = function(evt) {
switch (evt.keyCode) {
case 37:
case 39:
case 38:
case 40:
case 65:
case 66:
keyState[evt.keyCode] = false;
sendKeyState();
break;
}
}
window.addEventListener("keydown", processdown);
window.addEventListener("keyup", processup);
document.getElementById("go").onclick = function(evt) {
ws = new WebSocket("{{.}}");
ws.binaryType = 'arraybuffer';
ws.onmessage = function(evt) {
var dv = new DataView(evt.data);
var x, y = 0;
for (y = 0; y < 64; ++y) {
for (x = 0; x < 128; ++x) {
b = (dv.getUint8(x+(Math.floor(y/8)*128)) >> (y%8)) & 1;
if (b == 1) {
ctx.putImageData(on, x, y);
} else {
ctx.putImageData(off, x, y);
}
}
}
}
return false;
};
});
</script>
</head>
<body>
<form>
<button id="go">Go</button>
</form>
<canvas id="myCanvas" width="128" height="64"></canvas>
<div id="output"></div>
</body>
</html>
`))