2020-06-29 17:01:26 -04:00
|
|
|
// Package d2udpclientconnection provides an implementation of a UDP client connection with a game state.
|
2020-06-18 14:11:04 -04:00
|
|
|
package d2udpclientconnection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"encoding/json"
|
|
|
|
"net"
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client/d2clientconnectiontype"
|
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2game/d2player"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
|
|
|
|
)
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// UDPClientConnection is the implementation of the
|
|
|
|
// d2server.ClientConnection interface to represent remote client from the
|
|
|
|
// server perspective.
|
2020-06-18 14:11:04 -04:00
|
|
|
type UDPClientConnection struct {
|
2020-06-29 17:01:26 -04:00
|
|
|
id string // ID of the associated RemoteClientConnection
|
|
|
|
address *net.UDPAddr // IP address of the associated RemoteClientConnection
|
|
|
|
udpConnection *net.UDPConn // Server's UDP Connection
|
|
|
|
playerState *d2player.PlayerState // Client's game state
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// CreateUDPClientConnection constructs a new UDPClientConnection and
|
|
|
|
// returns a pointer to it.
|
2020-06-18 14:11:04 -04:00
|
|
|
func CreateUDPClientConnection(udpConnection *net.UDPConn, id string, address *net.UDPAddr) *UDPClientConnection {
|
|
|
|
result := &UDPClientConnection{
|
|
|
|
id: id,
|
|
|
|
address: address,
|
|
|
|
udpConnection: udpConnection,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// GetUniqueId returns UDPClientConnection.id
|
2020-06-18 14:11:04 -04:00
|
|
|
func (u UDPClientConnection) GetUniqueId() string {
|
|
|
|
return u.id
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// GetConnectionType returns an enum representing the connection type.
|
|
|
|
// See: d2clientconnectiontype.
|
2020-06-22 20:31:42 -04:00
|
|
|
func (u UDPClientConnection) GetConnectionType() d2clientconnectiontype.ClientConnectionType {
|
|
|
|
return d2clientconnectiontype.LANClient
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// SendPacketToClient compresses the JSON encoding of a NetPacket and
|
|
|
|
// sends it to the client.
|
2020-06-18 14:11:04 -04:00
|
|
|
func (u *UDPClientConnection) SendPacketToClient(packet d2netpacket.NetPacket) error {
|
|
|
|
data, err := json.Marshal(packet.PacketData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var buff bytes.Buffer
|
|
|
|
buff.WriteByte(byte(packet.PacketType))
|
|
|
|
writer, _ := gzip.NewWriterLevel(&buff, gzip.BestCompression)
|
|
|
|
writer.Write(data)
|
|
|
|
writer.Close()
|
2020-06-22 20:31:42 -04:00
|
|
|
_, err = u.udpConnection.WriteToUDP(buff.Bytes(), u.address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-18 14:11:04 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// SetPlayerState sets UDP.playerState to the given value.
|
2020-06-18 14:11:04 -04:00
|
|
|
func (u *UDPClientConnection) SetPlayerState(playerState *d2player.PlayerState) {
|
|
|
|
u.playerState = playerState
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// GetPlayerState returns UDPClientConnection.playerState.
|
2020-06-18 14:11:04 -04:00
|
|
|
func (u *UDPClientConnection) GetPlayerState() *d2player.PlayerState {
|
|
|
|
return u.playerState
|
|
|
|
}
|