| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using Common.Base;
- using Common.Network;
- using TNet;
- using UNet;
- namespace Model
- {
- public class NetworkComponent: Component<World>, IUpdate, IStart
- {
- private IService service;
- private void Accept(string host, int port, NetworkProtocol protocol = NetworkProtocol.TCP)
- {
- 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("protocol");
- }
- this.AcceptChannel();
- }
- public void Start()
- {
- this.Accept(World.Instance.Options.Host, World.Instance.Options.Port,
- World.Instance.Options.Protocol);
- }
- public void Update()
- {
- this.service.Update();
- }
- /// <summary>
- /// 接收连接
- /// </summary>
- private async void AcceptChannel()
- {
- while (true)
- {
- AChannel channel = await this.service.GetChannel();
- ProcessChannel(channel);
- }
- }
- /// <summary>
- /// 接收分发封包
- /// </summary>
- /// <param name="channel"></param>
- private static async void ProcessChannel(AChannel channel)
- {
- while (true)
- {
- byte[] message = await channel.RecvAsync();
- Env env = new Env();
- env[EnvKey.Channel] = channel;
- env[EnvKey.Message] = message;
- #pragma warning disable 4014
- World.Instance.GetComponent<EventComponent<EventAttribute>>()
- .RunAsync(EventType.MessageAction, env);
- #pragma warning restore 4014
- }
- }
- }
- }
|