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

6
namespace Model
7
{
8
	public abstract class NetworkComponent: Component
9
	{
10
		private AService Service;
T
tanghai 已提交
11

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

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

30
		protected void Awake(NetworkProtocol protocol, string host, int port)
T
tanghai 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
		{
			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();
		}
46

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

				AChannel channel = await this.Service.AcceptChannel();

58
				Session session = new Session(channel);
T
tanghai 已提交
59
				channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
60
				this.Add(session);
61
			}
T
tanghai 已提交
62
		}
63

64
		private void Add(Session session)
T
tanghai 已提交
65 66
		{
			this.sessions.Add(session.Id, session);
67
			this.adressSessions.Add(session.RemoteAddress, session);
68 69
		}

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

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

88
		public Session Get(string address)
T
tanghai 已提交
89
		{
90
			Session session;
T
tanghai 已提交
91
			if (this.adressSessions.TryGetValue(address, out session))
92
			{
T
tanghai 已提交
93 94 95 96 97 98 99
				return session;
			}

			string[] ss = address.Split(':');
			int port = int.Parse(ss[1]);
			string host = ss[0];
			AChannel channel = this.Service.ConnectChannel(host, port);
100
			session = new Session(channel);
101
			channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
T
tanghai 已提交
102 103 104 105 106 107 108 109 110 111
			this.Add(session);

			return session;
		}

		public override void Dispose()
		{
			if (this.Id == 0)
			{
				return;
112
			}
T
tanghai 已提交
113 114 115

			base.Dispose();

116
			foreach (Session session in this.sessions.Values.ToArray())
117
			{
118
				session.Dispose();
119 120
			}
			
T
tanghai 已提交
121
			this.Service.Dispose();
122 123 124 125
		}

		public void Update()
		{
T
tanghai 已提交
126
			if (this.Service == null)
127 128 129
			{
				return;
			}
T
tanghai 已提交
130
			this.Service.Update();
131 132 133
		}
	}
}