Esempio n. 1
0
        private void Item_Completed(object sender, SocketAsyncEventArgs e)
        {
            IChannelHandler rwhandler = null;
            IChannelHandler handler   = null;

            if (!handers.TryGetValue(HanderType.Decoder, out handler))
            {
                //如果没有解析类,就直接获取读取管道
                handers.TryGetValue(HanderType.ReadWrite, out rwhandler);
            }
            if (socketArg == null)
            {
                socketArg        = new SocketArgEvent();
                socketArg.chanel = socketChannel;
            }
            socketArg.data       = e.Buffer;
            socketArg.localIP    = localIP;
            socketArg.localPort  = localPort;
            socketArg.remoteIP   = remoteIP;
            socketArg.remotePort = remotePort;
            if (handler != null)
            {
                handler.ChannelRead(socketArg, e.Buffer);
                handler.ChannelReadComplete(socketArg);
            }
            else if (rwhandler != null)
            {
                rwhandler.ChannelRead(socketArg, e.Buffer);
                rwhandler.ChannelReadComplete(socketArg);
            }
        }
Esempio n. 2
0
        private void Item_Completed(object sender, SocketAsyncEventArgs e)
        {
            acceptEvent.Set();
            IChannelHandler handler = null;
            SocketArgEvent  arg     = new SocketArgEvent();
            IPEndPoint      point   = e.AcceptSocket.RemoteEndPoint as IPEndPoint;

            arg.remoteIP   = point.Address.ToString();
            arg.remotePort = point.Port;
            point          = e.AcceptSocket.LocalEndPoint as IPEndPoint;
            arg.localIP    = point.Address.ToString();
            arg.localPort  = point.Port;
            arg.chanel     = socketChannel;
            TCPSocketChannel channel = new TCPSocketChannel();

            channel.HandlerTypes   = this.handers;
            channel.Socket         = e.AcceptSocket;
            channel.SocketArgEvent = arg;
            arg.chanel             = channel;
            if (handers.TryGetValue(HanderType.ReadWrite, out handler))
            {
                handler.ChannelActive(arg);
            }
            lock (lock_obj)
            {
                channels.Add(arg);
            }
            Interlocked.Increment(ref m_numConnectedSockets);
            FreeArgEvent(e);
            WorkThread();
        }
 public override void ChannelRead(SocketArgEvent argEvent, object message)
 {
     if (message.GetType() == typeof(byte[]))
     {
         var           intput = (T)message;
         List <object> output = new List <object>(10);
         this.Decode(argEvent, intput, output);
         int size = output.Count;
         for (int i = 0; i < size; i++)
         {
             argEvent.chanel.FireChannelRead(output[i]);
         }
     }
 }
Esempio n. 4
0
 public override async Task WriteAsync(SocketArgEvent argEvent, object message)
 {
     // return null;
     if (message == null)
     {
         return;
     }
     if (message.GetType() == typeof(byte[]))
     {
         argEvent.chanel.FireChannelWrite(message);
     }
     else
     {
         List <byte> list  = new List <byte>();
         var         input = (T)message;
         this.Encode(argEvent, input, list);
         if (list.Count > 0)
         {
             argEvent.chanel.FireChannelWrite(list.ToArray());
         }
     }
 }
Esempio n. 5
0
 public virtual async Task CloseAsync(SocketArgEvent argEvent)
 {
     await argEvent.chanel.CloseAsync();
 }
Esempio n. 6
0
 public virtual void ChannelWritabilityChanged(SocketArgEvent argEvent)
 {
     argEvent.chanel.FireChannelWritabilityChanged();
 }
Esempio n. 7
0
 protected abstract void Encode(SocketArgEvent argEvent, T message, object output);
Esempio n. 8
0
 public override void ChannelRead(SocketArgEvent argEvent, object message)
 {
     //base.ChannelRead(argEvent, message);
     //接收的数据
     byte[] data = (byte[])message;
 }
Esempio n. 9
0
 public virtual void HandlerRemoved(SocketArgEvent argEvent)
 {
 }
Esempio n. 10
0
 public virtual void Flush(SocketArgEvent argEvent)
 {
 }
Esempio n. 11
0
 public virtual async Task DisconnectAsync(SocketArgEvent argEvent)
 {
     argEvent.chanel.Close();
 }
Esempio n. 12
0
        /// <summary>
        /// 监测数据
        /// </summary>
        private void DoWork()
        {
            while (!isStop)
            {
                if (socketChannel.IsClose)
                {
                    //说明监听被关闭;
                    this.Close();
                }
                SocketArgEvent item = null;
                if (!channels.TryTake(out item, 1000))
                {
                    continue;
                }

                TCPSocketChannel channel = item.chanel as TCPSocketChannel;
                if (channel != null)
                {
                    IChannelHandler rwhandler = null;
                    IChannelHandler handler   = null;
                    if (!handers.TryGetValue(HanderType.Decoder, out handler))
                    {
                        //如果没有解析类,就直接获取读取管道
                        handers.TryGetValue(HanderType.ReadWrite, out rwhandler);
                    }

                    if (channel.Socket.Available > 0)
                    {
                        //读取数据
                        while (channel.Socket.Connected && channel.Socket.Available > 0)
                        {
                            int num = channel.Socket.Available;
                            List <ByteBuffer> buffers = BufferManager.GetInstance().GetBufferSize(num);
                            for (int i = 0; i < buffers.Count; i++)
                            {
                                int r = channel.Socket.Receive(buffers[i].buffer);
                                buffers[i].Reset(0, r);
                            }
                            if (channel.AddBuffer(buffers, num) > 0)
                            {
                                if (handler != null)
                                {
                                    foreach (byte[] result in channel.RecBuffer)
                                    {
                                        handler.ChannelRead(item, result);
                                    }
                                }
                                else if (rwhandler != null)
                                {
                                    foreach (byte[] result in channel.RecBuffer)
                                    {
                                        rwhandler.ChannelRead(item, result);
                                    }
                                }
                            }
                        }
                    }
                    if (handler != null)
                    {
                        //当前一次读取已经完成
                        handler.ChannelReadComplete(item);
                    }
                    if (channel.Socket.Connected)
                    {
                        //还是连接状态就放回等待监测数据
                        channels.Add(item);
                    }
                    else
                    {
                        if (handler != null)
                        {
                            //
                            handler.ChannelInactive(item);
                            handler.CloseAsync(item);
                        }
                        Interlocked.Decrement(ref m_numConnectedSockets);
                    }
                }
            }
        }
Esempio n. 13
0
        /// <summary>
        /// 监测数据
        /// </summary>
        private void DoWork()
        {
            SocketArgEvent item       = new SocketArgEvent();
            IPEndPoint     iPEndPoint = socketChannel.Socket.LocalEndPoint as IPEndPoint;

            item.localIP   = iPEndPoint.Address.ToString();
            item.localPort = iPEndPoint.Port;
            localIP        = item.localIP;
            localPort      = item.localPort;
            item.chanel    = socketChannel;
            if (socketChannel.Socket.RemoteEndPoint != null)
            {
                iPEndPoint      = socketChannel.Socket.RemoteEndPoint as IPEndPoint;
                item.remoteIP   = iPEndPoint.Address.ToString();
                item.remotePort = iPEndPoint.Port;
            }
            while (!isStop)
            {
                if (socketChannel != null)
                {
                    if (socketChannel.IsClose)
                    {
                        //已经关闭socket,最后接收一次数据
                        Close();
                    }
                    IChannelHandler rwhandler = null;
                    IChannelHandler handler   = null;
                    if (!handers.TryGetValue(HanderType.Decoder, out handler))
                    {
                        //如果没有解析类,就直接获取读取管道
                        handers.TryGetValue(HanderType.ReadWrite, out rwhandler);
                    }

                    if (socketChannel.Socket.Available > 0)
                    {
                        //读取数据
                        if (isTCP)
                        {
                            TCPSocketChannel channel = socketChannel as TCPSocketChannel;
                            while (channel.Socket.Connected && channel.Socket.Available > 0)
                            {
                                int num = channel.Socket.Available;
                                List <ByteBuffer> buffers = BufferManager.GetInstance().GetBufferSize(num);
                                for (int i = 0; i < buffers.Count; i++)
                                {
                                    int r = channel.Socket.Receive(buffers[i].buffer);
                                    buffers[i].Reset(0, r);
                                }
                                if (channel.AddBuffer(buffers, num) > 0)
                                {
                                    if (handler != null)
                                    {
                                        foreach (byte[] result in channel.RecBuffer)
                                        {
                                            handler.ChannelRead(item, result);
                                        }
                                    }
                                    else if (rwhandler != null)
                                    {
                                        foreach (byte[] result in channel.RecBuffer)
                                        {
                                            rwhandler.ChannelRead(item, result);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        UDPSocketChannel channel  = socketChannel as UDPSocketChannel;
                        EndPoint         endPoint = null;
                        while (!channel.IsClose && channel.Socket.Available > 0)
                        {
                            int num = channel.Socket.Available;
                            List <ByteBuffer> buffers = BufferManager.GetInstance().GetBufferSize(num);
                            for (int i = 0; i < buffers.Count; i++)
                            {
                                int r = channel.Socket.ReceiveFrom(buffers[i].buffer, ref endPoint);
                                buffers[i].Reset(0, r);
                            }
                            if (channel.AddBuffer(buffers, num) > 0)
                            {
                                SocketArgEvent udpItem = new SocketArgEvent();
                                //
                                udpItem.localIP   = localIP;
                                udpItem.localPort = localPort;
                                if (endPoint != null)
                                {
                                    iPEndPoint      = endPoint as IPEndPoint;
                                    item.remoteIP   = iPEndPoint.Address.ToString();
                                    item.remotePort = iPEndPoint.Port;
                                }
                                if (handler != null)
                                {
                                    foreach (byte[] result in channel.RecBuffer)
                                    {
                                        handler.ChannelRead(udpItem, result);
                                    }
                                }
                                else if (rwhandler != null)
                                {
                                    foreach (byte[] result in channel.RecBuffer)
                                    {
                                        rwhandler.ChannelRead(udpItem, result);
                                    }
                                }
                                handler.ChannelReadComplete(udpItem);
                            }
                        }
                    }

                    if (isTCP)
                    {
                        if (handler != null)
                        {
                            //当前一次读取已经完成
                            handler.ChannelReadComplete(item);
                        }
                        if (item.chanel.Socket.Connected)
                        {
                            //还是连接状态就放回等待监测数据
                            Thread.Sleep(100);
                        }
                        else
                        {
                            if (handler != null)
                            {
                                //
                                handler.ChannelInactive(item);
                                handler.CloseAsync(item);
                            }
                        }
                    }
                    else
                    {
                        UDPSocketChannel channel = item.chanel as UDPSocketChannel;
                        if (!channel.IsClose)
                        {
                            //还是连接状态就放回等待监测数据
                            Thread.Sleep(100);
                        }
                        else
                        {
                            if (handler != null)
                            {
                                //
                                handler.ChannelInactive(item);
                                handler.CloseAsync(item);
                            }
                        }
                    }
                }
            }
        }
 protected internal abstract void Decode(SocketArgEvent context, T message, List <object> output);
Esempio n. 15
0
 protected internal override void Decode(SocketArgEvent context, byte[] message, List <object> output)
 {
     output.Add(message);
 }
Esempio n. 16
0
 public virtual async Task ConnectAsync(SocketArgEvent argEvent, EndPoint remoteAddress, EndPoint localAddress)
 {
     await argEvent.chanel.ConnectAsync(remoteAddress, localAddress);
 }
Esempio n. 17
0
 public virtual async Task DeregisterAsync(SocketArgEvent argEvent)
 {
 }
Esempio n. 18
0
 public virtual void UserEventTriggered(SocketArgEvent argEvent, object evt)
 {
 }
Esempio n. 19
0
 public virtual void ExceptionCaught(SocketArgEvent argEvent, Exception exception)
 {
     argEvent.chanel.FireExceptionCaught(exception);
 }
Esempio n. 20
0
 public virtual async Task WriteAsync(SocketArgEvent argEvent, object message)
 {
     await argEvent.chanel.WriteAndFlushAsync(message);
 }
Esempio n. 21
0
 public virtual void HandlerAdded(SocketArgEvent argEvent)
 {
 }
Esempio n. 22
0
 public virtual async Task BindAsync(SocketArgEvent argEvent, EndPoint localAddress)
 {
     await argEvent.chanel.BindAsync(localAddress);
 }
Esempio n. 23
0
 public virtual void Read(SocketArgEvent argEvent)
 {
     argEvent.chanel.FireChannelReadComplete();
 }
Esempio n. 24
0
 public virtual void ChannelInactive(SocketArgEvent argEvent)
 {
     argEvent.chanel.FireChannelInActive();
 }
Esempio n. 25
0
        protected override void Encode(SocketArgEvent argEvent, object message, object output)
        {
            List <byte[]> list = output as List <byte[]>;

            list.Add((byte[])message);
        }
Esempio n. 26
0
 public virtual void ChannelRead(SocketArgEvent argEvent, object message)
 {
     argEvent.chanel.FireChannelRead(message);
 }