2020-06-29 17:01:26 -04:00
|
|
|
// Package d2localclient facilitates communication between a local client and server.
|
2020-06-13 18:32:09 -04:00
|
|
|
package d2localclient
|
|
|
|
|
|
|
|
import (
|
2020-06-30 20:01:51 -04:00
|
|
|
"log"
|
|
|
|
|
2020-06-18 14:11:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2game/d2player"
|
2020-06-13 18:32:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking"
|
2020-06-22 20:31:42 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client/d2clientconnectiontype"
|
2020-06-13 18:32:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2server"
|
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
)
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// LocalClientConnection is the implementation of ClientConnection
|
|
|
|
// for a local client.
|
2020-06-13 18:32:09 -04:00
|
|
|
type LocalClientConnection struct {
|
2020-06-29 17:01:26 -04:00
|
|
|
clientListener d2networking.ClientListener // The game client
|
|
|
|
uniqueId string // Unique ID generated on construction
|
|
|
|
openNetworkServer bool // True if this is a server
|
|
|
|
playerState *d2player.PlayerState // Local player state
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// GetUniqueId returns LocalClientConnection.uniqueId.
|
2020-06-13 18:32:09 -04:00
|
|
|
func (l LocalClientConnection) 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 LocalClientConnection) GetConnectionType() d2clientconnectiontype.ClientConnectionType {
|
|
|
|
return d2clientconnectiontype.Local
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// SendPacketToClient passes a packet to the game client for processing.
|
2020-06-13 18:32:09 -04:00
|
|
|
func (l *LocalClientConnection) SendPacketToClient(packet d2netpacket.NetPacket) error {
|
|
|
|
return l.clientListener.OnPacketReceived(packet)
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// Create constructs a new LocalClientConnection and returns
|
|
|
|
// a pointer to it.
|
2020-06-18 14:11:04 -04:00
|
|
|
func Create(openNetworkServer bool) *LocalClientConnection {
|
2020-06-13 18:32:09 -04:00
|
|
|
result := &LocalClientConnection{
|
2020-06-18 14:11:04 -04:00
|
|
|
uniqueId: uuid.NewV4().String(),
|
|
|
|
openNetworkServer: openNetworkServer,
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// Open creates a new GameServer, runs the server and connects this client to it.
|
2020-06-30 20:01:51 -04:00
|
|
|
func (l *LocalClientConnection) Open(_ string, saveFilePath string) error {
|
2020-06-18 14:11:04 -04:00
|
|
|
l.SetPlayerState(d2player.LoadPlayerState(saveFilePath))
|
|
|
|
d2server.Create(l.openNetworkServer)
|
2020-06-13 18:32:09 -04:00
|
|
|
go d2server.Run()
|
|
|
|
d2server.OnClientConnected(l)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// Close disconnects from the server and destroys it.
|
2020-06-13 18:32:09 -04:00
|
|
|
func (l *LocalClientConnection) Close() error {
|
2020-06-30 20:01:51 -04:00
|
|
|
err := l.SendPacketToServer(d2netpacket.CreateServerClosedPacket())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("LocalClientConnection: error sending ServerClosedPacket to server: %s", err)
|
|
|
|
}
|
2020-06-13 18:32:09 -04:00
|
|
|
d2server.OnClientDisconnected(l)
|
|
|
|
d2server.Destroy()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// SendPacketToServer calls d2server.OnPacketReceived with the given packet.
|
2020-06-13 18:32:09 -04:00
|
|
|
func (l *LocalClientConnection) SendPacketToServer(packet d2netpacket.NetPacket) error {
|
|
|
|
// TODO: This is going to blow up if the server has ceased to be.
|
|
|
|
return d2server.OnPacketReceived(l, packet)
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// SetClientListener sets LocalClientConnection.clientListener to the given value.
|
2020-06-13 18:32:09 -04:00
|
|
|
func (l *LocalClientConnection) SetClientListener(listener d2networking.ClientListener) {
|
|
|
|
l.clientListener = listener
|
|
|
|
}
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// GetPlayerState returns LocalClientConnection.playerState.
|
2020-06-18 14:11:04 -04:00
|
|
|
func (l *LocalClientConnection) GetPlayerState() *d2player.PlayerState {
|
|
|
|
return l.playerState
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// SetPlayerState sets LocalClientConnection.playerState to the given value.
|
2020-06-18 14:11:04 -04:00
|
|
|
func (l *LocalClientConnection) SetPlayerState(playerState *d2player.PlayerState) {
|
|
|
|
l.playerState = playerState
|
|
|
|
}
|