Ejemplo n.º 1
0
        private TChannel Create(IPEndPoint ipEndPoint, long id)
        {
            TChannel channel = new TChannel(id, ipEndPoint, this);

            this.idChannels.Add(channel.Id, channel);
            return(channel);
        }
Ejemplo n.º 2
0
        private void OnAcceptComplete(object o)
        {
            if (this.acceptor == null)
            {
                return;
            }
            SocketAsyncEventArgs e = (SocketAsyncEventArgs)o;

            if (e.SocketError != SocketError.Success)
            {
                Log.Error($"accept error {e.SocketError}");
                this.AcceptAsync();
                return;
            }
            TChannel channel = new TChannel(e.AcceptSocket, this);

            this.idChannels[channel.Id] = channel;
            channel.Parent = this;

            try
            {
                this.OnAccept(channel);
            }
            catch (Exception exception)
            {
                Log.Error(exception);
            }

            if (this.acceptor == null)
            {
                return;
            }

            this.AcceptAsync();
        }
Ejemplo n.º 3
0
        private void OnAcceptComplete(SocketError socketError, Socket acceptSocket)
        {
            if (this.acceptor == null)
            {
                return;
            }

            if (socketError != SocketError.Success)
            {
                Log.Error($"accept error {socketError}");
                return;
            }

            try
            {
                long     id      = this.CreateAcceptChannelId(0);
                TChannel channel = new TChannel(id, acceptSocket, this);
                this.idChannels.Add(channel.Id, channel);
                long channelId = channel.Id;

                this.OnAccept(channelId, channel.RemoteAddress);
            }
            catch (Exception exception)
            {
                Log.Error(exception);
            }

            // 开始新的accept
            this.AcceptAsync();
        }
Ejemplo n.º 4
0
        private TChannel Get(long id)
        {
            TChannel channel = null;

            this.idChannels.TryGetValue(id, out channel);
            return(channel);
        }
Ejemplo n.º 5
0
        public override AChannel GetChannel(long id)
        {
            TChannel channel = null;

            this.idChannels.TryGetValue(id, out channel);
            return(channel);
        }
Ejemplo n.º 6
0
        public override AChannel ConnectChannel(IPEndPoint ipEndPoint)
        {
            TChannel channel = new TChannel(ipEndPoint, this);

            this.idChannels[channel.Id] = channel;
            channel.Parent = this;
            return(channel);
        }
Ejemplo n.º 7
0
 public override void Update()
 {
     foreach (long channelId in this.NeedStartSend)
     {
         TChannel tChannel = this.Get(channelId);
         tChannel?.Update();
     }
     this.NeedStartSend.Clear();
 }
Ejemplo n.º 8
0
 public TService(ThreadSynchronizationContext threadSynchronizationContext, ServiceType serviceType)
 {
     this.foreachAction = channelId =>
     {
         TChannel tChannel = this.Get(channelId);
         tChannel?.Update();
     };
     this.ServiceType = serviceType;
     this.ThreadSynchronizationContext = threadSynchronizationContext;
 }
Ejemplo n.º 9
0
        public override void Dispose()
        {
            this.acceptor?.Close();
            this.acceptor = null;
            this.innArgs.Dispose();
            ThreadSynchronizationContext = null;

            foreach (long id in this.idChannels.Keys.ToArray())
            {
                TChannel channel = this.idChannels[id];
                channel.Dispose();
            }
            this.idChannels.Clear();
        }
Ejemplo n.º 10
0
 protected override void Send(long channelId, long actorId, MemoryStream stream)
 {
     try
     {
         TChannel aChannel = this.Get(channelId);
         if (aChannel == null)
         {
             this.OnError(channelId, ErrorCore.ERR_SendMessageNotFoundTChannel);
             return;
         }
         aChannel.Send(actorId, stream);
     }
     catch (Exception e)
     {
         Log.Error(e);
     }
 }
Ejemplo n.º 11
0
        public override void Dispose()
        {
            if (this.IsDisposed)
            {
                return;
            }

            base.Dispose();

            foreach (long id in this.idChannels.Keys.ToArray())
            {
                TChannel channel = this.idChannels[id];
                channel.Dispose();
            }
            this.acceptor?.Close();
            this.acceptor = null;
            this.innArgs.Dispose();
        }
Ejemplo n.º 12
0
        public TService(ThreadSynchronizationContext threadSynchronizationContext, IPEndPoint ipEndPoint, ServiceType serviceType)
        {
            this.foreachAction = channelId =>
            {
                TChannel tChannel = this.Get(channelId);
                tChannel?.Update();
            };

            this.ServiceType = serviceType;
            this.ThreadSynchronizationContext = threadSynchronizationContext;

            this.acceptor = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.acceptor.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            this.innArgs.Completed += this.OnComplete;
            this.acceptor.Bind(ipEndPoint);
            this.acceptor.Listen(1000);

            this.ThreadSynchronizationContext.PostNext(this.AcceptAsync);
        }