[성현모] Http, Socket 통신 추가
This commit is contained in:
29
Projects/HubX/HubX.Library/Socket/Object/Client.cs
Normal file
29
Projects/HubX/HubX.Library/Socket/Object/Client.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using HubX.Library.Socket.Session;
|
||||
|
||||
namespace HubX.Library.Socket.Object
|
||||
{
|
||||
public class Client
|
||||
{
|
||||
|
||||
public EnumObjectType ObjectType = EnumObjectType.Client;
|
||||
|
||||
private int ClientId;
|
||||
public int Id
|
||||
{
|
||||
get { return ClientId; }
|
||||
set { ClientId = value; }
|
||||
}
|
||||
|
||||
public ClientSession Session { get; set; }
|
||||
|
||||
public Client()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Projects/HubX/HubX.Library/Socket/Object/EnumObjectType.cs
Normal file
14
Projects/HubX/HubX.Library/Socket/Object/EnumObjectType.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HubX.Library.Socket.Object
|
||||
{
|
||||
public enum EnumObjectType
|
||||
{
|
||||
NONE = 0,
|
||||
Client = 1,
|
||||
}
|
||||
}
|
||||
80
Projects/HubX/HubX.Library/Socket/Object/ObjectManager.cs
Normal file
80
Projects/HubX/HubX.Library/Socket/Object/ObjectManager.cs
Normal file
@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HubX.Library.Socket.Object
|
||||
{
|
||||
public class ObjectManager
|
||||
{
|
||||
public static ObjectManager Instance { get; } = new ObjectManager();
|
||||
|
||||
object _lock = new object();
|
||||
Dictionary<int, Client> _players = new Dictionary<int, Client>();
|
||||
|
||||
// [UNUSED(1)][TYPE(7)][ID(24)]
|
||||
int _counter = 0;
|
||||
|
||||
public T Add<T>() where T : Client, new()
|
||||
{
|
||||
T gameObject = new T();
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
gameObject.Id = GenerateId(gameObject.ObjectType);
|
||||
|
||||
if (gameObject.ObjectType == EnumObjectType.Client)
|
||||
{
|
||||
_players.Add(gameObject.Id, gameObject as Client);
|
||||
}
|
||||
}
|
||||
|
||||
return gameObject;
|
||||
}
|
||||
|
||||
int GenerateId(EnumObjectType type)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return ((int)type << 24) | (_counter++);
|
||||
}
|
||||
}
|
||||
|
||||
public static EnumObjectType GetObjectTypeById(int id)
|
||||
{
|
||||
int type = (id >> 24) & 0x7F;
|
||||
return (EnumObjectType)type;
|
||||
}
|
||||
|
||||
public bool Remove(int objectId)
|
||||
{
|
||||
EnumObjectType objectType = GetObjectTypeById(objectId);
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (objectType == EnumObjectType.Client)
|
||||
return _players.Remove(objectId);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public Client Find(int objectId)
|
||||
{
|
||||
EnumObjectType objectType = GetObjectTypeById(objectId);
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (objectType == EnumObjectType.Client)
|
||||
{
|
||||
Client player = null;
|
||||
if (_players.TryGetValue(objectId, out player))
|
||||
return player;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user