using UnityEngine;
using System.Collections;
using TNet;
/*
-----------------------
NetManager
handles connecting to an existing server or creating a new one
-----------------------
*/
public class NetManager : MonoBehaviour {
public string serverAddress = "127.0.0.1";
public int serverTcpPort = 5127;
public string remoteServerAddress = "127.0.0.1";
public int channelID = 1;
public bool autoStartLocalServer = false;
public bool autoConnect = true;
private string successFunctionName = string.Empty;
private string failureFunctionName = string.Empty;
private string gameStateFileName = "server.dat";
private float nextConnectTime = 0f;
/*
-----------------------
Start()
-----------------------
*/
void Start() {
// Make it possible to use UDP using a random port
TNManager.StartUDP( Random.Range( 10000, 50000 ) );
// automatically start a local server
if ( autoStartLocalServer ) {
int udpPort = Random.Range( 10000, 40000 );
// Start a local server, loading the saved data if possible
// The UDP port of the server doesn't matter much as it's optional,
// and the clients get notified of it via Packet.ResponseSetUDP.
TNUdpLobbyClient lan = GetComponent<TNUdpLobbyClient>();
int lobbyPort = ( lan != null ) ? lan.remotePort : 0;
TNServerInstance.Start( serverTcpPort, udpPort, gameStateFileName, lobbyPort );
Debug.LogWarning( "Server auto-started..." );
}
}
/*
-----------------------
OnDestroy()
-----------------------
*/
void OnDestroy() {
TNManager.Disconnect();
// Stop the server, saving all the data
if ( TNServerInstance.isActive ) {
TNServerInstance.Stop( gameStateFileName );
}
}
/*
-----------------------
Update()
-----------------------
*/
void Update() {
// connect to any available server
if ( autoConnect && !TNManager.isConnected && ( Time.time > nextConnectTime ) ) {
// List of discovered servers
List<ServerList.Entry> list = TNLobbyClient.knownServers.list;
// Server list automatically collects servers that have recently announced themselves
bool connecting = false;
for ( int i = 0; i < list.Count; ++i ) {
ServerList.Entry ent = list[i];
// if ( autoStartLocalServer ) {
// TNManager.Connect( ent.internalAddress.ToString() );
// Debug.LogWarning( "Connecting to " + ent.internalAddress.ToString() + "..." );
// } else {
// TNManager.Connect( ent.externalAddress.ToString() );
// Debug.LogWarning( "Connecting to " + ent.externalAddress.ToString() + "..." );
// }
TNManager.Connect( ent.externalAddress, ent.internalAddress ); // <<< returns "Invalid Arguments"
Debug.LogWarning( "Connecting to " + ent.externalAddress.ToString() + " | " + ent.internalAddress.ToString() );
connecting = true;
break;
}
if ( !connecting ) {
Debug.Log( "No servers found..." );
}
nextConnectTime = Time.time + ( (connecting) ? 10.0f : 1.0f );
}
}
/*
-----------------------
OnNetworkConnect()
on success, join a channel
-----------------------
*/
void OnNetworkConnect( bool result, string message ) {
if ( result ) {
Debug.LogWarning( "Connected > Joining channel." );
TNManager.JoinChannel( channelID, "" );
} else if ( !string.IsNullOrEmpty( failureFunctionName ) ) {
UnityTools.Broadcast( failureFunctionName, message );
autoConnect = false;
} else {
Debug.LogError( message );
autoConnect = false;
}
}
/*
-----------------------
OnNetworkDisconnect()
go back to the main menu
-----------------------
*/
void OnNetworkDisconnect() {
Debug.LogError( "Disconnected..." );
}
/*
-----------------------
OnNetworkJoinChannel()
joined a channel ( or failed trying )
-----------------------
*/
void OnNetworkJoinChannel( bool result, string message ) {
if ( result ) {
if ( !string.IsNullOrEmpty( successFunctionName ) ) {
UnityTools.Broadcast( successFunctionName );
}
Debug.LogWarning( "Joined channel!" );
} else {
if ( !string.IsNullOrEmpty( failureFunctionName ) ) {
UnityTools.Broadcast( failureFunctionName, message );
} else {
Debug.LogError( message );
}
TNManager.Disconnect();
}
}
}