Skip to content

Commit

Permalink
feat: Added start, stop and shutdown to RuffleSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoTenPvP committed Sep 11, 2019
1 parent bef7738 commit 49b0817
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Ruffles.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public static void Main(string[] args)

RuffleSocket client = new RuffleSocket(ClientConfig);

client.Start();
server.Start();

if (IPv6)
{
// IPv6 Connect
Expand Down
104 changes: 87 additions & 17 deletions Ruffles/Core/RuffleSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ internal void PublishEvent(NetworkEvent @event)

private readonly MemoryManager memoryManager;

private readonly Thread _networkThread;
private Thread _networkThread;

public bool IsRunning { get; private set; }
public bool IsTerminated { get; private set; }

private bool _initialized;

public RuffleSocket(SocketConfig config)
{
if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogInfo("Checking SokcetConfig validity...");
Expand Down Expand Up @@ -107,29 +112,94 @@ public RuffleSocket(SocketConfig config)
challengeInitializationVectors = new SlidingSet<ulong>((int)config.ConnectionChallengeHistory, true);

if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogInfo("Allocating memory manager");
memoryManager = new MemoryManager(config);

if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogInfo("Binding socket");
bool bindSuccess = Bind(config.IPv4ListenAddress, config.IPv6ListenAddress, config.DualListenPort, config.UseIPv6Dual);
memoryManager = new MemoryManager(config);
}

if (!bindSuccess)
{
if (Logging.CurrentLogLevel <= LogLevel.Error) Logging.LogError("Failed to bind socket");
}
else
/// <summary>
/// Starts the socket.
/// </summary>
public bool Start()
{
if (IsRunning)
{
if (Logging.CurrentLogLevel <= LogLevel.Info) Logging.LogInfo("Socket was successfully bound");
throw new InvalidOperationException("Socket already started");
}

_networkThread = new Thread(StartNetworkLogic)

if (!_initialized)
{
if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogInfo("Binding socket");
bool bindSuccess = Bind(config.IPv4ListenAddress, config.IPv6ListenAddress, config.DualListenPort, config.UseIPv6Dual);

if (!bindSuccess)
{
if (Logging.CurrentLogLevel <= LogLevel.Error) Logging.LogError("Failed to bind socket");
return false;
}
else
{
if (Logging.CurrentLogLevel <= LogLevel.Info) Logging.LogInfo("Socket was successfully bound");
_initialized = true;
}
}

if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogInfo("Starting networking thread");

// Create network thread
_networkThread = new Thread(StartNetworkLogic)
{
Name = "NetworkThread",
IsBackground = true
IsBackground = true
};

if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogInfo("Starting networking thread");
// Set running state to true
IsRunning = true;

// Start network thread
_networkThread.Start();
}

return true;
}

/// <summary>
/// Stops the socket.
/// </summary>
public void Stop()
{
if (IsRunning)
{
throw new InvalidOperationException("Cannot stop a non running socket");
}

// Disconnect all clients
for (int i = 0; i < connections.Length; i++)
{
if (!connections[i].Dead && connections[i].State != ConnectionState.Disconnected)
{
connections[i].Disconnect(true);
}
}

IsRunning = false;
_networkThread.Join();
}

/// <summary>
/// Shuts the socket down.
/// </summary>
public void Shutdown()
{
if (!_initialized)
{
throw new InvalidOperationException("Cannot shutdown a non initialized socket");
}

IsTerminated = true;
_initialized = false;

Stop();
ipv4Socket.Close();
ipv6Socket.Close();
}

private bool Bind(IPAddress addressIPv4, IPAddress addressIPv6, int port, bool ipv6Dual)
{
Expand Down Expand Up @@ -417,7 +487,7 @@ public bool Disconnect(ulong connectionId, bool sendMessage)

private void StartNetworkLogic()
{
while (true)
while (IsRunning)
{
try
{
Expand Down

0 comments on commit 49b0817

Please sign in to comment.