2023-03-07 20:47:49 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net"
|
2023-03-09 07:48:13 +00:00
|
|
|
"sync"
|
2023-03-07 20:47:49 +00:00
|
|
|
|
2023-03-10 07:02:28 +00:00
|
|
|
"github.com/CPunch/gopenfusion/protocol"
|
2023-03-07 20:47:49 +00:00
|
|
|
)
|
|
|
|
|
2023-03-11 02:30:16 +00:00
|
|
|
type PacketHandler func(peer *Peer, pkt protocol.Packet) error
|
|
|
|
|
2023-03-07 20:47:49 +00:00
|
|
|
type LoginServer struct {
|
2023-03-11 02:30:16 +00:00
|
|
|
listener net.Listener
|
|
|
|
packetHandlers map[uint32]PacketHandler
|
|
|
|
peers map[*Peer]bool
|
|
|
|
peersLock sync.Mutex
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 07:48:26 +00:00
|
|
|
func stubbedPacket(_ *Peer, _ protocol.Packet) error { /* stubbed */ return nil }
|
|
|
|
|
2023-03-07 20:47:49 +00:00
|
|
|
func NewLoginServer() *LoginServer {
|
|
|
|
listener, err := net.Listen("tcp", ":23000")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-03-11 02:30:16 +00:00
|
|
|
loginServer := &LoginServer{
|
2023-03-11 07:48:26 +00:00
|
|
|
listener: listener,
|
|
|
|
peers: make(map[*Peer]bool),
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|
2023-03-11 02:30:16 +00:00
|
|
|
|
2023-03-11 07:48:26 +00:00
|
|
|
loginServer.packetHandlers = map[uint32]PacketHandler{
|
|
|
|
protocol.P_CL2LS_REQ_LOGIN: loginServer.Login,
|
|
|
|
protocol.P_CL2LS_REQ_CHECK_CHAR_NAME: loginServer.CheckCharacterName,
|
|
|
|
protocol.P_CL2LS_REQ_SAVE_CHAR_NAME: loginServer.SaveCharacterName,
|
|
|
|
protocol.P_CL2LS_REQ_CHAR_CREATE: loginServer.CharacterCreate,
|
|
|
|
protocol.P_CL2LS_REQ_CHAR_SELECT: stubbedPacket,
|
|
|
|
protocol.P_CL2LS_REQ_CHAR_DELETE: loginServer.CharacterDelete,
|
|
|
|
protocol.P_CL2LS_REQ_SHARD_SELECT: stubbedPacket,
|
|
|
|
protocol.P_CL2LS_REQ_SHARD_LIST_INFO: stubbedPacket,
|
|
|
|
protocol.P_CL2LS_CHECK_NAME_LIST: stubbedPacket,
|
|
|
|
protocol.P_CL2LS_REQ_SAVE_CHAR_TUTOR: loginServer.FinishTutorial,
|
|
|
|
protocol.P_CL2LS_REQ_PC_EXIT_DUPLICATE: stubbedPacket,
|
|
|
|
protocol.P_CL2LS_REP_LIVE_CHECK: stubbedPacket,
|
|
|
|
protocol.P_CL2LS_REQ_CHANGE_CHAR_NAME: stubbedPacket,
|
|
|
|
protocol.P_CL2LS_REQ_SERVER_SELECT: stubbedPacket,
|
|
|
|
}
|
2023-03-11 02:30:16 +00:00
|
|
|
|
|
|
|
return loginServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *LoginServer) RegisterPacketHandler(typeID uint32, hndlr PacketHandler) {
|
|
|
|
server.packetHandlers[typeID] = hndlr
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (server *LoginServer) Start() {
|
|
|
|
log.Print("Server hosted on 127.0.0.1:23000")
|
|
|
|
|
|
|
|
for {
|
2023-03-09 07:48:13 +00:00
|
|
|
conn, err := server.listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Connection error: ", err)
|
|
|
|
return
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|
2023-03-09 07:48:13 +00:00
|
|
|
|
2023-03-09 22:42:13 +00:00
|
|
|
client := NewPeer(server, conn)
|
2023-03-09 07:48:13 +00:00
|
|
|
server.Connect(client)
|
2023-03-11 05:59:13 +00:00
|
|
|
go client.Handler()
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 02:30:16 +00:00
|
|
|
func (server *LoginServer) HandlePacket(peer *Peer, typeID uint32, pkt protocol.Packet) error {
|
|
|
|
if hndlr, ok := server.packetHandlers[typeID]; ok {
|
|
|
|
if err := hndlr(peer, pkt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2023-03-11 07:48:26 +00:00
|
|
|
log.Printf("[WARN] invalid packet ID: %x\n", typeID)
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|
2023-03-11 02:30:16 +00:00
|
|
|
|
|
|
|
return nil
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 22:42:13 +00:00
|
|
|
func (server *LoginServer) Disconnect(peer *Peer) {
|
2023-03-10 06:58:29 +00:00
|
|
|
server.peersLock.Lock()
|
2023-03-09 22:42:13 +00:00
|
|
|
delete(server.peers, peer)
|
2023-03-10 06:58:29 +00:00
|
|
|
server.peersLock.Unlock()
|
2023-03-09 07:48:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 22:42:13 +00:00
|
|
|
func (server *LoginServer) Connect(peer *Peer) {
|
2023-03-10 06:58:29 +00:00
|
|
|
server.peersLock.Lock()
|
2023-03-09 22:42:13 +00:00
|
|
|
server.peers[peer] = true
|
2023-03-10 06:58:29 +00:00
|
|
|
server.peersLock.Unlock()
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|