-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
591 lines (480 loc) · 16.3 KB
/
main.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
package main
import (
"encoding/json"
"fmt"
"net"
"net/http"
"strconv"
"strings"
"sync"
"time"
//"os"
//"math"
//"reflect"
//"encoding/binary"
"github.com/gorilla/websocket"
"github.com/pion/webrtc/v3"
"github.com/wawesomeNOGUI/webrtcGameTemplate/signal"
)
// Game Updates Map
// var Updates sync.Map
var NumberOfPlayers int
// Concurrent Safe Maps of Datachannels for broadcasting or sending messages between players
// DataChannelContainer in messaging.go
var reliableChans DataChannelContainer = DataChannelContainer{chans: make(map[string]*webrtc.DataChannel)}
var unreliableChans DataChannelContainer = DataChannelContainer{chans: make(map[string]*webrtc.DataChannel)}
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
var api *webrtc.API
func echo(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println("Upgrade: ", err)
return
}
defer c.Close()
fmt.Println("User connected from: ", c.RemoteAddr())
//===========This Player's Variables===================
var playerTag string
var playerPtr *Player
var room *Room
// var pRoomMutex sync.Mutex
defer func() {
playerPtr.room.Entities.DeleteEntity(playerTag)
}()
// lets UDP chan onOpen and reliable chan onOpen know that the player has been fully setup in the OnICEConnectionStateChange
playerReady := make(chan bool)
//===========WEBRTC====================================
// Create a new RTCPeerConnection
peerConnection, err := api.NewPeerConnection(webrtc.Configuration{})
if err != nil {
panic(err)
}
//Setup dataChannel to act like UDP with ordered messages (no retransmits)
//with the DataChannelInit struct
var udpPls webrtc.DataChannelInit
var retransmits uint16 = 0
//DataChannel will drop any messages older than
//the most recent one received if ordered = true && retransmits = 0
//This is nice so we can always assume client
//side that the message received from the server
//is the most recent update, and not have to
//implement logic for handling old messages
var ordered = true
udpPls.Ordered = &ordered
udpPls.MaxRetransmits = &retransmits
// Create a datachannel with label 'UDP' and options udpPls
dataChannel, err := peerConnection.CreateDataChannel("UDP", &udpPls)
if err != nil {
panic(err)
}
//Create a reliable datachannel with label "TCP" for all other communications
reliableChannel, err := peerConnection.CreateDataChannel("TCP", nil)
if err != nil {
panic(err)
}
// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
if connectionState == webrtc.ICEConnectionStateConnected {
//Store a new x and y for this player
NumberOfPlayers++
playerTag = strconv.Itoa(NumberOfPlayers)
fmt.Println(playerTag)
playerPtr = newPlayer(playerTag, 50, 50)
// start player in default room
v, ok := Rooms.Load("r1")
if !ok {
fmt.Println("Couldn't find room")
}
room = v.(*Room)
playerPtr.room = room
//Store a pointer to a Player Struct in the default room
room.Entities.StoreEntity(playerTag, playerPtr)
// go func() {
// for {
// // room = <-tmpPlayer.roomChangeChan //WallCheck will first set room to nil so player can't move while changing rooms
// tmp := <-tmpPlayer.roomChangeChan
// pRoomMutex.Lock()
// room = tmp
// pRoomMutex.Unlock()
// }
// }()
// send two playerreadies, one for each datachannel we're opening
playerReady <- true
playerReady <- true
fmt.Println("stored player")
} else if connectionState == webrtc.ICEConnectionStateDisconnected || connectionState == webrtc.ICEConnectionStateClosed {
playerPtr.room.Entities.DeleteEntity(playerTag)
fmt.Println("Deleted Player")
reliableChans.DeletePlayerChan(playerTag)
unreliableChans.DeletePlayerChan(playerTag)
err := peerConnection.Close() //deletes all references to this peerconnection in mem and same for ICE agent (ICE agent releases the "closed" status)
if err != nil { //https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close
fmt.Println(err)
}
}
})
//====================No retransmits, ordered dataChannel=======================
// Register channel opening handling
dataChannel.OnOpen(func() {
<-playerReady
unreliableChans.AddPlayerChan(playerTag, dataChannel)
/*
for {
time.Sleep(time.Millisecond * 50) //50 milliseconds = 20 updates per second
//20 milliseconds = ~60 updates per second
//fmt.Println(UpdatesString)
// Send the message as text so we can JSON.parse in javascript
sendErr := dataChannel.SendText(UpdatesString)
if sendErr != nil {
fmt.Println("data send err", sendErr)
break
}
}
*/
})
// Register text message handling
dataChannel.OnMessage(func(msg webrtc.DataChannelMessage) {
playerPtr.mu.Lock()
defer playerPtr.mu.Unlock()
playerPtr.room.Entities.mu.Lock()
defer playerPtr.room.Entities.mu.Unlock()
//can use non concurrent safe methods on entities below here
roomNumIndex := strings.Index(string(msg.Data), ",")
if roomNumIndex == -1 {
return
}
getRoomFromMsg := string(msg.Data[:roomNumIndex])
if getRoomFromMsg != playerPtr.room.roomKey {
return
}
msg.Data = msg.Data[roomNumIndex + 1:]
if msg.Data[0] == 'X' { //88 = "X"
if playerPtr.owner != nil {
return
}
x, err := strconv.ParseFloat(string(msg.Data[1:]), 64)
if err != nil {
fmt.Println(err)
}
// Edge of screen
if x < 0 {
x = 0
} else if x > 160 - playerPtr.width {
x = 160 - playerPtr.width
}
// Move Owned Item (playerPtr.held is an EntitiyInterface)
if playerPtr.held != nil {
playerPtr.held.Update(x - playerPtr.X, 0)
}
playerPtr.X = x
} else if msg.Data[0] == 'Y' { //89 = "Y"
if playerPtr.owner != nil {
return
}
y, err := strconv.ParseFloat(string(msg.Data[1:]), 64)
if err != nil {
fmt.Println(err)
}
if y < 0 {
y = 0
} else if y > 105 - playerPtr.height {
y = 105 - playerPtr.height
}
// Move Owned Item (playerPtr.held is an EntitiyInterface)
if playerPtr.held != nil {
playerPtr.held.Update(0, y - playerPtr.Y)
}
playerPtr.Y = y
}
})
//==============================================================================
//=========================Reliable DataChannel=================================
// Register channel opening handling
reliableChannel.OnOpen(func() {
<-playerReady
reliableChans.AddPlayerChan(playerTag, reliableChannel)
//Send Client their playerTag so they know who they are in the Updates Array
sendErr := reliableChannel.SendText("T" + playerTag)
if sendErr != nil {
panic(err)
}
})
// Register message handling (Data all served as a bytes slice []byte)
// for user controls
reliableChannel.OnMessage(func(msg webrtc.DataChannelMessage) {
playerPtr.mu.Lock()
defer playerPtr.mu.Unlock()
playerPtr.room.Entities.mu.Lock()
defer playerPtr.room.Entities.mu.Unlock()
//can use non concurrent safe methods on entities below here
roomNumIndex := strings.Index(string(msg.Data), ",")
if roomNumIndex == -1 {
return
}
getRoomFromMsg := string(msg.Data[:roomNumIndex])
if getRoomFromMsg != playerPtr.room.roomKey {
return
}
msg.Data = msg.Data[roomNumIndex + 1:]
if msg.Data[0] == 'X' { //88 = "X"
if playerPtr.owner != nil {
return
}
x, err := strconv.ParseFloat(string(msg.Data[1:]), 64)
if err != nil {
fmt.Println(err)
}
// Edge of screen
if x < 0 {
x = 0
} else if x > 160 - playerPtr.width {
x = 160 - playerPtr.width
}
// Move Owned Item (playerPtr.held is an EntitiyInterface)
if playerPtr.held != nil {
playerPtr.held.Update(x - playerPtr.X, 0)
}
playerPtr.X = x
} else if msg.Data[0] == 'Y' { //89 = "Y"
if playerPtr.owner != nil {
return
}
y, err := strconv.ParseFloat(string(msg.Data[1:]), 64)
if err != nil {
fmt.Println(err)
}
if y < 0 {
y = 0
} else if y > 105 - playerPtr.height {
y = 105 - playerPtr.height
}
// Move Owned Item (playerPtr.held is an EntitiyInterface)
if playerPtr.held != nil {
playerPtr.held.Update(0, y - playerPtr.Y)
}
playerPtr.Y = y
} else if msg.Data[0] == 'D' {
//dropped item
if playerPtr.held != nil {
playerPtr.room.Entities.entities[playerPtr.held.Key()] = playerPtr.held
playerPtr.held.SetOwner(nil)
playerPtr.held.SetRoom(room)
playerPtr.held = nil
}
} else if msg.Data[0] == 'P' && playerPtr.held == nil {
// Picked Up Item
msg.Data = msg.Data[1:]
dataSlice := strings.Split(string(msg.Data), ",")
sX := dataSlice[0]
sY := dataSlice[1]
sDir := dataSlice[2]
hitX, err := strconv.ParseFloat(sX, 64)
if err != nil {
fmt.Println(err)
}
hitY, err := strconv.ParseFloat(sY, 64)
if err != nil {
fmt.Println(err)
}
gotItem, _ := playerPtr.room.Entities.nonConcurrentSafeTryPickUpItem(playerPtr, hitX, hitY)
if gotItem {
if sDir == "" {
sDir = "l"
playerPtr.held.SetX(playerPtr.X - 10)
playerPtr.held.SetY(playerPtr.Y)
}
if sDir[0] == 'l' {
playerPtr.held.SetX(playerPtr.held.GetX() - 4)
} else if sDir[0] == 'r' {
playerPtr.held.SetX(playerPtr.held.GetX() + 4)
}
if strings.Contains(sDir, "u") {
playerPtr.held.SetY(playerPtr.held.GetY() - 4)
} else if strings.Contains(sDir, "d") {
playerPtr.held.SetY(playerPtr.held.GetY() + 4)
}
// Send this player the item key and offset so they can render it with no delay clientside
str := "I" + playerPtr.held.Key() + "," + fmt.Sprintf("%.1f", playerPtr.held.GetX()-playerPtr.X) + "," + fmt.Sprintf("%.1f", playerPtr.held.GetY()-playerPtr.Y)
reliableChans.SendToPlayer(playerTag, str)
}
} else if msg.Data[0] == 'U' { // player sent username
reliableChans.Broadcast("U" + playerTag + "," + string(msg.Data[1:]))
}
})
//==============================================================================
// Create an offer to send to the browser
offer, err := peerConnection.CreateOffer(nil)
if err != nil {
panic(err)
}
// Create channel that is blocked until ICE Gathering is complete
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
// Sets the LocalDescription, and starts our UDP listeners
err = peerConnection.SetLocalDescription(offer)
if err != nil {
panic(err)
}
// Block until ICE Gathering is complete, disabling trickle ICE
// we do this because we only can exchange one signaling message
// in a production application you should exchange ICE Candidates via OnICECandidate
<-gatherComplete
fmt.Println(*peerConnection.LocalDescription())
//Send the SDP with the final ICE candidate to the browser as our offer
err = c.WriteMessage(1, []byte(signal.Encode(*peerConnection.LocalDescription()))) //write message back to browser, 1 means message in byte format?
if err != nil {
fmt.Println("write:", err)
}
//Wait for the browser to send an answer (its SDP)
msgType, message, err2 := c.ReadMessage() //ReadMessage blocks until message received
if err2 != nil {
fmt.Println("read:", err)
}
answer := webrtc.SessionDescription{}
signal.Decode(string(message), &answer) //set answer to the decoded SDP
fmt.Println(answer, msgType)
// Set the remote SessionDescription
err = peerConnection.SetRemoteDescription(answer)
if err != nil {
panic(err)
}
//=====================Trickle ICE==============================================
//Make a new struct to use for trickle ICE candidates
var trickleCandidate webrtc.ICECandidateInit
var leftBracket uint8 = 123 //123 = ascii value of "{"
for {
_, message, err2 := c.ReadMessage() //ReadMessage blocks until message received
if err2 != nil {
fmt.Println("read:", err)
}
//If staement to make sure we aren't adding websocket error messages to ICE
if message[0] == leftBracket {
//Take []byte and turn it into a struct of type webrtc.ICECandidateInit
//(declared above as trickleCandidate)
err := json.Unmarshal(message, &trickleCandidate)
if err != nil {
fmt.Println("errorUnmarshal:", err)
}
fmt.Println(trickleCandidate)
err = peerConnection.AddICECandidate(trickleCandidate)
if err != nil {
fmt.Println("errorAddICE:", err)
}
}
}
}
// Sends current game state unreliably to all players
// (seemed better to just put in gameLoop because of weird race condition between SerializeEntities and UpdateEntities kept showing up)
func sendGameStateUnreliableLoop() {
for {
time.Sleep(time.Millisecond * 50) //50 milliseconds = 20 updates per second
Rooms.Range(func(rk, rv interface{}) bool {
switch z := rv.(type) {
case *Room:
//z.sendGameStateUnreliableChan <- true // let room update entities
// // here z is a pointer to a Room
s := z.Entities.SerializeEntities()
for k, _ := range z.Entities.Players() {
unreliableChans.SendToPlayer(k, s)
}
default:
// no match; here z has the same type as v (interface{})
}
return true
})
}
}
// Game Vars
// will contain items that can be picked up by players (mutex)
// ItemContainer & Item defined in types.go
//var strayItems ItemContainer = ItemContainer{items: make(map[string]Item)}
// will contain items with the key being the item, and each item has an Owner tag set to the playerTag who owns it
//var ownedItems ItemContainer = ItemContainer{items: make(map[string]Item)}
// var entities EntityContainer = EntityContainer{entities: make(map[string]Entity)}
var Rooms sync.Map
func initGameVars() {
InitializeRooms(&Rooms)
InitializeEntities(&Rooms)
fmt.Println("Game Ready. \n\n\n")
}
// All server orchestrated game logic
func gameLoop() {
for {
time.Sleep(time.Millisecond * 16) // 16 ms is a little faster than 60 updates per second
// Update Rooms
Rooms.Range(func(k, v interface{}) bool {
switch z := v.(type) {
case *Room:
//z.updateEntitiesChan <- true // let room update entities
// // here z is a pointer to a Room
z.updateFunc(z)
// // then send updates to players in that room
// s := z.Entities.SerializeEntities()
// for k, _ := range z.Entities.Players() {
// unreliableChans.SendToPlayer(k, s)
// }
default:
// no match; here z has the same type as v (interface{})
}
return true
})
}
}
//var playerUpdatePendingChan chan *Player = make(chan *Player)
// func handlePlayerUpdatesLoop() {
// // playerUpdatePendingChan := make(chan *Player)
// for {
// select {
// case p := <-playerUpdatePendingChan:
// p.room.Entities.mu.Lock()
// p.canUpdate <- true
// <-p.updateDone //wait for player goroutine to finish updates
// p.room.Entities.mu.Unlock()
// }
// }
// }
func main() {
/*
dat, err := os.ReadFile("./gameData/itemData.json")
if err != nil {
panic(err)
}
if err := json.Unmarshal(dat, &itemData); err != nil {
panic(err)
}
*/
initGameVars()
go sendGameStateUnreliableLoop()
go gameLoop()
// Listen on UDP Port 80, will be used for all WebRTC traffic
udpListener, err := net.ListenUDP("udp", &net.UDPAddr{
IP: net.IP{0, 0, 0, 0},
Port: 80,
})
if err != nil {
panic(err)
}
fmt.Printf("Listening for WebRTC traffic at %s\n", udpListener.LocalAddr())
// Create a SettingEngine, this allows non-standard WebRTC behavior
settingEngine := webrtc.SettingEngine{}
//Our Public Candidate is declared here cause we're not using a STUN server for discovery
//and just hardcoding the open port, and port forwarding webrtc traffic on the router
settingEngine.SetNAT1To1IPs([]string{"162.200.58.171"}, webrtc.ICECandidateTypeHost)
// settingEngine.SetNAT1To1IPs([]string{}, webrtc.ICECandidateTypeHost)
// Configure our SettingEngine to use our UDPMux. By default a PeerConnection has
// no global state. The API+SettingEngine allows the user to share state between them.
// In this case we are sharing our listening port across many.
settingEngine.SetICEUDPMux(webrtc.NewICEUDPMux(nil, udpListener))
// Create a new API using our SettingEngine
api = webrtc.NewAPI(webrtc.WithSettingEngine(settingEngine))
fileServer := http.FileServer(http.Dir("./public"))
http.HandleFunc("/echo", echo) //this request comes from webrtc.html
http.Handle("/", fileServer)
err = http.ListenAndServe(":80", nil) //Http server blocks
if err != nil {
panic(err)
}
}