Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from ggmolly/feat/portable-tcp-server
Browse files Browse the repository at this point in the history
refactor: TCP server to use standard library instead of syscalls
  • Loading branch information
Molly authored Jun 15, 2024
2 parents 955536f + 24875fe commit 2e473de
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 165 deletions.
4 changes: 1 addition & 3 deletions answer/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package answer
import (
"bytes"
"encoding/json"
"syscall"

"github.com/ggmolly/belfast/connection"
"github.com/ggmolly/belfast/protobuf"
Expand Down Expand Up @@ -36,8 +35,7 @@ func Forge_SC8239(buffer *[]byte, client *connection.Client) (int, int, error) {
}
answerBuffer.Write(jsonData)

// Write buffer to fd
n, err := syscall.Write(client.FD, answerBuffer.Bytes())
n, err := (*client.Connection).Write(answerBuffer.Bytes())
if err != nil {
return 0, packetId, err
}
Expand Down
23 changes: 10 additions & 13 deletions connection/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"fmt"
"math/rand"
"net"
"syscall"
"time"

"github.com/ggmolly/belfast/logger"
"github.com/ggmolly/belfast/orm"
"github.com/ggmolly/belfast/protobuf"
"google.golang.org/protobuf/proto"
)

Expand All @@ -18,13 +18,12 @@ var (
)

type Client struct {
SockAddr syscall.Sockaddr
ProxyFD int // only used in proxy strategy, contains the fd of the proxy client
IP net.IP
Port int
FD int
State int
PacketIndex int
Hash uint32
Connection *net.Conn
Commander *orm.Commander
Buffer bytes.Buffer
Server *Server
Expand Down Expand Up @@ -104,19 +103,17 @@ func (client *Client) GetCommander(accountId uint32) error {
return err
}

func (client *Client) Kill() {
if err := syscall.EpollCtl(client.Server.EpollFD, syscall.EPOLL_CTL_DEL, client.FD, nil); err != nil {
logger.LogEvent("Client", "Kill()", fmt.Sprintf("%s:%d -> %v", client.IP, client.Port, err), logger.LOG_LEVEL_ERROR)
}
if err := syscall.Close(client.FD); err != nil {
logger.LogEvent("Client", "Kill()", fmt.Sprintf("%s:%d -> %v", client.IP, client.Port, err), logger.LOG_LEVEL_ERROR)
return
}
// Sends SC_10999 (disconnected from server) message to the Client, reasons are defined in consts/disconnect_reasons.go
func (client *Client) Disconnect(reason uint8) error {
_, _, err := SendProtoMessage(10999, client, &protobuf.SC_10999{
Reason: proto.Uint32(uint32(reason)),
})
return err
}

// Sends the content of the buffer to the client via TCP
func (client *Client) Flush() {
_, err := syscall.Write(client.FD, client.Buffer.Bytes())
_, err := (*client.Connection).Write(client.Buffer.Bytes())
if err != nil {
logger.LogEvent("Client", "Flush()", fmt.Sprintf("%s:%d -> %v", client.IP, client.Port, err), logger.LOG_LEVEL_ERROR)
}
Expand Down
Loading

0 comments on commit 2e473de

Please sign in to comment.