gopenfusion/server/loginserver.go
CPunch 8569225ec7 major refactoring; started DB
- DB work has started in db. this will be a direct port of the OpenFusion DB format.
- LoginServer is now less of a dummy. You can create and login to accounts, and create a character to go through the tutorial with.
- config.go will host some commonly changed variables.
- protocol: fixed a bug relating to arrays being ignored while encoding packets
2023-03-09 01:48:13 -06:00

80 lines
1.7 KiB
Go

package server
import (
"fmt"
"log"
"net"
"sync"
"github.com/CPunch/GopenFusion/protocol"
)
type LoginServer struct {
listener net.Listener
clients map[*Client]bool
lock sync.Mutex
}
func NewLoginServer() *LoginServer {
listener, err := net.Listen("tcp", ":23000")
if err != nil {
log.Fatal(err)
}
return &LoginServer{
listener: listener,
clients: make(map[*Client]bool),
}
}
func (server *LoginServer) Start() {
log.Print("Server hosted on 127.0.0.1:23000")
for {
conn, err := server.listener.Accept()
if err != nil {
log.Println("Connection error: ", err)
return
}
client := NewClient(server, conn)
server.Connect(client)
go client.ClientHandler()
}
}
func (server *LoginServer) HandlePacket(client *Client, typeID uint32, pkt *protocol.Packet) {
switch typeID {
case protocol.P_CL2LS_REQ_LOGIN:
server.Login(client, pkt)
case protocol.P_CL2LS_REQ_CHECK_CHAR_NAME:
var charPkt protocol.SP_CL2LS_REQ_CHECK_CHAR_NAME
pkt.Decode(&charPkt)
client.Send(&protocol.SP_LS2CL_REP_CHECK_CHAR_NAME_SUCC{
SzFirstName: charPkt.SzFirstName,
SzLastName: charPkt.SzLastName,
}, protocol.P_LS2CL_REP_CHECK_CHAR_NAME_SUCC)
case protocol.P_CL2LS_REQ_SAVE_CHAR_NAME:
server.SaveCharacterName(client, pkt)
case protocol.P_CL2LS_REQ_CHAR_CREATE:
server.CharacterCreate(client, pkt)
default:
log.Printf("[WARN] unsupported packet ID: %x\n", typeID)
}
}
func (server *LoginServer) Disconnect(client *Client) {
server.lock.Lock()
delete(server.clients, client)
fmt.Printf("Client %p disconnected\n", client)
server.lock.Unlock()
}
func (server *LoginServer) Connect(client *Client) {
server.lock.Lock()
server.clients[client] = true
fmt.Printf("Client %p connected\n", client)
server.lock.Unlock()
}