2023-03-03 09:15:17 -03:00
|
|
|
|
using BattleBitAPI.Common.Enums;
|
|
|
|
|
using BattleBitAPI.Common.Serialization;
|
|
|
|
|
using BattleBitAPI.Networking;
|
2023-03-03 11:42:13 -03:00
|
|
|
|
using CommunityServerAPI.BattleBitAPI;
|
2023-03-03 09:15:17 -03:00
|
|
|
|
using System;
|
2023-03-03 11:42:13 -03:00
|
|
|
|
using System.Diagnostics;
|
2023-03-03 09:15:17 -03:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
|
|
|
|
|
namespace BattleBitAPI.Client
|
|
|
|
|
{
|
|
|
|
|
// This class was created mainly for Unity Engine, for this reason, Task async was not implemented.
|
|
|
|
|
public class Client
|
|
|
|
|
{
|
|
|
|
|
// ---- Public Variables ----
|
2023-03-03 11:42:13 -03:00
|
|
|
|
public bool IsConnected { get; set; }
|
|
|
|
|
public int GamePort { get; set; }
|
|
|
|
|
public bool IsPasswordProtected { get; set; }
|
|
|
|
|
public string ServerName { get; set; }
|
|
|
|
|
public string Gamemode { get; set; }
|
|
|
|
|
public string Map { get; set; }
|
|
|
|
|
public MapSize MapSize { get; set; }
|
|
|
|
|
public MapDayNight DayNight { get; set; }
|
|
|
|
|
public int CurrentPlayers { get; set; }
|
|
|
|
|
public int InQueuePlayers { get; set; }
|
|
|
|
|
public int MaxPlayers { get; set; }
|
|
|
|
|
public string LoadingScreenText { get; set; }
|
|
|
|
|
public string ServerRulesText { get; set; }
|
2023-03-03 09:15:17 -03:00
|
|
|
|
|
|
|
|
|
// ---- Private Variables ----
|
|
|
|
|
private TcpClient mClient;
|
|
|
|
|
private string mDestination;
|
|
|
|
|
private int mPort;
|
|
|
|
|
private bool mIsConnectingFlag;
|
|
|
|
|
|
|
|
|
|
// ---- Construction ----
|
|
|
|
|
public Client(string destination, int port)
|
|
|
|
|
{
|
|
|
|
|
this.mDestination = destination;
|
|
|
|
|
this.mPort = port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- Main Tick ----
|
|
|
|
|
public void Tick()
|
|
|
|
|
{
|
|
|
|
|
//Are we connecting?
|
|
|
|
|
if (mIsConnectingFlag)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
//Have we connected?
|
|
|
|
|
if (!this.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
//Attempt to connect to server async.
|
|
|
|
|
this.mIsConnectingFlag = true;
|
|
|
|
|
|
2023-03-03 11:42:13 -03:00
|
|
|
|
//Dispose old client if exist.
|
|
|
|
|
if (this.mClient != null)
|
|
|
|
|
{
|
|
|
|
|
try { this.mClient.Close(); } catch { }
|
|
|
|
|
try { this.mClient.Dispose(); } catch { }
|
|
|
|
|
this.mClient = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Create new client
|
|
|
|
|
this.mClient = new TcpClient();
|
|
|
|
|
this.mClient.SendBufferSize = Const.MaxNetworkPackageSize;
|
|
|
|
|
this.mClient.ReceiveBufferSize = Const.MaxNetworkPackageSize;
|
|
|
|
|
|
|
|
|
|
//Attempt to connect.
|
2023-03-03 09:15:17 -03:00
|
|
|
|
var state = mClient.BeginConnect(mDestination, mPort, (x) =>
|
|
|
|
|
{
|
|
|
|
|
this.mIsConnectingFlag = false;
|
|
|
|
|
|
2023-03-03 11:42:13 -03:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//Did we connect?
|
|
|
|
|
mClient.EndConnect(x);
|
|
|
|
|
|
|
|
|
|
var networkStream = mClient.GetStream();
|
|
|
|
|
|
|
|
|
|
//Prepare our hail package and send it.
|
|
|
|
|
using (var hail = BattleBitAPI.Common.Serialization.Stream.Get())
|
|
|
|
|
{
|
|
|
|
|
hail.Write((byte)NetworkCommuncation.Hail);
|
|
|
|
|
hail.Write((ushort)this.GamePort);
|
|
|
|
|
hail.Write(this.IsPasswordProtected);
|
|
|
|
|
hail.Write(this.ServerName);
|
|
|
|
|
hail.Write(this.Gamemode);
|
|
|
|
|
hail.Write(this.Map);
|
|
|
|
|
hail.Write((byte)this.MapSize);
|
|
|
|
|
hail.Write((byte)this.DayNight);
|
|
|
|
|
hail.Write((byte)this.CurrentPlayers);
|
|
|
|
|
hail.Write((byte)this.InQueuePlayers);
|
|
|
|
|
hail.Write((byte)this.MaxPlayers);
|
|
|
|
|
hail.Write(this.LoadingScreenText);
|
|
|
|
|
hail.Write(this.ServerRulesText);
|
|
|
|
|
|
|
|
|
|
//Send our hail package.
|
|
|
|
|
networkStream.Write(hail.Buffer, 0, hail.WritePosition);
|
|
|
|
|
networkStream.Flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Sadly can not use Task Async here, Unity isn't great with tasks.
|
|
|
|
|
var watch = Stopwatch.StartNew();
|
2023-03-03 09:15:17 -03:00
|
|
|
|
|
2023-03-03 11:42:13 -03:00
|
|
|
|
//Read the first byte.
|
|
|
|
|
NetworkCommuncation response = NetworkCommuncation.None;
|
|
|
|
|
while (watch.ElapsedMilliseconds < Const.HailConnectTimeout)
|
|
|
|
|
{
|
|
|
|
|
if (mClient.Available > 0)
|
|
|
|
|
{
|
|
|
|
|
var data = networkStream.ReadByte();
|
|
|
|
|
if (data >= 0)
|
|
|
|
|
{
|
|
|
|
|
response = (NetworkCommuncation)data;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Thread.Sleep(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Were we accepted.
|
|
|
|
|
if (response == NetworkCommuncation.Accepted)
|
|
|
|
|
{
|
|
|
|
|
//We are accepted.
|
|
|
|
|
this.IsConnected = true;
|
|
|
|
|
|
|
|
|
|
mOnConnectedToServer();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//Did we at least got a response?
|
|
|
|
|
if (response == NetworkCommuncation.None)
|
|
|
|
|
throw new Exception("Server did not respond to your connect request.");
|
|
|
|
|
|
|
|
|
|
//Try to read our deny reason.
|
|
|
|
|
if (response == NetworkCommuncation.Denied && mClient.Available > 0)
|
|
|
|
|
{
|
|
|
|
|
string errorString = null;
|
|
|
|
|
|
|
|
|
|
using (var readStream = BattleBitAPI.Common.Serialization.Stream.Get())
|
|
|
|
|
{
|
|
|
|
|
readStream.WritePosition = networkStream.Read(readStream.Buffer, 0, mClient.Available);
|
|
|
|
|
if (!readStream.TryReadString(out errorString))
|
|
|
|
|
errorString = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (errorString != null)
|
|
|
|
|
throw new Exception(errorString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Exception("Server denied our connect request with an unknown reason.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
2023-03-03 09:15:17 -03:00
|
|
|
|
{
|
2023-03-03 11:42:13 -03:00
|
|
|
|
mLogError("Unable to connect to API server: " + e.Message);
|
|
|
|
|
return;
|
2023-03-03 09:15:17 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, null);
|
|
|
|
|
|
2023-03-03 11:42:13 -03:00
|
|
|
|
//We haven't connected yet.
|
2023-03-03 09:15:17 -03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-03-03 11:42:13 -03:00
|
|
|
|
|
|
|
|
|
//We are connected at this point.
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- Callbacks ----
|
|
|
|
|
private void mOnConnectedToServer()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
private void mOnDisconnectedFromServer(string reason)
|
|
|
|
|
{
|
2023-03-03 09:15:17 -03:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 11:42:13 -03:00
|
|
|
|
|
2023-03-03 09:15:17 -03:00
|
|
|
|
// ---- Private ----
|
2023-03-03 11:42:13 -03:00
|
|
|
|
private void mLogError(string str)
|
|
|
|
|
{
|
2023-03-03 09:15:17 -03:00
|
|
|
|
|
2023-03-03 11:42:13 -03:00
|
|
|
|
}
|
|
|
|
|
private void mCloseConnection(string reason)
|
|
|
|
|
{
|
|
|
|
|
if (this.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
this.IsConnected = false;
|
2023-03-03 09:15:17 -03:00
|
|
|
|
|
2023-03-03 11:42:13 -03:00
|
|
|
|
//Dispose old client if exist.
|
|
|
|
|
if (this.mClient != null)
|
|
|
|
|
{
|
|
|
|
|
try { this.mClient.Close(); } catch { }
|
|
|
|
|
try { this.mClient.Dispose(); } catch { }
|
|
|
|
|
this.mClient = null;
|
|
|
|
|
}
|
2023-03-03 09:15:17 -03:00
|
|
|
|
|
2023-03-03 11:42:13 -03:00
|
|
|
|
mOnDisconnectedFromServer(reason);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-03 09:15:17 -03:00
|
|
|
|
}
|
|
|
|
|
}
|