Skip to content

Instantly share code, notes, and snippets.

@timothypratley
Created April 13, 2012 20:57
Show Gist options
  • Save timothypratley/2380150 to your computer and use it in GitHub Desktop.
Save timothypratley/2380150 to your computer and use it in GitHub Desktop.
simple world server
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DigitalIdeaSolutions.Collections.Generic;
using Newtonsoft.Json;
using ExitGames.Client.Photon.Lite;
using ExitGames.Client.Photon;
using System.Collections;
using System.Threading;
using System.Diagnostics;
namespace GraphServer
{
public class Publisher : IPhotonPeerListener
{
public bool Running { get; set; }
public string ServerName { get; private set;}
public string ApplicationName { get; private set; }
public string WorldName { get;private set; }
public ObservableDictionary<int, PhysicalEntity> PhysicalEntities { get; private set; }
private LitePeer connection;
public Publisher(ObservableDictionary<int, PhysicalEntity> physicalEntities, string serverName, string applicationName, string worldName)
{
ServerName = serverName;
ApplicationName = applicationName;
WorldName = worldName;
PhysicalEntities = physicalEntities;
connection = new LitePeer(this);
Running = true;
// TODO: should this be background?
new Thread(run).Start();
}
const int ReconnectWaitMS = 1000;
void run() {
bool isRetry = false;
while (Running)
{
if (connection.PeerState == PeerStateValue.Disconnected)
{
if (isRetry)
{
Console.WriteLine("Sleeping for " + Math.Round(ReconnectWaitMS/1000.0, 1) + " seconds...");
Thread.Sleep(ReconnectWaitMS);
}
else
{
isRetry = true;
}
Console.WriteLine("Connecting to " + ServerName);
if (!connection.Connect(ServerName, ApplicationName))
{
Console.WriteLine("Invalid connection details, ServerName:" + ServerName + " ApplicationName:" + ApplicationName);
Running = false;
return;
}
}
connection.Service();
Thread.Sleep(10);
}
}
void physicalEntities_KeyRemoved(object sender, KeyEventArgs<int> e)
{
Trace.TraceInformation(e.Key + " removed");
connection.OpRaiseEvent((byte)OpCodeEnum.RaiseEvent, new Hashtable() {
{ "remove", e.Key }
}, true);
}
void physicalEntities_KeyModified(object sender, KeyModifiedEventArgs<int, PhysicalEntity> e)
{
Trace.TraceInformation(e.Key + " old: " + JsonConvert.SerializeObject(e.PreviousValue) + " new: " + JsonConvert.SerializeObject(e.NewValue));
connection.OpRaiseEvent((byte)OpCodeEnum.RaiseEvent, Object2Hashtable(e.NewValue), true);
}
void physicalEntities_KeyAdded(object sender, KeyAddedEventArgs<int, PhysicalEntity> e)
{
Trace.TraceInformation(e.Key + " added: " + JsonConvert.SerializeObject(e.Value));
connection.OpRaiseEvent((byte)OpCodeEnum.RaiseEvent, Object2Hashtable(e.Value), true);
}
#region IPhotonPeerListener Members
public void DebugReturn(DebugLevel level, string message)
{
Console.WriteLine(level + ": " + message);
switch (level)
{
case DebugLevel.ERROR: Trace.TraceError(message); break;
case DebugLevel.WARNING: Trace.TraceWarning(message); break;
case DebugLevel.INFO: Trace.TraceInformation(message); break;
case DebugLevel.ALL: Trace.TraceInformation(message); break;
}
}
public void OnEvent(EventData eventData)
{
Console.WriteLine("OnEvent " + JsonConvert.SerializeObject(eventData));
}
public void OnOperationResponse(OperationResponse operationResponse)
{
Console.WriteLine("OperationResponse " + JsonConvert.SerializeObject(operationResponse));
if (operationResponse.ReturnCode == 0)
{
Console.WriteLine("OK - " + (OpCodeEnum)operationResponse.OperationCode
+ "(" + operationResponse.OperationCode + ")");
}
else
{
Console.WriteLine("NOK - " + (OpCodeEnum)operationResponse.OperationCode
+ "(" + operationResponse.OperationCode + ")\n ->ReturnCode=" + operationResponse.ReturnCode
+ " DebugMessage=" + operationResponse.DebugMessage);
return;
}
switch (operationResponse.OperationCode)
{
case LiteOpCode.Join:
int myActorNr = (int)operationResponse.Parameters[LiteOpKey.ActorNr];
Console.WriteLine(" ->My ActorNr is:" + myActorNr);
break;
}
}
public void OnStatusChanged(StatusCode statusCode)
{
Console.WriteLine("Status changed: " + statusCode);
switch (statusCode)
{
case StatusCode.Connect:
PhysicalEntities.KeyAdded += physicalEntities_KeyAdded;
PhysicalEntities.KeyModified += physicalEntities_KeyModified;
PhysicalEntities.KeyRemoved += physicalEntities_KeyRemoved;
Console.WriteLine("Joining " + WorldName);
connection.OpJoin(WorldName);
break;
case StatusCode.Disconnect:
PhysicalEntities.KeyAdded -= physicalEntities_KeyAdded;
PhysicalEntities.KeyModified -= physicalEntities_KeyModified;
PhysicalEntities.KeyRemoved -= physicalEntities_KeyRemoved;
break;
}
}
#endregion
public Hashtable Object2Hashtable(object obj)
{
var result = new Hashtable();
foreach (var p in obj.GetType().GetProperties())
{
result.Add(p.Name, p.GetValue(obj, null));
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment