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; namespace AX.NetworkSystem { public static class NetworkManager { private static List sessions = new List(); 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 session, CloseReason reason) { session.Started -= OnConnected; session.Closed -= OnClosed; session.MessageReceived -= OnMessageReceived; var pair = new KeyValuePair(session, reason); NetworkMessageDispatcher.SendClosedMessage(pair); } private static void OnConnected(IAppSession session) { NetworkMessageDispatcher.SendConnectedMessage(session); } private static void OnMessageReceived(IAppSession session, BinaryMessage message) { NetworkMessageDispatcher.SendMessage(message, session); } } }