[성현모] 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
33
Projects/HubX/HubX.Library/Socket/Packet/EnumMessageId.cs
Normal file
33
Projects/HubX/HubX.Library/Socket/Packet/EnumMessageId.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HubX.Library.Socket.Packet
|
||||
{
|
||||
public enum EnumMessageId
|
||||
{
|
||||
//C2S : Client to Server
|
||||
//S2C : Server to Client
|
||||
|
||||
//unique key CRUD
|
||||
C2S_INSERT_UniqueKey = 10,
|
||||
S2C_INSERT_UniqueKey = 11,
|
||||
C2S_SELECT_UniqueKey = 12,
|
||||
S2C_SELECT_UniqueKey = 13,
|
||||
C2S_UPDATE_UniqueKey = 14,
|
||||
S2C_UPDATE_UniqueKey = 15,
|
||||
C2S_DELETE_UniqueKey = 16,
|
||||
S2C_DELETE_UniqueKey = 17,
|
||||
}
|
||||
|
||||
public enum EnumMessageResult
|
||||
{
|
||||
None = 0,
|
||||
|
||||
Success = 10,
|
||||
Failed = 10,
|
||||
Error = 11,
|
||||
}
|
||||
}
|
||||
43
Projects/HubX/HubX.Library/Socket/Packet/PacketHandler.cs
Normal file
43
Projects/HubX/HubX.Library/Socket/Packet/PacketHandler.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using HubX.Library.Socket.Object;
|
||||
using HubX.Library.Socket.Session;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SystemX.Core.Communication;
|
||||
|
||||
namespace HubX.Library.Socket.Packet
|
||||
{
|
||||
|
||||
public class PacketHandler
|
||||
{
|
||||
public static void C2S_INSERT_UniqueKeyHandler(PacketSession session, IMessage packet)
|
||||
{
|
||||
C2S_INSERT_UniqueKey movePacket = packet as C2S_INSERT_UniqueKey;
|
||||
ClientSession clientSession = session as ClientSession;
|
||||
|
||||
Client client = clientSession.Client;
|
||||
if (client == null)
|
||||
return;
|
||||
}
|
||||
|
||||
public static void C_SkillHandler(PacketSession session, IMessage packet)
|
||||
{
|
||||
//C_Skill skillPacket = packet as C_Skill;
|
||||
//ClientSession clientSession = session as ClientSession;
|
||||
|
||||
//Player player = clientSession.MyPlayer;
|
||||
//if (player == null)
|
||||
// return;
|
||||
|
||||
//GameRoom room = player.Room;
|
||||
//if (room == null)
|
||||
// return;
|
||||
|
||||
//room.Push(room.HandleSkill, player, skillPacket);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
46
Projects/HubX/HubX.Library/Socket/Packet/Protocol.cs
Normal file
46
Projects/HubX/HubX.Library/Socket/Packet/Protocol.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HubX.Library.Socket.Packet
|
||||
{
|
||||
public interface IMessage
|
||||
{
|
||||
}
|
||||
|
||||
public class C2S : IMessage
|
||||
{
|
||||
public EnumMessageId MessageId { get; set; }
|
||||
}
|
||||
|
||||
public class S2C : IMessage
|
||||
{
|
||||
public EnumMessageId MessageId { get; set; }
|
||||
|
||||
public EnumMessageResult Result { get; set; }
|
||||
}
|
||||
|
||||
public sealed class C2S_INSERT_UniqueKey : C2S
|
||||
{
|
||||
public string Identity { get; set; }
|
||||
|
||||
public string Data1 { get; set; }
|
||||
public string Data2 { get; set; }
|
||||
public string Data3 { get; set; }
|
||||
public string Data4 { get; set; }
|
||||
public string Data5 { get; set; }
|
||||
}
|
||||
|
||||
public sealed class S2C_INSERT_UniqueKey : S2C
|
||||
{
|
||||
public string Identity { get; set; }
|
||||
|
||||
public string Data1 { get; set; }
|
||||
public string Data2 { get; set; }
|
||||
public string Data3 { get; set; }
|
||||
public string Data4 { get; set; }
|
||||
public string Data5 { get; set; }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SystemX.Core.Communication;
|
||||
|
||||
namespace HubX.Library.Socket.Packet
|
||||
{
|
||||
public class PacketManager
|
||||
{
|
||||
#region Singleton
|
||||
static PacketManager _instance = new PacketManager();
|
||||
public static PacketManager Instance { get { return _instance; } }
|
||||
#endregion
|
||||
|
||||
PacketManager()
|
||||
{
|
||||
Register();
|
||||
}
|
||||
|
||||
Dictionary<ushort, Action<PacketSession, ArraySegment<byte>, ushort>> _onRecv = new Dictionary<ushort, Action<PacketSession, ArraySegment<byte>, ushort>>();
|
||||
Dictionary<ushort, Action<PacketSession, IMessage>> _handler = new Dictionary<ushort, Action<PacketSession, IMessage>>();
|
||||
|
||||
public Action<PacketSession, IMessage, ushort> CustomHandler { get; set; }
|
||||
|
||||
public void Register()
|
||||
{
|
||||
_onRecv.Add((ushort)EnumMessageId.C2S_INSERT_UniqueKey, MakePacket<C2S_INSERT_UniqueKey>);
|
||||
_handler.Add((ushort)EnumMessageId.C2S_INSERT_UniqueKey, PacketHandler.C2S_INSERT_UniqueKeyHandler);
|
||||
}
|
||||
|
||||
public void OnRecvPacket(PacketSession session, ArraySegment<byte> buffer)
|
||||
{
|
||||
ushort count = 0;
|
||||
|
||||
ushort size = BitConverter.ToUInt16(buffer.Array, buffer.Offset);
|
||||
count += 2;
|
||||
ushort id = BitConverter.ToUInt16(buffer.Array, buffer.Offset + count);
|
||||
count += 2;
|
||||
|
||||
Action<PacketSession, ArraySegment<byte>, ushort> action = null;
|
||||
if (_onRecv.TryGetValue(id, out action))
|
||||
action.Invoke(session, buffer, id);
|
||||
}
|
||||
|
||||
void MakePacket<T>(PacketSession session, ArraySegment<byte> buffer, ushort id) where T : IMessage, new()
|
||||
{
|
||||
T pkt = new T();
|
||||
//pkt.MergeFrom(buffer.Array, buffer.Offset + 4, buffer.Count - 4);
|
||||
|
||||
if (CustomHandler != null)
|
||||
{
|
||||
CustomHandler.Invoke(session, pkt, id);
|
||||
}
|
||||
else
|
||||
{
|
||||
Action<PacketSession, IMessage> action = null;
|
||||
if (_handler.TryGetValue(id, out action))
|
||||
action.Invoke(session, pkt);
|
||||
}
|
||||
}
|
||||
|
||||
public Action<PacketSession, IMessage> GetPacketHandler(ushort id)
|
||||
{
|
||||
Action<PacketSession, IMessage> action = null;
|
||||
if (_handler.TryGetValue(id, out action))
|
||||
return action;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
56
Projects/HubX/HubX.Library/Socket/Session/ClientSession.cs
Normal file
56
Projects/HubX/HubX.Library/Socket/Session/ClientSession.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using HubX.Library.Socket.Object;
|
||||
using HubX.Library.Socket.Packet;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SystemX.Core.Communication;
|
||||
|
||||
namespace HubX.Library.Socket.Session
|
||||
{
|
||||
public class ClientSession : PacketSession
|
||||
{
|
||||
public Client Client { get; set; }
|
||||
public int SessionId { get; set; }
|
||||
|
||||
public void Send(IMessage packet)
|
||||
{
|
||||
//string msgName = packet.Descriptor.Name.Replace("_", string.Empty);
|
||||
//EnumMessageId msgId = (EnumMessageId)Enum.Parse(typeof(EnumMessageId), msgName);
|
||||
//ushort size = (ushort)packet.CalculateSize();
|
||||
//byte[] sendBuffer = new byte[size + 4];
|
||||
//Array.Copy(BitConverter.GetBytes((ushort)(size + 4)), 0, sendBuffer, 0, sizeof(ushort));
|
||||
//Array.Copy(BitConverter.GetBytes((ushort)msgId), 0, sendBuffer, 2, sizeof(ushort));
|
||||
//Array.Copy(packet.ToByteArray(), 0, sendBuffer, 4, size);
|
||||
//Send(new ArraySegment<byte>(sendBuffer));
|
||||
}
|
||||
|
||||
public override void OnConnected(EndPoint endPoint)
|
||||
{
|
||||
Log4net.WriteLine($"OnConnected : {endPoint}", LogType.SOCKET);
|
||||
Client = ObjectManager.Instance.Add<Client>();
|
||||
{
|
||||
Client.Session = this;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnRecvPacket(ArraySegment<byte> buffer)
|
||||
{
|
||||
Log4net.WriteLine($"OnRecvPacket : {Encoding.UTF8.GetString(buffer)}", LogType.SOCKET);
|
||||
PacketManager.Instance.OnRecvPacket(this, buffer);
|
||||
}
|
||||
|
||||
public override void OnDisconnected(EndPoint endPoint)
|
||||
{
|
||||
Log4net.WriteLine($"OnDisconnected : {endPoint}", LogType.SOCKET);
|
||||
}
|
||||
|
||||
public override void OnSend(int numOfBytes)
|
||||
{
|
||||
//Console.WriteLine($"Transferred bytes: {numOfBytes}");
|
||||
}
|
||||
}
|
||||
}
|
||||
54
Projects/HubX/HubX.Library/Socket/Session/SessionManager.cs
Normal file
54
Projects/HubX/HubX.Library/Socket/Session/SessionManager.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using HubX.Library.Socket.Packet;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SystemX.Core.Communication;
|
||||
|
||||
namespace HubX.Library.Socket.Session
|
||||
{
|
||||
public class SessionManager
|
||||
{
|
||||
static SessionManager _session = new SessionManager();
|
||||
public static SessionManager Instance { get { return _session; } }
|
||||
|
||||
int _sessionId = 0;
|
||||
Dictionary<int, ClientSession> _sessions = new Dictionary<int, ClientSession>();
|
||||
object _lock = new object();
|
||||
|
||||
public ClientSession Generate()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
int sessionId = ++_sessionId;
|
||||
|
||||
ClientSession session = new ClientSession();
|
||||
session.SessionId = sessionId;
|
||||
_sessions.Add(sessionId, session);
|
||||
|
||||
Log4net.WriteLine($"Connected : {sessionId}", LogType.SOCKET);
|
||||
|
||||
return session;
|
||||
}
|
||||
}
|
||||
|
||||
public ClientSession Find(int id)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
ClientSession session = null;
|
||||
_sessions.TryGetValue(id, out session);
|
||||
return session;
|
||||
}
|
||||
}
|
||||
|
||||
public void Remove(ClientSession session)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_sessions.Remove(session.SessionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user