| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- namespace ETModel
- {
- public abstract class NetworkComponent : Component
- {
- public AppType AppType;
-
- private AService Service;
- private readonly Dictionary<long, Session> sessions = new Dictionary<long, Session>();
- public IMessagePacker MessagePacker { get; set; }
- public IMessageDispatcher MessageDispatcher { get; set; }
- public void Awake(NetworkProtocol protocol)
- {
- try
- {
- switch (protocol)
- {
- case NetworkProtocol.KCP:
- this.Service = new KService();
- break;
- case NetworkProtocol.TCP:
- this.Service = new TService();
- break;
- case NetworkProtocol.WebSocket:
- this.Service = new WService();
- break;
- }
- }
- catch (Exception e)
- {
- throw new Exception($"{e}");
- }
- }
- public void Awake(NetworkProtocol protocol, string address)
- {
- try
- {
- IPEndPoint ipEndPoint;
- switch (protocol)
- {
- case NetworkProtocol.KCP:
- ipEndPoint = NetworkHelper.ToIPEndPoint(address);
- this.Service = new KService(ipEndPoint, this.OnAccept);
- break;
- case NetworkProtocol.TCP:
- ipEndPoint = NetworkHelper.ToIPEndPoint(address);
- this.Service = new TService(ipEndPoint, this.OnAccept);
- break;
- case NetworkProtocol.WebSocket:
- string[] prefixs = address.Split(';');
- this.Service = new WService(prefixs, this.OnAccept);
- break;
- }
- }
- catch (Exception e)
- {
- throw new Exception($"NetworkComponent Awake Error {address}", e);
- }
- }
- public int Count
- {
- get { return this.sessions.Count; }
- }
- public void OnAccept(AChannel channel)
- {
- Session session = ComponentFactory.CreateWithParent<Session, AChannel>(this, channel);
- this.sessions.Add(session.Id, session);
- }
- public virtual void Remove(long id)
- {
- Session session;
- if (!this.sessions.TryGetValue(id, out session))
- {
- return;
- }
- this.sessions.Remove(id);
- session.Dispose();
- }
- public Session Get(long id)
- {
- Session session;
- this.sessions.TryGetValue(id, out session);
- return session;
- }
- /// <summary>
- /// 创建一个新Session
- /// </summary>
- public Session Create(IPEndPoint ipEndPoint)
- {
- AChannel channel = this.Service.ConnectChannel(ipEndPoint);
- Session session = ComponentFactory.CreateWithParent<Session, AChannel>(this, channel);
- this.sessions.Add(session.Id, session);
- return session;
- }
-
- /// <summary>
- /// 创建一个新Session
- /// </summary>
- public Session Create(string address)
- {
- AChannel channel = this.Service.ConnectChannel(address);
- Session session = ComponentFactory.CreateWithParent<Session, AChannel>(this, channel);
- this.sessions.Add(session.Id, session);
- return session;
- }
- public void Update()
- {
- if (this.Service == null)
- {
- return;
- }
- this.Service.Update();
- }
- public override void Dispose()
- {
- if (this.IsDisposed)
- {
- return;
- }
- base.Dispose();
- foreach (Session session in this.sessions.Values.ToArray())
- {
- session.Dispose();
- }
- this.Service.Dispose();
- }
- }
- }
|