mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-11-18 02:16:23 -05:00
Dedicated server (#871)
* My brain hurts, here is a broken dedicated server * somewhat stablised the server * Made it less shouty, and added channel * Split the DS functionallity from the main.go file * Split the DS functionallity from the main.go file, and remembered to add the server file * My brain hurts, here is a broken dedicated server * somewhat stablised the server * Made it less shouty, and added channel * Split the DS functionallity from the main.go file * Split the DS functionallity from the main.go file, and remembered to add the server file * Added Stable Dedicated Server Functionallity Co-authored-by: gravestench <dknuth0101@gmail.com>
This commit is contained in:
parent
1a6c6b8e9f
commit
f98e1267fa
74
d2networking/dedicated_server.go
Normal file
74
d2networking/dedicated_server.go
Normal file
@ -0,0 +1,74 @@
|
||||
package d2networking
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2server"
|
||||
)
|
||||
|
||||
const (
|
||||
stopServer = 0b1
|
||||
)
|
||||
|
||||
/*
|
||||
StartDedicatedServer Checks whether or not we should start a server i.e the -listen parameter has been passed in, and if so launches a
|
||||
server hosted to the network, in theory. (this is still WIP)
|
||||
*/
|
||||
func StartDedicatedServer(manager *d2asset.AssetManager, in chan byte, log chan string) (started bool, e error) {
|
||||
var listen bool
|
||||
|
||||
maxPlayers := 3
|
||||
|
||||
for argCount, arg := range os.Args {
|
||||
if arg == "--listen" || arg == "-L" {
|
||||
listen = true
|
||||
}
|
||||
|
||||
if arg == "--maxplayers" || arg == "-p" {
|
||||
max, _ := strconv.ParseInt(os.Args[argCount+1], 10, 32)
|
||||
maxPlayers = int(max)
|
||||
}
|
||||
}
|
||||
|
||||
if !listen {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
server, err := d2server.NewGameServer(manager, true, maxPlayers)
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
err = server.Start()
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// I have done the messaging with a bitmask for memory efficiency, this can easily be translated to pretty error
|
||||
// messages later, sue me.
|
||||
go func() {
|
||||
for {
|
||||
msgIn := <-in
|
||||
/* For those who do not know an AND operation denoted by & discards bits which do not line up so for instance:
|
||||
01011001 & 00000001 = 00000001 or 1
|
||||
00100101 & 00000010 = 00000000 or 0
|
||||
01100110 & 01100000 = 01100000 or 96
|
||||
these can be used to have multiple messages in just 8 bits, that's a quarter of a rune in go!
|
||||
*/
|
||||
if (msgIn & stopServer) == stopServer {
|
||||
log <- "Stopping server"
|
||||
|
||||
server.Stop()
|
||||
log <- "Exiting..."
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return true, nil
|
||||
}
|
27
main.go
27
main.go
@ -2,6 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2networking"
|
||||
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2app"
|
||||
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
||||
@ -47,6 +52,28 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
srvChanIn := make(chan byte)
|
||||
srvChanLog := make(chan string)
|
||||
started, srvErr := d2networking.StartDedicatedServer(asset, srvChanIn, srvChanLog)
|
||||
|
||||
if srvErr != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
c := make(chan os.Signal)
|
||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM) // This traps Control-c to safely shut down the server
|
||||
|
||||
go func() {
|
||||
<-c
|
||||
srvChanIn <- 0b1
|
||||
}()
|
||||
|
||||
if started {
|
||||
for data := range srvChanLog {
|
||||
log.Println(data)
|
||||
}
|
||||
}
|
||||
|
||||
audio := ebiten2.CreateAudio(asset)
|
||||
|
||||
inputManager := d2input.NewInputManager()
|
||||
|
Loading…
Reference in New Issue
Block a user