You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
4.8 KiB
160 lines
4.8 KiB
using System; |
|
using System.Collections.Generic; |
|
using System.Text; |
|
using System.Net; |
|
using AX.Network.Common; |
|
using AX.Network.Client; |
|
using AX.Network.Protocols; |
|
using TcpSession = AX.Network.Client.TcpSession<AX.Network.Protocols.BinaryProtocol, AX.Network.Protocols.BinaryMessage>; |
|
|
|
namespace AX.NetworkSystem |
|
{ |
|
public static class NetworkManager |
|
{ |
|
private static List<TcpSession> sessions = new List<TcpSession>(); |
|
private static TcpSession defaultSession; |
|
private static TcpSession chatSession; |
|
private static TcpSession transferSession; |
|
private const string DEFAULT = "DEFAULT"; |
|
private const string TRANSFER = "TRANSFER"; |
|
private const string CHAT = "CHAT"; |
|
|
|
static NetworkManager() |
|
{ |
|
TcpSessionPool.Initialize(1, Encoding.UTF8, 1024 * 1024); |
|
} |
|
|
|
public static TcpSession CreateSession(string ip, int port, string tag) |
|
{ |
|
if (string.IsNullOrEmpty(ip)) |
|
throw new ArgumentException("ip"); |
|
|
|
if (port < UInt16.MinValue || port > UInt16.MaxValue) |
|
throw new ArgumentOutOfRangeException("port"); |
|
|
|
if (string.IsNullOrEmpty(tag)) |
|
throw new ArgumentException("tag"); |
|
|
|
var session = TcpSessionPool.Acquire(); |
|
|
|
var endpoint = new IPEndPoint(IPAddress.Parse(ip), port); |
|
|
|
session.RemoteEndPoint = endpoint; |
|
session.Tag = tag; |
|
|
|
session.Started += OnConnected; |
|
session.Closed += OnClosed; |
|
session.MessageReceived += OnMessageReceived; |
|
|
|
sessions.Add(session); |
|
|
|
if (tag == DEFAULT) |
|
defaultSession = session; |
|
else if (tag == TRANSFER) |
|
transferSession = session; |
|
else if (tag == CHAT) |
|
chatSession = session; |
|
|
|
return session; |
|
} |
|
|
|
public static TcpSession CreateDefaultSession(string ip, int port) |
|
{ |
|
defaultSession = CreateSession(ip, port, DEFAULT); |
|
return defaultSession; |
|
} |
|
|
|
public static TcpSession Default |
|
{ |
|
get { return defaultSession; } |
|
} |
|
|
|
public static TcpSession CreateChatSession(string ip, int port) |
|
{ |
|
chatSession = CreateSession(ip, port, CHAT); |
|
return chatSession; |
|
} |
|
|
|
public static TcpSession Chat |
|
{ |
|
get { return chatSession; } |
|
} |
|
|
|
public static TcpSession CreateTransfer(string ip, int port) |
|
{ |
|
transferSession = CreateSession(ip, port, TRANSFER); |
|
return transferSession; |
|
} |
|
|
|
public static TcpSession Transfer |
|
{ |
|
get { return transferSession; } |
|
} |
|
|
|
public static void DestroySession(TcpSession session) |
|
{ |
|
if ((string)session.Tag == DEFAULT && defaultSession != null) |
|
defaultSession = null; |
|
else if ((string)session.Tag == CHAT && defaultSession != null) |
|
chatSession = null; |
|
else if ((string)session.Tag == TRANSFER && defaultSession != null) |
|
transferSession = null; |
|
|
|
session.Tag = null; |
|
session.Close(); |
|
sessions.Remove(session); |
|
|
|
TcpSessionPool.Release(session); |
|
} |
|
|
|
public static void DestroySession(string tag) |
|
{ |
|
var session = GetSessionByTag(tag); |
|
|
|
if (session != null) |
|
DestroySession(session); |
|
} |
|
|
|
public static void DestroyAllSession() |
|
{ |
|
for (var i = sessions.Count - 1; i >= 0; --i) |
|
{ |
|
var session = sessions[i]; |
|
|
|
session.Tag = null; |
|
session.Close(); |
|
sessions.RemoveAt(i); |
|
|
|
TcpSessionPool.Release(session); |
|
} |
|
|
|
TcpSessionPool.Clear(); |
|
} |
|
|
|
public static TcpSession GetSessionByTag(string tag) |
|
{ |
|
return sessions.Find((session) => { return (string)session.Tag == tag; }); |
|
} |
|
|
|
private static void OnClosed(IAppSession<BinaryProtocol, BinaryMessage> session, CloseReason reason) |
|
{ |
|
session.Started -= OnConnected; |
|
session.Closed -= OnClosed; |
|
session.MessageReceived -= OnMessageReceived; |
|
|
|
var pair = new KeyValuePair<object, CloseReason>(session, reason); |
|
|
|
NetworkMessageDispatcher.SendClosedMessage(pair); |
|
} |
|
|
|
private static void OnConnected(IAppSession<BinaryProtocol, BinaryMessage> session) |
|
{ |
|
NetworkMessageDispatcher.SendConnectedMessage(session); |
|
} |
|
|
|
private static void OnMessageReceived(IAppSession<BinaryProtocol, BinaryMessage> session, BinaryMessage message) |
|
{ |
|
NetworkMessageDispatcher.SendMessage(message, session); |
|
} |
|
} |
|
} |