NetworkComponent.cs 2.7 KB
Newer Older
1
using System;
T
tanghai 已提交
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Threading.Tasks;
5

6
namespace Model
7
{
8
	public abstract class NetworkComponent : Component
9
	{
10
		private AService Service;
11

12
		private readonly Dictionary<long, Session> sessions = new Dictionary<long, Session>();
T
tanghai 已提交
13

14
		public IMessagePacker MessagePacker { get; set; }
15

16
		public IMessageDispatcher MessageDispatcher { get; set; }
17 18

		public void Awake(NetworkProtocol protocol)
19
		{
T
tanghai 已提交
20
			switch (protocol)
21
			{
T
tanghai 已提交
22 23 24 25 26 27 28 29
				case NetworkProtocol.TCP:
					this.Service = new TService();
					break;
				case NetworkProtocol.UDP:
					this.Service = new UService();
					break;
				default:
					throw new ArgumentOutOfRangeException();
30
			}
T
tanghai 已提交
31
		}
32

33
		public void Awake(NetworkProtocol protocol, string host, int port)
T
tanghai 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
		{
			switch (protocol)
			{
				case NetworkProtocol.TCP:
					this.Service = new TService(host, port);
					break;
				case NetworkProtocol.UDP:
					this.Service = new UService(host, port);
					break;
				default:
					throw new ArgumentOutOfRangeException();
			}

			this.StartAccept();
		}
49

T
tanghai 已提交
50 51 52
		private async void StartAccept()
		{
			while (true)
53
			{
T
tanghai 已提交
54 55 56 57 58
				if (this.Id == 0)
				{
					return;
				}

59
				await this.Accept();
60
			}
T
tanghai 已提交
61
		}
62

63
		public virtual async Task<Session> Accept()
T
tanghai 已提交
64
		{
65
			AChannel channel = await this.Service.AcceptChannel();
66
			Session session = new Session(this, channel);
67 68 69
			channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
			this.sessions.Add(session.Id, session);
			return session;
70 71
		}

72
		public virtual void Remove(long id)
73
		{
74
			Session session;
T
tanghai 已提交
75
			if (!this.sessions.TryGetValue(id, out session))
76 77 78
			{
				return;
			}
T
tanghai 已提交
79
			this.sessions.Remove(id);
80
			session.Dispose();
81 82
		}

83
		public Session Get(long id)
84
		{
85
			Session session;
T
tanghai 已提交
86 87 88 89
			this.sessions.TryGetValue(id, out session);
			return session;
		}

T
tanghai 已提交
90
		/// <summary>
T
tanghai 已提交
91
		/// 创建一个新Session
T
tanghai 已提交
92
		/// </summary>
93
		public virtual Session Create(string address)
T
tanghai 已提交
94
		{
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
			try
			{
				string[] ss = address.Split(':');
				int port = int.Parse(ss[1]);
				string host = ss[0];
				AChannel channel = this.Service.ConnectChannel(host, port);
				Session session = new Session(this, channel);
				channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
				this.sessions.Add(session.Id, session);
				return session;
			}
			catch (Exception e)
			{
				Log.Error(e.ToString());
				return null;
			}
T
tanghai 已提交
111 112
		}

113
		public void Update()
114 115 116 117 118 119 120 121
		{
			if (this.Service == null)
			{
				return;
			}
			this.Service.Update();
		}

T
tanghai 已提交
122 123 124 125 126
		public override void Dispose()
		{
			if (this.Id == 0)
			{
				return;
127
			}
T
tanghai 已提交
128 129 130

			base.Dispose();

131
			foreach (Session session in this.sessions.Values.ToArray())
132
			{
133
				session.Dispose();
134
			}
T
tanghai 已提交
135

T
tanghai 已提交
136
			this.Service.Dispose();
137 138 139
		}
	}
}