Skip to content

Commit 15d1bfa

Browse files
committed
better data handling
1 parent 53d8bbf commit 15d1bfa

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

internal/gameServer/tcp.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ const (
4242
func (g *GameServer) tcpSendFile(tcpData *TCPData, conn *net.TCPConn, withSize bool) {
4343
startTime := time.Now()
4444
var ok bool
45+
var data []byte
4546
for !ok {
4647
g.tcpMutex.Lock()
47-
_, ok = g.tcpFiles[tcpData.filename]
48+
data, ok = g.tcpFiles[tcpData.filename]
4849
g.tcpMutex.Unlock()
4950
if !ok {
5051
time.Sleep(time.Millisecond)
@@ -53,22 +54,20 @@ func (g *GameServer) tcpSendFile(tcpData *TCPData, conn *net.TCPConn, withSize b
5354
return
5455
}
5556
} else {
56-
g.tcpMutex.Lock()
5757
if withSize {
5858
size := make([]byte, 4)
59-
binary.BigEndian.PutUint32(size, uint32(len(g.tcpFiles[tcpData.filename])))
59+
binary.BigEndian.PutUint32(size, uint32(len(data)))
6060
_, err := conn.Write(size)
6161
if err != nil {
6262
g.Logger.Error(err, "could not write size", "address", conn.RemoteAddr().String())
6363
}
6464
}
65-
if len(g.tcpFiles[tcpData.filename]) > 0 {
66-
_, err := conn.Write(g.tcpFiles[tcpData.filename])
65+
if len(data) > 0 {
66+
_, err := conn.Write(data)
6767
if err != nil {
6868
g.Logger.Error(err, "could not write file", "address", conn.RemoteAddr().String())
6969
}
7070
}
71-
g.tcpMutex.Unlock()
7271

7372
// g.Logger.Info("sent save file", "filename", tcpData.filename, "filesize", tcpData.filesize, "address", conn.RemoteAddr().String())
7473
tcpData.filename = ""
@@ -96,9 +95,10 @@ func (g *GameServer) tcpSendSettings(conn *net.TCPConn) {
9695
func (g *GameServer) tcpSendCustom(conn *net.TCPConn, customID byte) {
9796
startTime := time.Now()
9897
var ok bool
98+
var data []byte
9999
for !ok {
100100
g.tcpMutex.Lock()
101-
_, ok = g.customData[customID]
101+
data, ok = g.customData[customID]
102102
g.tcpMutex.Unlock()
103103
if !ok {
104104
time.Sleep(time.Millisecond)
@@ -107,9 +107,7 @@ func (g *GameServer) tcpSendCustom(conn *net.TCPConn, customID byte) {
107107
return
108108
}
109109
} else {
110-
g.tcpMutex.Lock()
111-
_, err := conn.Write(g.customData[customID])
112-
g.tcpMutex.Unlock()
110+
_, err := conn.Write(data)
113111
if err != nil {
114112
g.Logger.Error(err, "could not write data", "address", conn.RemoteAddr().String())
115113
}

0 commit comments

Comments
 (0)