made it clearer when session object and server object was being used by renaming s to session and server. Split out ravi,broadcast and discord into sys_*

This commit is contained in:
stratic-dev
2024-10-10 21:38:12 +01:00
parent 830834c5b5
commit 32dbfa7514
11 changed files with 367 additions and 362 deletions

View File

@@ -8,8 +8,9 @@ import (
"strings"
"sync"
"erupe-ce/config"
_config "erupe-ce/config"
"erupe-ce/network"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
)
@@ -33,50 +34,50 @@ type Config struct {
// NewServer creates a new Server type.
func NewServer(config *Config) *Server {
s := &Server{
server := &Server{
logger: config.Logger,
erupeConfig: config.ErupeConfig,
db: config.DB,
}
return s
return server
}
// Start starts the server in a new goroutine.
func (s *Server) Start() error {
func (server *Server) Start() error {
l, err := net.Listen("tcp", fmt.Sprintf(":%d", s.erupeConfig.Entrance.Port))
l, err := net.Listen("tcp", fmt.Sprintf(":%d", server.erupeConfig.Entrance.Port))
if err != nil {
return err
}
s.listener = l
server.listener = l
go s.acceptClients()
go server.acceptClients()
return nil
}
// Shutdown exits the server gracefully.
func (s *Server) Shutdown() {
s.logger.Debug("Shutting down...")
func (server *Server) Shutdown() {
server.logger.Debug("Shutting down...")
s.Lock()
s.isShuttingDown = true
s.Unlock()
server.Lock()
server.isShuttingDown = true
server.Unlock()
// This will cause the acceptor goroutine to error and exit gracefully.
s.listener.Close()
server.listener.Close()
}
// acceptClients handles accepting new clients in a loop.
func (s *Server) acceptClients() {
func (server *Server) acceptClients() {
for {
conn, err := s.listener.Accept()
conn, err := server.listener.Accept()
if err != nil {
// Check if we are shutting down and exit gracefully if so.
s.Lock()
shutdown := s.isShuttingDown
s.Unlock()
server.Lock()
shutdown := server.isShuttingDown
server.Unlock()
if shutdown {
break
@@ -86,20 +87,20 @@ func (s *Server) acceptClients() {
}
// Start a new goroutine for the connection so that we don't block other incoming connections.
go s.handleEntranceServerConnection(conn)
go server.handleEntranceServerConnection(conn)
}
}
func (s *Server) handleEntranceServerConnection(conn net.Conn) {
func (server *Server) handleEntranceServerConnection(conn net.Conn) {
defer conn.Close()
// Client initalizes the connection with a one-time buffer of 8 NULL bytes.
nullInit := make([]byte, 8)
n, err := io.ReadFull(conn, nullInit)
if err != nil {
s.logger.Warn("Failed to read 8 NULL init", zap.Error(err))
server.logger.Warn("Failed to read 8 NULL init", zap.Error(err))
return
} else if n != len(nullInit) {
s.logger.Warn("io.ReadFull couldn't read the full 8 byte init.")
server.logger.Warn("io.ReadFull couldn't read the full 8 byte init.")
return
}
@@ -107,11 +108,11 @@ func (s *Server) handleEntranceServerConnection(conn net.Conn) {
cc := network.NewCryptConn(conn)
pkt, err := cc.ReadPacket()
if err != nil {
s.logger.Warn("Error reading packet", zap.Error(err))
server.logger.Warn("Error reading packet", zap.Error(err))
return
}
if s.erupeConfig.DebugOptions.LogInboundMessages {
if server.erupeConfig.DebugOptions.LogInboundMessages {
fmt.Printf("[Client] -> [Server]\nData [%d bytes]:\n%s\n", len(pkt), hex.Dump(pkt))
}
@@ -119,9 +120,9 @@ func (s *Server) handleEntranceServerConnection(conn net.Conn) {
if strings.Split(conn.RemoteAddr().String(), ":")[0] == "127.0.0.1" {
local = true
}
data := makeSv2Resp(s.erupeConfig, s, local)
data := makeSv2Resp(server.erupeConfig, server, local)
if len(pkt) > 5 {
data = append(data, makeUsrResp(pkt, s)...)
data = append(data, makeUsrResp(pkt, server)...)
}
cc.SendPacket(data)
// Close because we only need to send the response once.

View File

@@ -12,7 +12,7 @@ import (
"net"
)
func encodeServerInfo(config *_config.Config, s *Server, local bool) []byte {
func encodeServerInfo(config *_config.Config, server *Server, local bool) []byte {
serverInfos := config.Entrance.Entries
bf := byteframe.NewByteFrame()
@@ -42,22 +42,22 @@ func encodeServerInfo(config *_config.Config, s *Server, local bool) []byte {
bf.WriteUint16(uint16(len(si.Channels)))
bf.WriteUint8(si.Type)
bf.WriteUint8(uint8(((gametime.TimeAdjusted().Unix() / 86400) + int64(serverIdx)) % 3))
if s.erupeConfig.ClientID >= _config.G1 {
if server.erupeConfig.ClientID >= _config.G1 {
bf.WriteUint8(si.Recommended)
}
fullName := append(append(stringsupport.UTF8ToSJIS(si.Name), []byte{0x00}...), stringsupport.UTF8ToSJIS(si.Description)...)
if s.erupeConfig.ClientID >= _config.G1 && s.erupeConfig.ClientID <= _config.G5 {
if server.erupeConfig.ClientID >= _config.G1 && server.erupeConfig.ClientID <= _config.G5 {
bf.WriteUint8(uint8(len(fullName)))
bf.WriteBytes(fullName)
} else {
if s.erupeConfig.ClientID >= _config.G51 {
if server.erupeConfig.ClientID >= _config.G51 {
bf.WriteUint8(0) // Ignored
}
bf.WriteBytes(stringsupport.PaddedString(string(fullName), 65, false))
}
if s.erupeConfig.ClientID >= _config.GG {
if server.erupeConfig.ClientID >= _config.GG {
bf.WriteUint32(si.AllowedClientFlags)
}
@@ -71,7 +71,7 @@ func encodeServerInfo(config *_config.Config, s *Server, local bool) []byte {
bf.WriteUint16(uint16(channelIdx | 16))
bf.WriteUint16(ci.MaxPlayers)
var currentPlayers uint16
s.db.QueryRow("SELECT current_players FROM servers WHERE server_id=$1", sid).Scan(&currentPlayers)
server.db.QueryRow("SELECT current_players FROM servers WHERE server_id=$1", sid).Scan(&currentPlayers)
bf.WriteUint16(currentPlayers)
bf.WriteUint16(0)
bf.WriteUint16(0)
@@ -86,7 +86,7 @@ func encodeServerInfo(config *_config.Config, s *Server, local bool) []byte {
}
}
bf.WriteUint32(uint32(gametime.TimeAdjusted().Unix()))
bf.WriteUint32(uint32(s.erupeConfig.GameplayOptions.ClanMemberLimits[len(s.erupeConfig.GameplayOptions.ClanMemberLimits)-1][1]))
bf.WriteUint32(uint32(server.erupeConfig.GameplayOptions.ClanMemberLimits[len(server.erupeConfig.GameplayOptions.ClanMemberLimits)-1][1]))
return bf.Data()
}
@@ -108,7 +108,7 @@ func makeHeader(data []byte, respType string, entryCount uint16, key byte) []byt
return bf.Data()
}
func makeSv2Resp(config *_config.Config, s *Server, local bool) []byte {
func makeSv2Resp(config *_config.Config, server *Server, local bool) []byte {
serverInfos := config.Entrance.Entries
// Decrease by the number of MezFes Worlds
var mf int
@@ -128,9 +128,9 @@ func makeSv2Resp(config *_config.Config, s *Server, local bool) []byte {
}
}
}
rawServerData := encodeServerInfo(config, s, local)
rawServerData := encodeServerInfo(config, server, local)
if s.erupeConfig.DebugOptions.LogOutboundMessages {
if server.erupeConfig.DebugOptions.LogOutboundMessages {
fmt.Printf("[Server] -> [Client]\nData [%d bytes]:\n%s\n", len(rawServerData), hex.Dump(rawServerData))
}
@@ -144,7 +144,7 @@ func makeSv2Resp(config *_config.Config, s *Server, local bool) []byte {
return bf.Data()
}
func makeUsrResp(pkt []byte, s *Server) []byte {
func makeUsrResp(pkt []byte, server *Server) []byte {
bf := byteframe.NewByteFrameFromBytes(pkt)
_ = bf.ReadUint32() // ALL+
_ = bf.ReadUint8() // 0x00
@@ -153,7 +153,7 @@ func makeUsrResp(pkt []byte, s *Server) []byte {
for i := 0; i < int(userEntries); i++ {
cid := bf.ReadUint32()
var sid uint16
err := s.db.QueryRow("SELECT(SELECT server_id FROM sign_sessions WHERE char_id=$1) AS _", cid).Scan(&sid)
err := server.db.QueryRow("SELECT(SELECT server_id FROM sign_sessions WHERE char_id=$1) AS _", cid).Scan(&sid)
if err != nil {
resp.WriteUint16(0)
} else {
@@ -162,7 +162,7 @@ func makeUsrResp(pkt []byte, s *Server) []byte {
resp.WriteUint16(0)
}
if s.erupeConfig.DebugOptions.LogOutboundMessages {
if server.erupeConfig.DebugOptions.LogOutboundMessages {
fmt.Printf("[Server] -> [Client]\nData [%d bytes]:\n%s\n", len(resp.Data()), hex.Dump(resp.Data()))
}