1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-20 03:16:26 -05:00
OpenDiablo2/d2networking/d2client/d2remoteclient/remote_client_connection.go

183 lines
5.6 KiB
Go
Raw Normal View History

// Package d2remoteclient facilitates communication between a remote client and server.
2020-06-18 14:11:04 -04:00
package d2remoteclient
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"log"
"net"
"strings"
"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"
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
uuid "github.com/satori/go.uuid"
)
// RemoteClientConnection is the implementation of ClientConnection
// for a remote client.
2020-06-18 14:11:04 -04:00
type RemoteClientConnection struct {
clientListener d2networking.ClientListener // The GameClient
uniqueId string // Unique ID generated on construction
udpConnection *net.UDPConn // UDP connection to the server
active bool // The connection is currently open
2020-06-18 14:11:04 -04:00
}
// GetUniqueId returns RemoteClientConnection.uniqueId.
2020-06-18 14:11:04 -04:00
func (l RemoteClientConnection) GetUniqueId() string {
return l.uniqueId
}
// GetConnectionType returns an enum representing the connection type.
// See: d2clientconnectiontype
func (l RemoteClientConnection) GetConnectionType() d2clientconnectiontype.ClientConnectionType {
return d2clientconnectiontype.LANClient
2020-06-18 14:11:04 -04:00
}
// SendPacketToClient passes a packet to the game client for processing.
func (l *RemoteClientConnection) SendPacketToClient(packet d2netpacket.NetPacket) error {
2020-06-18 14:11:04 -04:00
return l.clientListener.OnPacketReceived(packet)
}
// Create constructs a new RemoteClientConnection
// and returns a pointer to it.
2020-06-18 14:11:04 -04:00
func Create() *RemoteClientConnection {
result := &RemoteClientConnection{
uniqueId: uuid.NewV4().String(),
}
return result
}
// Open runs serverListener() in a goroutine to continuously read UDP packets.
// It also sends a PlayerConnectionRequestPacket packet to the server (see d2netpacket).
2020-06-18 14:11:04 -04:00
func (l *RemoteClientConnection) Open(connectionString string, saveFilePath string) error {
if !strings.Contains(connectionString, ":") {
2020-06-18 14:11:04 -04:00
connectionString += ":6669"
}
// TODO: Connect to the server
udpAddress, err := net.ResolveUDPAddr("udp", connectionString)
// TODO: Show connection error screen if connection fails
if err != nil {
return err
}
l.udpConnection, err = net.DialUDP("udp", nil, udpAddress)
// TODO: Show connection error screen if connection fails
if err != nil {
return err
}
l.active = true
go l.serverListener()
log.Printf("Connected to server at %s", l.udpConnection.RemoteAddr().String())
gameState := d2player.LoadPlayerState(saveFilePath)
l.SendPacketToServer(d2netpacket.CreatePlayerConnectionRequestPacket(l.GetUniqueId(), gameState))
return nil
}
// Close informs the server that this client has disconnected and sets
// RemoteClientConnection.active to false.
2020-06-18 14:11:04 -04:00
func (l *RemoteClientConnection) Close() error {
l.active = false
l.SendPacketToServer(d2netpacket.CreatePlayerDisconnectRequestPacket(l.GetUniqueId()))
2020-06-18 14:11:04 -04:00
return nil
}
// SendPacketToServer compresses the JSON encoding of a NetPacket and
// sends it to the server.
2020-06-18 14:11:04 -04:00
func (l *RemoteClientConnection) SendPacketToServer(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()
if _, err = l.udpConnection.Write(buff.Bytes()); err != nil {
return err
}
return nil
}
// SetClientListener sets RemoteClientConnection.clientListener to the given value.
2020-06-18 14:11:04 -04:00
func (l *RemoteClientConnection) SetClientListener(listener d2networking.ClientListener) {
l.clientListener = listener
}
// serverListener runs a while loop, reading from the GameServer's UDP
// connection.
2020-06-18 14:11:04 -04:00
func (l *RemoteClientConnection) serverListener() {
buffer := make([]byte, 4096)
for l.active {
n, _, err := l.udpConnection.ReadFromUDP(buffer)
if err != nil {
fmt.Printf("Socket error: %s\n", err)
continue
}
if n <= 0 {
continue
}
buff := bytes.NewBuffer(buffer)
packetTypeId, err := buff.ReadByte()
packetType := d2netpackettype.NetPacketType(packetTypeId)
reader, err := gzip.NewReader(buff)
sb := new(strings.Builder)
io.Copy(sb, reader)
stringData := sb.String()
switch packetType {
case d2netpackettype.GenerateMap:
var packet d2netpacket.GenerateMapPacket
json.Unmarshal([]byte(stringData), &packet)
l.SendPacketToClient(d2netpacket.NetPacket{
PacketType: packetType,
PacketData: packet,
})
case d2netpackettype.MovePlayer:
var packet d2netpacket.MovePlayerPacket
json.Unmarshal([]byte(stringData), &packet)
l.SendPacketToClient(d2netpacket.NetPacket{
PacketType: packetType,
PacketData: packet,
})
case d2netpackettype.UpdateServerInfo:
var packet d2netpacket.UpdateServerInfoPacket
json.Unmarshal([]byte(stringData), &packet)
l.SendPacketToClient(d2netpacket.NetPacket{
PacketType: packetType,
PacketData: packet,
})
case d2netpackettype.AddPlayer:
var packet d2netpacket.AddPlayerPacket
json.Unmarshal([]byte(stringData), &packet)
l.SendPacketToClient(d2netpacket.NetPacket{
PacketType: packetType,
PacketData: packet,
})
case d2netpackettype.Ping:
l.SendPacketToServer(d2netpacket.CreatePongPacket(l.uniqueId))
case d2netpackettype.PlayerDisconnectionNotification:
var packet d2netpacket.PlayerDisconnectRequestPacket
json.Unmarshal([]byte(stringData), &packet)
log.Printf("Received disconnect: %s", packet.Id)
2020-06-18 14:11:04 -04:00
default:
fmt.Printf("Unknown packet type %d\n", packetType)
}
}
}