2023-03-07 20:47:49 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-03-17 21:27:47 +00:00
|
|
|
"fmt"
|
2023-03-07 20:47:49 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2023-03-09 07:48:13 +00:00
|
|
|
"sync"
|
2023-03-07 20:47:49 +00:00
|
|
|
|
2023-03-22 05:30:58 +00:00
|
|
|
"github.com/CPunch/gopenfusion/core/db"
|
|
|
|
"github.com/CPunch/gopenfusion/core/protocol"
|
2023-03-07 20:47:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LoginServer struct {
|
2023-03-11 02:30:16 +00:00
|
|
|
listener net.Listener
|
2023-03-17 21:27:47 +00:00
|
|
|
port int
|
|
|
|
dbHndlr *db.DBHandler
|
2023-03-11 02:30:16 +00:00
|
|
|
packetHandlers map[uint32]PacketHandler
|
2023-03-18 21:40:20 +00:00
|
|
|
peers map[*protocol.CNPeer]bool
|
2023-03-11 02:30:16 +00:00
|
|
|
peersLock sync.Mutex
|
2023-03-17 21:27:47 +00:00
|
|
|
shard *ShardServer
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 21:27:47 +00:00
|
|
|
func NewLoginServer(dbHndlr *db.DBHandler, port int) (*LoginServer, error) {
|
|
|
|
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
2023-03-07 20:47:49 +00:00
|
|
|
if err != nil {
|
2023-03-17 21:27:47 +00:00
|
|
|
return nil, err
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 21:27:47 +00:00
|
|
|
server := &LoginServer{
|
2023-03-11 07:48:26 +00:00
|
|
|
listener: listener,
|
2023-03-17 21:27:47 +00:00
|
|
|
port: port,
|
|
|
|
dbHndlr: dbHndlr,
|
2023-03-18 21:40:20 +00:00
|
|
|
peers: make(map[*protocol.CNPeer]bool),
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|
2023-03-11 02:30:16 +00:00
|
|
|
|
2023-03-17 21:27:47 +00:00
|
|
|
server.packetHandlers = map[uint32]PacketHandler{
|
|
|
|
protocol.P_CL2LS_REQ_LOGIN: server.Login,
|
|
|
|
protocol.P_CL2LS_REQ_CHECK_CHAR_NAME: server.CheckCharacterName,
|
|
|
|
protocol.P_CL2LS_REQ_SAVE_CHAR_NAME: server.SaveCharacterName,
|
|
|
|
protocol.P_CL2LS_REQ_CHAR_CREATE: server.CharacterCreate,
|
|
|
|
protocol.P_CL2LS_REQ_CHAR_SELECT: server.ShardSelect,
|
|
|
|
protocol.P_CL2LS_REQ_CHAR_DELETE: server.CharacterDelete,
|
2023-03-11 07:48:26 +00:00
|
|
|
protocol.P_CL2LS_REQ_SHARD_SELECT: stubbedPacket,
|
|
|
|
protocol.P_CL2LS_REQ_SHARD_LIST_INFO: stubbedPacket,
|
|
|
|
protocol.P_CL2LS_CHECK_NAME_LIST: stubbedPacket,
|
2023-03-17 21:27:47 +00:00
|
|
|
protocol.P_CL2LS_REQ_SAVE_CHAR_TUTOR: server.FinishTutorial,
|
2023-03-11 07:48:26 +00:00
|
|
|
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
|
|
|
|
2023-03-17 21:27:47 +00:00
|
|
|
return server, nil
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (server *LoginServer) Start() {
|
2023-03-17 21:27:47 +00:00
|
|
|
log.Printf("Server hosted on 127.0.0.1:%d\n", server.port)
|
2023-03-07 20:47:49 +00:00
|
|
|
|
|
|
|
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-18 21:40:20 +00:00
|
|
|
client := protocol.NewCNPeer(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-18 21:40:20 +00:00
|
|
|
func (server *LoginServer) HandlePacket(peer *protocol.CNPeer, typeID uint32, pkt protocol.Packet) error {
|
2023-03-11 02:30:16 +00:00
|
|
|
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-18 21:40:20 +00:00
|
|
|
func (server *LoginServer) Disconnect(peer *protocol.CNPeer) {
|
2023-03-10 06:58:29 +00:00
|
|
|
server.peersLock.Lock()
|
2023-03-17 21:27:47 +00:00
|
|
|
log.Printf("Peer %p disconnected from LOGIN\n", peer)
|
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-18 21:40:20 +00:00
|
|
|
func (server *LoginServer) Connect(peer *protocol.CNPeer) {
|
2023-03-10 06:58:29 +00:00
|
|
|
server.peersLock.Lock()
|
2023-03-17 21:27:47 +00:00
|
|
|
log.Printf("New peer %p connected to LOGIN\n", peer)
|
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
|
|
|
}
|
2023-03-17 21:27:47 +00:00
|
|
|
|
|
|
|
func (server *LoginServer) AddShard(shard *ShardServer) {
|
|
|
|
server.shard = shard
|
|
|
|
}
|