|
using System.Collections.Generic;
using Pfz.Threading;
using System;
using System.Runtime.Serialization;
namespace Pfz.RemoteGaming
{
/// <summary>
/// Class that must be inherited to make a game component.
/// Note that properties that are send from client to server and vice-versa must be declared as
/// abstract get/set. The remote game engine will emit the appropriate classes to deal with
/// get/set changes and to notify the other side of the changed values.
/// </summary>
[Serializable]
public abstract class RemoteGameComponent:
IAdvancedDisposable,
ISerializable
{
internal long _id;
internal RemoteGameClient _client;
internal Dictionary<int, object> _values = new Dictionary<int, object>();
internal Dictionary<int, object> _modifications = new Dictionary<int, object>();
/// <summary>
/// Finalizer. Calls Dispose(false);
/// </summary>
~RemoteGameComponent()
{
Dispose(false);
}
internal object _lock;
internal bool _wasDisposed;
/// <summary>
/// Frees all managed and unmanaged resources. Calls Dispose(true);
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases all resources used by this component.
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
var lockObject = _lock;
if (lockObject == null)
{
Dispose(false);
return;
}
lock(lockObject)
{
if (_wasDisposed)
return;
_wasDisposed = true;
}
var room = _room;
if (room != null)
{
_room = null;
if (IsPublic)
room._RemoveComponent(this);
}
var owner = _owner;
if (owner != null)
{
_owner = null;
if (!IsPublic)
owner._RemoveComponent(this);
}
_values = null;
_modifications = null;
_client = null;
}
}
/// <summary>
/// Gets a value indicating if this object was disposed.
/// </summary>
public bool WasDisposed
{
get
{
var lockObject = _lock;
if (lockObject == null)
return true;
lock(lockObject)
return _wasDisposed;
}
}
/// <summary>
/// Implement this to initialize the object. Trying to use the game properties from the constructor doesn't work.
/// In the client, the OnInitialize only happens after receiving all initial values from the server.
/// </summary>
internal protected virtual void OnInitialize()
{
}
internal RemoteGameParticipant _owner;
/// <summary>
/// Gets the owner of this component.
/// </summary>
public RemoteGameParticipant Owner
{
get
{
return _owner;
}
internal set
{
lock(_lock)
{
CheckUndisposed();
_owner = value;
OwnerId = value.ParticipantId;
}
}
}
/// <summary>
/// Throws an exception if this component was disposed.
/// </summary>
internal protected void CheckUndisposed()
{
if (_wasDisposed)
throw new ObjectDisposedException(GetType().FullName);
}
internal RemoteGameRoom _room;
/// <summary>
/// Gets the room in which this component was created.
/// </summary>
public RemoteGameRoom Room
{
get
{
return _room;
}
}
/// <summary>
/// On the server side, gets a value indicating if this component is public (seen by all participants)
/// or not. For the client, this property is always false.
/// </summary>
public bool IsPublic { get; internal set; }
/// <summary>
/// Gets the Id of the client that created this component.
/// Do not implement it, as it needs to be abstract by the engine.
/// </summary>
public abstract long OwnerId { get; set; }
/// <summary>
/// Gets a value indicating if the owner changed room, disconnected or was disposed.
/// </summary>
public abstract bool IsOwnerDisconnected { get; set; }
/// <summary>
/// This is more useful to the framework itself.
/// It tells if the component was created by a RemoteGameClient (true) or by a RemoteGameRoom/Participant (false).
/// </summary>
public bool IsClientComponent
{
get
{
return _client != null;
}
}
internal KeyValuePair<int, object>[] _GetModifications()
{
lock(_lock)
{
if (_wasDisposed)
return null;
int count = _modifications.Count;
if (count == 0)
return null;
var result = new KeyValuePair<int, object>[count];
int index = -1;
foreach(var pair in _modifications)
{
index++;
result[index] = pair;
}
_modifications.Clear();
return result;
}
}
#region ISerializable
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AssemblyName = typeof(_RemoteGameComponentReference).Assembly.FullName;
info.FullTypeName = typeof(_RemoteGameComponentReference).FullName;
info.AddValue("_ownerId", OwnerId);
info.AddValue("_id", _id);
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I started to program computers when I was 11 years old, as a hobbyist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.
At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do their work easier, faster and with less errors.
Want more info or simply want to contact me?
Take a look at:
http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com
Codeproject MVP 2012, 2015 & 2016
Microsoft MVP 2013-2014 (in October 2014 I started working at Microsoft, so I can't be a Microsoft MVP anymore).