2020-06-18 14:11:04 -04:00
|
|
|
package d2remoteclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
2020-07-17 22:11:16 -04:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2020-07-02 16:29:28 -04:00
|
|
|
|
2020-07-17 22:11:16 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2game/d2player"
|
2020-06-18 14:11:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking"
|
2020-07-17 22:11:16 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client/d2clientconnectiontype"
|
2020-06-18 14:11:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
|
|
|
)
|
|
|
|
|
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
|
2020-07-02 16:29:28 -04:00
|
|
|
uniqueID string // Unique ID generated on construction
|
2020-08-09 20:32:47 -04:00
|
|
|
tcpConnection *net.TCPConn // UDP connection to the server
|
2020-06-29 17:01:26 -04:00
|
|
|
active bool // The connection is currently open
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
|
|
|
|
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{
|
2020-07-02 16:29:28 -04:00
|
|
|
uniqueID: uuid.NewV4().String(),
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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-07-02 16:29:28 -04:00
|
|
|
func (r *RemoteClientConnection) Open(connectionString, 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
|
2020-08-09 20:32:47 -04:00
|
|
|
tcpAddress, err := net.ResolveTCPAddr("tcp", connectionString)
|
2020-06-18 14:11:04 -04:00
|
|
|
|
|
|
|
// TODO: Show connection error screen if connection fails
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
r.tcpConnection, err = net.DialTCP("tcp", nil, tcpAddress)
|
2020-06-18 14:11:04 -04:00
|
|
|
// TODO: Show connection error screen if connection fails
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:29:28 -04:00
|
|
|
r.active = true
|
|
|
|
go r.serverListener()
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
log.Printf("Connected to server at %s", r.tcpConnection.RemoteAddr().String())
|
2020-06-18 14:11:04 -04:00
|
|
|
|
|
|
|
gameState := d2player.LoadPlayerState(saveFilePath)
|
2020-08-09 20:32:47 -04:00
|
|
|
packet := d2netpacket.CreatePlayerConnectionRequestPacket(r.GetUniqueID(), gameState)
|
|
|
|
err = r.SendPacketToServer(packet)
|
2020-07-02 16:29:28 -04:00
|
|
|
|
2020-06-30 20:01:51 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Print("RemoteClientConnection: error sending PlayerConnectionRequestPacket to server.")
|
|
|
|
return err
|
|
|
|
}
|
2020-06-18 14:11:04 -04:00
|
|
|
|
|
|
|
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-07-02 16:29:28 -04:00
|
|
|
func (r *RemoteClientConnection) Close() error {
|
|
|
|
r.active = false
|
|
|
|
err := r.SendPacketToServer(d2netpacket.CreatePlayerDisconnectRequestPacket(r.GetUniqueID()))
|
|
|
|
|
2020-06-30 20:01:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-22 20:31:42 -04:00
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:29:28 -04:00
|
|
|
// GetUniqueID returns RemoteClientConnection.uniqueID.
|
|
|
|
func (r RemoteClientConnection) GetUniqueID() string {
|
|
|
|
return r.uniqueID
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConnectionType returns an enum representing the connection type.
|
|
|
|
// See: d2clientconnectiontype
|
|
|
|
func (r RemoteClientConnection) GetConnectionType() d2clientconnectiontype.ClientConnectionType {
|
|
|
|
return d2clientconnectiontype.LANClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetClientListener sets RemoteClientConnection.clientListener to the given value.
|
|
|
|
func (r *RemoteClientConnection) SetClientListener(listener d2networking.ClientListener) {
|
|
|
|
r.clientListener = listener
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// SendPacketToServer compresses the JSON encoding of a NetPacket and
|
|
|
|
// sends it to the server.
|
2020-07-02 16:29:28 -04:00
|
|
|
func (r *RemoteClientConnection) SendPacketToServer(packet d2netpacket.NetPacket) error {
|
2020-08-09 20:32:47 -04:00
|
|
|
data, err := json.Marshal(packet)
|
2020-06-18 14:11:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-02 16:29:28 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
if _, err = r.tcpConnection.Write(data); err != nil {
|
2020-06-18 14:11:04 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:29:28 -04:00
|
|
|
return nil
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// serverListener runs a while loop, reading from the GameServer's TCP
|
2020-06-29 17:01:26 -04:00
|
|
|
// connection.
|
2020-07-02 16:29:28 -04:00
|
|
|
func (r *RemoteClientConnection) serverListener() {
|
2020-08-09 20:32:47 -04:00
|
|
|
var packet d2netpacket.NetPacket
|
|
|
|
decoder := json.NewDecoder(r.tcpConnection)
|
2020-07-02 16:29:28 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
for {
|
|
|
|
err := decoder.Decode(&packet)
|
2020-07-02 16:29:28 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
p, err := r.decodeToPacket(packet.PacketType, string(packet.PacketData))
|
2020-07-02 16:29:28 -04:00
|
|
|
if err != nil {
|
2020-08-09 20:32:47 -04:00
|
|
|
log.Println(packet.PacketType, err)
|
2020-07-02 16:29:28 -04:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
err = r.clientListener.OnPacketReceived(p)
|
2020-07-02 16:29:28 -04:00
|
|
|
if err != nil {
|
2020-08-09 20:32:47 -04:00
|
|
|
log.Println(packet.PacketType, err)
|
2020-07-02 16:29:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// bytesToJSON reads the packet type, decompresses the packet and returns a JSON string.
|
|
|
|
func (r *RemoteClientConnection) bytesToJSON(buffer []byte) (string, d2netpackettype.NetPacketType, error) {
|
2020-08-09 20:32:47 -04:00
|
|
|
packet, err := d2netpacket.UnmarshalNetPacket(buffer)
|
2020-07-02 16:29:28 -04:00
|
|
|
if err != nil {
|
2020-08-09 20:32:47 -04:00
|
|
|
return "", 0, err
|
2020-07-02 16:29:28 -04:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
return string(packet.PacketData), packet.PacketType, nil
|
2020-07-02 16:29:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// decodeToPacket unmarshals the JSON string into the correct struct
|
|
|
|
// and returns a NetPacket declaring that struct.
|
|
|
|
func (r *RemoteClientConnection) decodeToPacket(t d2netpackettype.NetPacketType, data string) (d2netpacket.NetPacket, error) {
|
|
|
|
var np = d2netpacket.NetPacket{}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
case d2netpackettype.GenerateMap:
|
|
|
|
var p d2netpacket.GenerateMapPacket
|
|
|
|
if err = json.Unmarshal([]byte(data), &p); err != nil {
|
|
|
|
break
|
2020-06-30 20:01:51 -04:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
np = d2netpacket.NetPacket{PacketType: t, PacketData: d2netpacket.MarshalPacket(p)}
|
2020-07-02 16:29:28 -04:00
|
|
|
|
|
|
|
case d2netpackettype.MovePlayer:
|
|
|
|
var p d2netpacket.MovePlayerPacket
|
|
|
|
if err = json.Unmarshal([]byte(data), &p); err != nil {
|
|
|
|
break
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
np = d2netpacket.NetPacket{PacketType: t, PacketData: d2netpacket.MarshalPacket(p)}
|
2020-07-02 16:29:28 -04:00
|
|
|
|
|
|
|
case d2netpackettype.UpdateServerInfo:
|
|
|
|
var p d2netpacket.UpdateServerInfoPacket
|
|
|
|
if err = json.Unmarshal([]byte(data), &p); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
np = d2netpacket.NetPacket{PacketType: t, PacketData: d2netpacket.MarshalPacket(p)}
|
2020-07-02 16:29:28 -04:00
|
|
|
|
|
|
|
case d2netpackettype.AddPlayer:
|
|
|
|
var p d2netpacket.AddPlayerPacket
|
|
|
|
if err = json.Unmarshal([]byte(data), &p); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
np = d2netpacket.NetPacket{PacketType: t, PacketData: d2netpacket.MarshalPacket(p)}
|
2020-07-02 16:29:28 -04:00
|
|
|
|
|
|
|
case d2netpackettype.Ping:
|
|
|
|
var p d2netpacket.PingPacket
|
|
|
|
if err = json.Unmarshal([]byte(data), &p); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
np = d2netpacket.NetPacket{PacketType: t, PacketData: d2netpacket.MarshalPacket(p)}
|
2020-07-02 16:29:28 -04:00
|
|
|
|
|
|
|
case d2netpackettype.PlayerDisconnectionNotification:
|
|
|
|
var p d2netpacket.PlayerDisconnectRequestPacket
|
|
|
|
if err = json.Unmarshal([]byte(data), &p); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
np = d2netpacket.NetPacket{PacketType: t, PacketData: d2netpacket.MarshalPacket(p)}
|
2020-07-02 16:29:28 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("RemoteClientConnection: unrecognized packet type: %v", t)
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
2020-07-02 16:29:28 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return np, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return np, nil
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|