1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System;
- using System.IO;
- using System.Net;
- namespace KYFramework.Network
- {
- public enum ChannelType
- {
- Connect,
- Accept,
- }
- public abstract class AChannel : ComponentWithId
- {
- public ChannelType ChannelType { get; }
- public AService Service { get; }
- public abstract MemoryStream Stream { get; }
- public int Error { get; set; }
- public IPEndPoint RemoteAddress { get; protected set; }
- private Action<AChannel, int> errorCallback;
- private Action<MemoryStream> readCallback;
- public event Action<AChannel, int> ErrorCallback
- {
- add
- {
- this.errorCallback += value;
- }
- remove
- {
- this.errorCallback -= value;
- }
- }
- public event Action<MemoryStream> ReadCallback
- {
- add
- {
- this.readCallback += value;
- }
- remove
- {
- this.readCallback -= value;
- }
- }
- protected void OnRead(MemoryStream memoryStream)
- {
- this.readCallback.Invoke(memoryStream);
- }
- protected void OnError(int e)
- {
- this.Error = e;
- this.errorCallback?.Invoke(this, e);
- }
- protected AChannel(AService service, ChannelType channelType)
- {
- this.Id = IdGenerater.GenerateId();
- this.ChannelType = channelType;
- this.Service = service;
- Log.Info("AChannel init...");
- }
- public abstract void Start();
- public abstract void Send(MemoryStream stream);
- public override void Dispose()
- {
- if (this.IsDisposed)
- {
- return;
- }
- base.Dispose();
- this.Service.Remove(this.Id);
- }
- }
- }
|