2020-06-29 17:01:26 -04:00
|
|
|
// 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"
|
|
|
|
|
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"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
)
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// RemoteClientConnection is the implementation of ClientConnection
|
|
|
|
// for a remote client.
|
2020-06-18 14:11:04 -04:00
|
|
|
type RemoteClientConnection struct {
|
2020-06-29 17:01:26 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// GetUniqueId returns RemoteClientConnection.uniqueId.
|
2020-06-18 14:11:04 -04:00
|
|
|
func (l RemoteClientConnection) GetUniqueId() string {
|
|
|
|
return l.uniqueId
|
|
|
|
}
|
|
|
|
|
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 (l RemoteClientConnection) GetConnectionType() d2clientconnectiontype.ClientConnectionType {
|
|
|
|
return d2clientconnectiontype.LANClient
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -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)
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// 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 {
|
2020-06-22 11:53:44 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// 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
|
2020-06-22 20:31:42 -04:00
|
|
|
l.SendPacketToServer(d2netpacket.CreatePlayerDisconnectRequestPacket(l.GetUniqueId()))
|
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// 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,
|
|
|
|
})
|
2020-06-22 20:31:42 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|