Skip to content

Commit

Permalink
Added singleton-like pattern to NetworkManager.
Browse files Browse the repository at this point in the history
Removed numerous parameters from interfaces.
  • Loading branch information
vchelaru committed Mar 5, 2019
1 parent c8215f7 commit b2d8937
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
12 changes: 12 additions & 0 deletions INetState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RedGrin
{
public interface INetState
{
}
}
2 changes: 1 addition & 1 deletion INetworkArena.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface INetworkArena
/// <param name="entityId">The unique ID of the entity to create</param>
/// <param name="entityData">The initial state of the created entity</param>
/// <returns>The created INetworkEntity, which will be tracked by the NetworkManager</returns>
INetworkEntity RequestCreateEntity(long ownerId, long entityId, object entityData);
INetworkEntity RequestCreateEntity(object entityData);

/// <summary>
/// Called by the network manager when a message is received to destroy an entity.
Expand Down
2 changes: 1 addition & 1 deletion INetworkEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public interface INetworkEntity
/// <param name="entityState">A generic object describing an entity state </param>
/// <param name="stateTime">The time this state was received, compare to current network time to understand elapsed time</param>
/// <param name="isReckoningState">Whether or not this is a dead reckoning update</param>
void UpdateState(object entityState, double stateTime, bool isReckoningState = false);
void UpdateState(object entityState, double stateTime);
}
}
36 changes: 33 additions & 3 deletions NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ public class NetworkManager
public event NetworkEvent Connected;
public event NetworkEvent Disconnected;

static NetworkManager self;
public static NetworkManager Self
{
get
{
if(self == null)
{
throw new InvalidOperationException("NetworkManager must first be constructed in a class such as Game1");
}
return self;
}
}

/// <summary>
/// The seconds the last update happened.
/// </summary>
Expand Down Expand Up @@ -155,6 +168,7 @@ public INetworkArena GameArena
/// <param name="log">An ILogger to write messages to</param>
public NetworkManager(NetworkConfiguration config, ILogger log = null)
{
self = this;
Configuration = config;
// if no logger was provided, use NullLogger
mLog = log ?? new NullLogger();
Expand Down Expand Up @@ -421,7 +435,7 @@ private void CreateEntity(long ownerId, long entityId, object payload, double ti

if(targetEntity == null)
{
targetEntity = GameArena.RequestCreateEntity(ownerId, entityId, payload);
targetEntity = GameArena.RequestCreateEntity(payload);
}
else
{
Expand All @@ -430,9 +444,9 @@ private void CreateEntity(long ownerId, long entityId, object payload, double ti
throw new RedGrinException(msg);
}

targetEntity.UpdateState(payload, time);
targetEntity.OwnerId = ownerId;
targetEntity.EntityId = entityId;
targetEntity.UpdateState(payload, time);
mEntities.Add(targetEntity);

BroadcastIfServer(entityId, ownerId, payload, NetworkMessageType.Create);
Expand Down Expand Up @@ -472,7 +486,10 @@ private void UpdateEntity(long entityId, long ownerId, object payload, bool isRe
// ignore if null, entity creation message may not have arrived
if(targetEntity != null)
{
targetEntity.UpdateState(payload, time, isReckoning);
if(targetEntity.OwnerId != this.NetworkId || isReckoning)
{
targetEntity.UpdateState(payload, time);
}

BroadcastIfServer(entityId, targetEntity.OwnerId, payload,
isReckoning ?
Expand Down Expand Up @@ -605,6 +622,19 @@ private void SendDataMessage(INetworkEntity entity, NetworkMessageType action, N
SendDataMessage(entity.EntityId, entity.OwnerId, payload, action, recipient);
}

public string GetLocalIpAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
return null;
}

/// <summary>
/// Composes and sends a data message.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions RedGrin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="INetState.cs" />
<Compile Include="RedGrinException.cs" />
<Compile Include="INetworkArena.cs" />
<Compile Include="Logging\ILogger.cs" />
Expand Down

0 comments on commit b2d8937

Please sign in to comment.