2023-03-07 20:47:49 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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-09 07:48:13 +00:00
|
|
|
listener net.Listener
|
|
|
|
clients map[*Client]bool
|
|
|
|
lock 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,
|
|
|
|
clients: make(map[*Client]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
|
|
|
|
|
|
|
client := NewClient(server, conn)
|
|
|
|
server.Connect(client)
|
|
|
|
go client.ClientHandler()
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 07:48:13 +00:00
|
|
|
func (server *LoginServer) HandlePacket(client *Client, typeID uint32, pkt *protocol.Packet) {
|
2023-03-07 20:47:49 +00:00
|
|
|
switch typeID {
|
|
|
|
case protocol.P_CL2LS_REQ_LOGIN:
|
2023-03-09 07:48:13 +00:00
|
|
|
server.Login(client, pkt)
|
2023-03-07 20:47:49 +00:00
|
|
|
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:
|
2023-03-09 07:48:13 +00:00
|
|
|
server.SaveCharacterName(client, pkt)
|
|
|
|
case protocol.P_CL2LS_REQ_CHAR_CREATE:
|
|
|
|
server.CharacterCreate(client, pkt)
|
2023-03-07 20:47:49 +00:00
|
|
|
default:
|
|
|
|
log.Printf("[WARN] unsupported packet ID: %x\n", typeID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 07:48:13 +00:00
|
|
|
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()
|
2023-03-07 20:47:49 +00:00
|
|
|
}
|