Initial commit
This commit is contained in:
75
furcadia/client.go
Normal file
75
furcadia/client.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package furcadia
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// TYPE: FurcadiaClient ///////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
type FurcadiaClient struct {
|
||||
// Furcadia character to authenticate as
|
||||
Character *Character
|
||||
|
||||
// Network connection object
|
||||
_Conn net.Conn
|
||||
}
|
||||
|
||||
func NewFurcadiaClient(c Character) *FurcadiaClient {
|
||||
return &FurcadiaClient{
|
||||
Character: &c,
|
||||
_Conn: nil,
|
||||
}
|
||||
}
|
||||
|
||||
// IsConnected returns true if this furcadia client instance had Connect()
|
||||
// called on it at least once.
|
||||
func (fc *FurcadiaClient) IsConnected() bool {
|
||||
return fc._Conn != nil
|
||||
}
|
||||
|
||||
// Connect establishes a connection to the Furcadia game server
|
||||
// (for the first time).
|
||||
//
|
||||
// Note: this method CANNOT be called twice on the same client instance!
|
||||
func (fc *FurcadiaClient) Connect(address string) error {
|
||||
if fc.IsConnected() {
|
||||
return ErrAlreadyConnected
|
||||
}
|
||||
|
||||
c, err := net.Dial("tcp", address)
|
||||
if err != nil {
|
||||
log.Printf("[%v] Cannot connect to %v: %v", fc.String(), address, err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("[%v] Connected to %v...", fc.String(), address)
|
||||
fc._Conn = c
|
||||
return nil
|
||||
}
|
||||
|
||||
// Disconnect closes an existing connection to the Furcadia game server.
|
||||
// Calling this method allows future calls to Connect() again. If this instance
|
||||
// was not connected at the time, nothing will happen and nil is returned.
|
||||
//
|
||||
// If closing the underlying network connection produces an error, it will be
|
||||
// returned back to the caller. In either case, the connection will be closed
|
||||
// and no longer owned by this instance.
|
||||
func (fc *FurcadiaClient) Disconnect() error {
|
||||
if !fc.IsConnected() {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := fc._Conn.Close()
|
||||
fc._Conn = nil
|
||||
return err
|
||||
}
|
||||
|
||||
// String returns a string representation of a FurcadiaClient instance for
|
||||
// printing/logging purposes
|
||||
func (fc *FurcadiaClient) String() string {
|
||||
return fmt.Sprintf("FurcadiaClient<%s>", fc.Character.DisplayName)
|
||||
}
|
||||
Reference in New Issue
Block a user