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
|
|
|
|
|
|
|
"github.com/CPunch/GopenFusion/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LoginServer struct {
|
2023-03-10 06:58:29 +00:00
|
|
|
listener net.Listener
|
|
|
|
peers map[*Peer]bool
|
|
|
|
peersLock sync.Mutex
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLoginServer() *LoginServer {
|
|
|
|
listener, err := net.Listen("tcp", ":23000")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &LoginServer{
|
2023-03-09 07:48:13 +00:00
|
|
|
listener: listener,
|
2023-03-09 22:42:13 +00:00
|
|
|
peers: make(map[*Peer]bool),
|
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)
|
|
|
|
go client.ClientHandler()
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 22:42:13 +00:00
|
|
|
func (server *LoginServer) HandlePacket(peer *Peer, typeID uint32, pkt *protocol.Packet) {
|
2023-03-07 20:47:49 +00:00
|
|
|
switch typeID {
|
|
|
|
case protocol.P_CL2LS_REQ_LOGIN:
|
2023-03-09 22:42:13 +00:00
|
|
|
server.Login(peer, pkt)
|
2023-03-07 20:47:49 +00:00
|
|
|
case protocol.P_CL2LS_REQ_CHECK_CHAR_NAME:
|
2023-03-10 06:41:21 +00:00
|
|
|
server.CheckCharacterName(peer, pkt)
|
2023-03-07 20:47:49 +00:00
|
|
|
case protocol.P_CL2LS_REQ_SAVE_CHAR_NAME:
|
2023-03-09 22:42:13 +00:00
|
|
|
server.SaveCharacterName(peer, pkt)
|
2023-03-09 07:48:13 +00:00
|
|
|
case protocol.P_CL2LS_REQ_CHAR_CREATE:
|
2023-03-09 22:42:13 +00:00
|
|
|
server.CharacterCreate(peer, pkt)
|
2023-03-10 06:41:21 +00:00
|
|
|
case protocol.P_CL2LS_REQ_CHAR_SELECT:
|
|
|
|
/* stubbed */
|
|
|
|
case protocol.P_CL2LS_REQ_CHAR_DELETE:
|
|
|
|
server.CharacterDelete(peer, pkt)
|
|
|
|
case protocol.P_CL2LS_REQ_SHARD_SELECT:
|
|
|
|
/* stubbed */
|
|
|
|
case protocol.P_CL2LS_REQ_SHARD_LIST_INFO:
|
|
|
|
/* stubbed */
|
|
|
|
case protocol.P_CL2LS_CHECK_NAME_LIST:
|
|
|
|
/* stubbed */
|
|
|
|
case protocol.P_CL2LS_REQ_SAVE_CHAR_TUTOR:
|
|
|
|
server.FinishTutorial(peer, pkt)
|
|
|
|
case protocol.P_CL2LS_REQ_PC_EXIT_DUPLICATE:
|
|
|
|
/* stubbed */
|
|
|
|
case protocol.P_CL2LS_REP_LIVE_CHECK:
|
|
|
|
/* stubbed */
|
|
|
|
case protocol.P_CL2LS_REQ_CHANGE_CHAR_NAME:
|
|
|
|
/* stubbed */
|
|
|
|
case protocol.P_CL2LS_REQ_SERVER_SELECT:
|
|
|
|
/* stubbed */
|
2023-03-07 20:47:49 +00:00
|
|
|
default:
|
|
|
|
log.Printf("[WARN] unsupported packet ID: %x\n", typeID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|