[성현모] Http, Socket 통신 추가
This commit is contained in:
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