1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-02 22:11:10 +00:00
OpenDiablo2/d2networking/d2client/d2localclient/local_client_connection.go
danhale-git c27fb572bc
D2networking resolve lint issues (#497)
* Added automap.go stub

* Handle errors in original AutoMap.txt file

* Completed AutoMapRecord struct and comments

* AutoMap loader implemented

* Update from base repo

* Comments added to d2netpacket and d2netpackettype.

Note, the Overview for d2netpacket is in net_packet.go. It could be placed in a doc.go file but net_packet.go seemed appropriate in this case.

* Comments added to d2server

* client_connection.go missed from previous commit

* Comments added to d2client

* Doc.go added to d2networking and other corrections

* Unused parameter present to satisfy interface, named it '_'

* Unhandled errors and other issues resolved in d2networking.

This commit adds simple error reporting and creates a lot of mess. I will be doing one more pass to tidy things up a bit before I move on.

* Commented out continue on RemoteClientConnection error and implemented stringer in message type

* NetPacketType.String uses map instead of switch.

* Map moved into String method
2020-06-30 20:01:51 -04:00

91 lines
3.2 KiB
Go

// Package d2localclient facilitates communication between a local client and server.
package d2localclient
import (
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2game/d2player"
"github.com/OpenDiablo2/OpenDiablo2/d2networking"
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client/d2clientconnectiontype"
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2server"
uuid "github.com/satori/go.uuid"
)
// LocalClientConnection is the implementation of ClientConnection
// for a local client.
type LocalClientConnection struct {
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
}
// GetUniqueId returns LocalClientConnection.uniqueId.
func (l LocalClientConnection) GetUniqueId() string {
return l.uniqueId
}
// GetConnectionType returns an enum representing the connection type.
// See: d2clientconnectiontype
func (l LocalClientConnection) GetConnectionType() d2clientconnectiontype.ClientConnectionType {
return d2clientconnectiontype.Local
}
// SendPacketToClient passes a packet to the game client for processing.
func (l *LocalClientConnection) SendPacketToClient(packet d2netpacket.NetPacket) error {
return l.clientListener.OnPacketReceived(packet)
}
// Create constructs a new LocalClientConnection and returns
// a pointer to it.
func Create(openNetworkServer bool) *LocalClientConnection {
result := &LocalClientConnection{
uniqueId: uuid.NewV4().String(),
openNetworkServer: openNetworkServer,
}
return result
}
// Open creates a new GameServer, runs the server and connects this client to it.
func (l *LocalClientConnection) Open(_ string, saveFilePath string) error {
l.SetPlayerState(d2player.LoadPlayerState(saveFilePath))
d2server.Create(l.openNetworkServer)
go d2server.Run()
d2server.OnClientConnected(l)
return nil
}
// Close disconnects from the server and destroys it.
func (l *LocalClientConnection) Close() error {
err := l.SendPacketToServer(d2netpacket.CreateServerClosedPacket())
if err != nil {
log.Printf("LocalClientConnection: error sending ServerClosedPacket to server: %s", err)
}
d2server.OnClientDisconnected(l)
d2server.Destroy()
return nil
}
// SendPacketToServer calls d2server.OnPacketReceived with the given packet.
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)
}
// SetClientListener sets LocalClientConnection.clientListener to the given value.
func (l *LocalClientConnection) SetClientListener(listener d2networking.ClientListener) {
l.clientListener = listener
}
// GetPlayerState returns LocalClientConnection.playerState.
func (l *LocalClientConnection) GetPlayerState() *d2player.PlayerState {
return l.playerState
}
// SetPlayerState sets LocalClientConnection.playerState to the given value.
func (l *LocalClientConnection) SetPlayerState(playerState *d2player.PlayerState) {
l.playerState = playerState
}