#if !FishyFacepunch using FishNet.Managing; using FishNet.Transporting; using Steamworks; using System; using UnityEngine; namespace FishyFacepunch { public class FishyFacepunch : Transport { ~FishyFacepunch() { Shutdown(); } #region Public. [System.NonSerialized] public ulong LocalUserSteamID; #endregion #region Serialized. /// /// Steam application Id. /// [Tooltip("Steam application Id.")] [SerializeField] private uint _steamAppID = 480; [Header("Server")] /// /// Address server should bind to. /// [Tooltip("Address server should bind to.")] [SerializeField] private string _serverBindAddress = string.Empty; /// /// Port to use. /// [Tooltip("Port to use.")] [SerializeField] private ushort _port = 27015; /// /// Maximum number of players which may be connected at once. /// [Tooltip("Maximum number of players which may be connected at once.")] [Range(1, ushort.MaxValue)] [SerializeField] private ushort _maximumClients = 16; [Header("Client")] /// /// Address client should connect to. /// [Tooltip("Address client should connect to.")] [SerializeField] private string _clientAddress = string.Empty; [Tooltip("Timeout for connecting in seconds.")] [SerializeField] private int _timeout = 25; #endregion #region Private. /// /// MTUs for each channel. /// private int[] _mtus; /// /// Client for the transport. /// private Client.ClientSocket _client = new Client.ClientSocket(); /// /// Client when acting as host. /// private Client.ClientHostSocket _clientHost = new Client.ClientHostSocket(); /// /// Server for the transport. /// private Server.ServerSocket _server = new Server.ServerSocket(); #endregion #region Const. /// /// Id to use for client when acting as host. /// internal const int CLIENT_HOST_ID = short.MaxValue; #endregion #region Initialization and Unity. public override void Initialize(NetworkManager networkManager, int transportIndex) { base.Initialize(networkManager, transportIndex); CreateChannelData(); #if !UNITY_SERVER SteamClient.Init(_steamAppID, true); SteamNetworking.AllowP2PPacketRelay(true); #endif _clientHost.Initialize(this); _client.Initialize(this); _server.Initialize(this); } private void OnDestroy() { Shutdown(); } private void Update() { _clientHost.CheckSetStarted(); } #endregion #region Setup. /// /// Creates ChannelData for the transport. /// private void CreateChannelData() { _mtus = new int[2] { 1048576, 1200 }; } /// /// Tries to initialize steam network access. /// private void InitializeRelayNetworkAccess() { #if !UNITY_SERVER SteamNetworkingUtils.InitRelayNetworkAccess(); LocalUserSteamID = Steamworks.SteamClient.SteamId.Value; #endif } #endregion #region ConnectionStates. /// /// Gets the IP address of a remote connection Id. /// /// /// public override string GetConnectionAddress(int connectionId) { return _server.GetConnectionAddress(connectionId); } /// /// Called when a connection state changes for the local client. /// public override event Action OnClientConnectionState; /// /// Called when a connection state changes for the local server. /// public override event Action OnServerConnectionState; /// /// Called when a connection state changes for a remote client. /// public override event Action OnRemoteConnectionState; /// /// Gets the current local ConnectionState. /// /// True if getting ConnectionState for the server. public override LocalConnectionState GetConnectionState(bool server) { if (server) return _server.GetLocalConnectionState(); else return _client.GetLocalConnectionState(); } /// /// Gets the current ConnectionState of a remote client on the server. /// /// ConnectionId to get ConnectionState for. public override RemoteConnectionState GetConnectionState(int connectionId) { return _server.GetConnectionState(connectionId); } /// /// Handles a ConnectionStateArgs for the local client. /// /// public override void HandleClientConnectionState(ClientConnectionStateArgs connectionStateArgs) { OnClientConnectionState?.Invoke(connectionStateArgs); } /// /// Handles a ConnectionStateArgs for the local server. /// /// public override void HandleServerConnectionState(ServerConnectionStateArgs connectionStateArgs) { OnServerConnectionState?.Invoke(connectionStateArgs); } /// /// Handles a ConnectionStateArgs for a remote client. /// /// public override void HandleRemoteConnectionState(RemoteConnectionStateArgs connectionStateArgs) { OnRemoteConnectionState?.Invoke(connectionStateArgs); } #endregion #region Iterating. /// /// Processes data received by the socket. /// /// True to process data received on the server. public override void IterateIncoming(bool server) { if (server) { _server.IterateIncoming(); } else { _client.IterateIncoming(); _clientHost.IterateIncoming(); } } /// /// Processes data to be sent by the socket. /// /// True to process data received on the server. public override void IterateOutgoing(bool server) { if (server) _server.IterateOutgoing(); else _client.IterateOutgoing(); } #endregion #region ReceivedData. /// /// Called when client receives data. /// public override event Action OnClientReceivedData; /// /// Handles a ClientReceivedDataArgs. /// /// public override void HandleClientReceivedDataArgs(ClientReceivedDataArgs receivedDataArgs) { OnClientReceivedData?.Invoke(receivedDataArgs); } /// /// Called when server receives data. /// public override event Action OnServerReceivedData; /// /// Handles a ClientReceivedDataArgs. /// /// public override void HandleServerReceivedDataArgs(ServerReceivedDataArgs receivedDataArgs) { OnServerReceivedData?.Invoke(receivedDataArgs); } #endregion #region Sending. /// /// Sends to the server or all clients. /// /// Channel to use. /// /// Data to send. public override void SendToServer(byte channelId, ArraySegment segment) { _client.SendToServer(channelId, segment); _clientHost.SendToServer(channelId, segment); } /// /// Sends data to a client. /// /// /// /// public override void SendToClient(byte channelId, ArraySegment segment, int connectionId) { _server.SendToClient(channelId, segment, connectionId); } #endregion #region Configuration. /// /// Returns the maximum number of clients allowed to connect to the server. If the transport does not support this method the value -1 is returned. /// /// public override int GetMaximumClients() { return _server.GetMaximumClients(); } /// /// Sets maximum number of clients allowed to connect to the server. If applied at runtime and clients exceed this value existing clients will stay connected but new clients may not connect. /// /// public override void SetMaximumClients(int value) { _server.SetMaximumClients(value); } /// /// Sets which address the client will connect to. /// /// public override void SetClientAddress(string address) { _clientAddress = address; } public override void SetServerBindAddress(string address, IPAddressType addressType) { _serverBindAddress = address; } /// /// Sets which port to use. /// /// public override void SetPort(ushort port) { _port = port; } /// /// Returns the adjusted timeout as float /// /// public override float GetTimeout(bool asServer) { return _timeout; } #endregion #region Start and stop. /// /// Starts the local server or client using configured settings. /// /// True to start server. public override bool StartConnection(bool server) { Debug.Log("StartConnection fishy server: " + server); if (server) return StartServer(); else return StartClient(_clientAddress); } /// /// Stops the local server or client. /// /// True to stop server. public override bool StopConnection(bool server) { if (server) return StopServer(); else return StopClient(); } /// /// Stops a remote client from the server, disconnecting the client. /// /// ConnectionId of the client to disconnect. /// True to abrutly stp the client socket without waiting socket thread. public override bool StopConnection(int connectionId, bool immediately) { return StopClient(connectionId, immediately); } /// /// Stops both client and server. /// public override void Shutdown() { //Stops client then server connections. StopConnection(false); StopConnection(true); } #region Privates. /// /// Starts server. /// /// True if there were no blocks. A true response does not promise a socket will or has connected. private bool StartServer() { bool clientRunning = false; #if !UNITY_SERVER if (!SteamClient.IsValid) { Debug.LogError("Steam Facepunch not initialized. Server could not be started."); return false; } //if (_client.GetLocalConnectionState() != LocalConnectionState.Stopped) //{ // Debug.LogError("Server cannot run while client is running."); // return false; //} clientRunning = (_client.GetLocalConnectionState() != LocalConnectionState.Stopped); /* If remote _client is running then stop it * and start the client host variant. */ if (clientRunning) _client.StopConnection(); #endif _server.ResetInvalidSocket(); if (_server.GetLocalConnectionState() != LocalConnectionState.Stopped) { Debug.LogError("Server is already running."); return false; } InitializeRelayNetworkAccess(); bool result = _server.StartConnection(_serverBindAddress, _port, _maximumClients); //If need to restart client. if (result && clientRunning) StartConnection(false); return result; } /// /// Stops server. /// private bool StopServer() { return _server.StopConnection(); } /// /// Starts the client. /// /// /// True if there were no blocks. A true response does not promise a socket will or has connected. private bool StartClient(string address) { if (!SteamClient.IsValid) { Debug.LogError("Steam Facepunch not initialized. Client could not be started."); return false; } //If not acting as a host. if (_server.GetLocalConnectionState() == LocalConnectionState.Stopped) { if (_client.GetLocalConnectionState() != LocalConnectionState.Stopped) { Debug.LogError("Client is already running."); return false; } //Stop client host if running. if (_clientHost.GetLocalConnectionState() != LocalConnectionState.Stopped) _clientHost.StopConnection(); //Initialize. InitializeRelayNetworkAccess(); _client.StartConnection(address, _port); } //Acting as host. else { _clientHost.StartConnection(_server); } return true; } /// /// Stops the client. /// private bool StopClient() { bool result = false; result |= _client.StopConnection(); result |= _clientHost.StopConnection(); return result; } /// /// Stops a remote client on the server. /// /// /// True to abrutly stp the client socket without waiting socket thread. private bool StopClient(int connectionId, bool immediately) { return _server.StopConnection(connectionId); } #endregion #endregion #region Channels. /// /// Gets the MTU for a channel. This should take header size into consideration. /// For example, if MTU is 1200 and a packet header for this channel is 10 in size, this method should return 1190. /// /// /// public override int GetMTU(byte channel) { if (channel >= _mtus.Length) { Debug.LogError($"Channel {channel} is out of bounds."); return 0; } return _mtus[channel]; } #endregion } } #endif // !DISABLESTEAMWORKS