Ejemplo n.º 1
0
        public void OnTransportWrite(IUdpSession session, ref WrappedWriter writer, WrappedArgs args)
        {
            //Console.WriteLine($"{nameof(UdpFilter_2)}.{nameof(OnTransportWrite)}");
            var buffer = args.Value as byte[];

            writer.Write(buffer);
        }
Ejemplo n.º 2
0
        public override void OnUdpMessageInput(IUdpSession session, ref WrappedReader reader, WrappedArgs args)
        {
            //消息类型
            var messageType = reader.ReadInt32();
            var buffer      = reader.ReadBuffer((int)reader.Length);
            //频道号
            var convSpan = buffer.Slice(buffer.Start, 4).First.Span;
            var conv     = base.OrderOperators.ReadUInt32(convSpan);

            if (!this.sessions.ContainsKey(conv))
            {
                return;
            }

            var kcpSession = this.sessions[conv];

            if (messageType == k_message)
            {
                base.SendKcpMessageTo(kcpSession, ref buffer);
            }
            else if (messageType == c_message)
            {
                base.SendUdpMessageTo(kcpSession, ref buffer);
            }
        }
Ejemplo n.º 3
0
        public void OnTransportRead(IUdpSession session, ref WrappedReader reader, WrappedArgs args)
        {
            long length = reader.Length;
            var  buffer = reader.ReadBuffer((int)length);
            //string str= System.Text.Encoding.UTF8.GetString(buffer.FirstSpan);

            //Console.WriteLine($"{nameof(UdpFilter_2)}.{nameof(OnTransportRead)}>>LoaclAddress:{session.LocalAddress}    remoteAddress:{session.RcvAddress}>>" + str);
        }
Ejemplo n.º 4
0
        public void OnTransportRead(IUdpSession session, ref WrappedReader reader, WrappedArgs args)
        {
            this.buffers.Clear();
            var buffer = reader.ReadBuffer((int)reader.Length);

            this.buffers.Add(buffer.First.GetBinaryArray());
            args.Value = this.buffers;
        }
Ejemplo n.º 5
0
 public override void OnUdpException(IUdpSession session, Exception ex)
 {
     Console.WriteLine("error>>>>>>>>>>>>>>>>" + ex.ToString());
     foreach (var item in this.sessions)
     {
         base.SendExceptionTo(item.Value, ex);
     }
 }
Ejemplo n.º 6
0
        public void OnTransportActive(IUdpSession session)
        {
            Console.WriteLine($"{nameof(UdpFilter_2)}.{nameof(OnTransportActive)}" + session.LocalAddress);

            if (session.LocalAddress.ToString() == Program.c_p.ToString())
            {
                M.session = session;
            }
        }
Ejemplo n.º 7
0
        public void OnTransportWrite(IUdpSession session, ref WrappedWriter writer, WrappedArgs args)
        {
            if (this.isInvalid || this.isStop)
            {
                return;
            }

            this.controller.OnUdpMessageOutput(session, ref writer, args);
        }
Ejemplo n.º 8
0
        public void OnTransportRead(IUdpSession session, ref WrappedReader reader, WrappedArgs args)
        {
            if (this.isInvalid || this.isStop)
            {
                return;
            }

            this.controller.OnUdpMessageInput(session, ref reader, args);
        }
Ejemplo n.º 9
0
 public void OnTransportWrite(IUdpSession session, ref WrappedWriter writer, WrappedArgs args)
 {
     byte[] bytes = args.Value as byte[];
     if (bytes != null)
     {
         writer.Write(bytes.Length);
         writer.Write(bytes);
     }
 }
Ejemplo n.º 10
0
        protected override bool OnHandleData(string request, ref int pos)
        {
            var done = _dteDataSender.SendData(_session, request, ref pos);

            if (done)
            {
                _session = null;
            }
            return(done);
        }
Ejemplo n.º 11
0
        static void ReceiveCompleted(object sender, ReceiveCommond e)
        {
            IUdpSession session = sender as IUdpSession;

            Console.WriteLine(Encoding.UTF8.GetString(e.Data));
            session.SendAsync(new SendCommond()
            {
                CommondId = e.CommondId, Buffer = e.Data
            });
        }
Ejemplo n.º 12
0
        protected override bool Handle(CommandInfo commandInfo)
        {
            int    start = commandInfo.ArgsPos;
            int    contextId;
            string command = commandInfo.CommandLine;

            if (!ParseItem(command, ref start, out contextId))
            {
                return(false);
            }

            int mode;

            if (!ParseItem(command, ref start, out mode))
            {
                return(false);
            }

            int sourcePort;

            ParseItem(command, ref start, out sourcePort, 0);

            int dataMode;

            ParseItem(command, ref start, out dataMode, 0);

            string remoteAddress;

            if (ParseItem(command, ref start, out remoteAddress, string.Empty))
            {
                remoteAddress = Unescape(remoteAddress);
            }

            int remotePort;

            ParseItem(command, ref start, out remotePort, 0);

            int addressFamily; //0: ipv4; 1: ipv6

            ParseItem(command, ref start, out addressFamily, 0);

            IUdpSession session = CreateSession(DataProtocol.UDP) as IUdpSession;

            if (session == null)
            {
                return(false);
            }

            var id = session.Setup(contextId, (UdpMode)mode, remoteAddress, remotePort, sourcePort, dataMode == 1,
                                   (IpAddrFamily)addressFamily);

            SendResponse("+KUDPCFG: " + id);

            return(true);
        }
Ejemplo n.º 13
0
        public override void OnUdpMessageOutput(IUdpSession session, ref WrappedWriter writer, WrappedArgs args)
        {
            IWrappedMemory memory = args.Value as IWrappedMemory;

            if (memory.Flag == MemoryFlag.Kcp)
            {
                //kcp协议
                writer.Write(k_message);
            }
            else if (memory.Flag == MemoryFlag.Udp)
            {
                //普通udp协议
                writer.Write(c_message);
            }

            writer.Write(memory.GetReadableMemory());
        }
Ejemplo n.º 14
0
        protected override bool Handle(CommandInfo commandInfo)
        {
            string command   = commandInfo.CommandLine;
            int    start     = commandInfo.ArgsPos;
            var    sessionId = 0;

            if (!ParseItem(command, ref start, out sessionId))
            {
                return(false);
            }

            string remoteAddress;

            if (ParseItem(command, ref start, out remoteAddress, string.Empty))
            {
                remoteAddress = Unescape(remoteAddress);
            }

            int remotePort;

            ParseItem(command, ref start, out remotePort, 0);

            var numData = 0;

            if (!ParseItem(command, ref start, out numData))
            {
                return(false);
            }

            _session = GetUdpSession(sessionId);
            if (_session == null)
            {
                return(false);
            }

            SendResponse("Connect");

            _session.BeginSend(numData, remoteAddress, remotePort);
            return(true);
        }
Ejemplo n.º 15
0
        public override async Task StopAsync()
        {
            if (this.isStop)
            {
                return;
            }

            this.isStop        = true;
            this.runnableUnits = null;
            this.scheduler.Unregister(this);
            await base.StopAsync();

            if (this.channel != null)
            {
                await this.channel.StopAsync();

                this.channel = null;
            }

            this.session = null;
            this.controller.Dispose();
        }
Ejemplo n.º 16
0
 public void OnTransportException(IUdpSession session, Exception ex)
 {
 }
Ejemplo n.º 17
0
 public void OnTransportInactive(IUdpSession session)
 {
 }
Ejemplo n.º 18
0
 public void OnTransportActive(IUdpSession session)
 {
     this.session = session;
     this.controller.OnUdpActive(session);
 }
Ejemplo n.º 19
0
 public void OnTransportInactive(IUdpSession session)
 {
     this.session = null;
     this.controller.OnUdpInactive(session);
 }
Ejemplo n.º 20
0
        public void OnTransportException(IUdpSession session, Exception ex)
        {
            this.isInvalid = true;

            this.controller.OnUdpException(session, ex);
        }
Ejemplo n.º 21
0
 public void OnTransportException(IUdpSession session, Exception ex)
 {
     Console.WriteLine($"{nameof(UdpFilter_2)}.{nameof(OnTransportException)}" + ex.ToString());
 }
Ejemplo n.º 22
0
 public abstract void OnUdpException(IUdpSession session, Exception ex);
Ejemplo n.º 23
0
 public abstract void OnUdpMessageInput(IUdpSession session, ref WrappedReader reader, WrappedArgs args);
Ejemplo n.º 24
0
 public abstract void OnUdpMessageOutput(IUdpSession session, ref WrappedWriter writer, WrappedArgs args);
Ejemplo n.º 25
0
        public KcpSession(uint conv, EndPoint remoteAddress, EndPoint localAddress, KcpOptions options, IUdpSession udpSession, IEventSubscriber subscriber, FilterPipeline <IKcpSession> pipeline, IKcpClosable closable)
        {
            this.Id            = IdGeneratorHelper.GetNextId();
            this.Conv          = conv;
            this.LocalAddress  = localAddress;
            this.RemoteAddress = remoteAddress;

            this.Order      = options.Order;
            this.udpSession = udpSession;
            this.subscriber = subscriber;
            this.Pipeline   = pipeline;
            this.closable   = closable;

            var littleEndian = this.Order == BinaryOrder.LittleEndian;

            this.kcpKit = new KcpKit(conv, littleEndian, this.MemoryPool);
            this.kcpKit.SettingMtu(options.Mtu);
            this.kcpKit.SettingNoDelay(options.NoDelay);
            this.kcpKit.SettingWndSize(options.WndSize);
            this.kcpKit.SettingStreamMode(options.StreamMode);
            this.kcpKit.SettingReservedSize(options.ReservedSize);

            this.kcpKit.onRcv += this.OnKcpRcvEvent;
            this.kcpKit.onSnd += this.OnKcpSndEvent;

            this.kcpOperators = BinaryOrderOperatorsFactory.GetOperators(this.Order);
            this.rcvPool      = new WrappedMemoryPool(this.MemoryPool, MemoryFlag.Kcp);
            this.sndPool      = new WrappedMemoryPool(this.MemoryPool, MemoryFlag.Kcp);

            this.sndMemory       = new WrappedMemory(this.MemoryPool.Rent(), MemoryFlag.Kcp);
            this.readerUdpMemory = new WrappedMemory(this.MemoryPool.Rent(), MemoryFlag.Udp);
            this.writerUdpMemory = new WrappedMemory(this.MemoryPool.Rent(), MemoryFlag.Udp);

            this.readerFlushDelegate = (pos, endPos) => { };
            this.writerFlushDelegate = this.OnWriterComplete;

            this.runnableUnitDelegate = this.Update;
            this.subscriber.Register(this.runnableUnitDelegate);

            this.Pipeline.OnTransportActive(this);
        }
Ejemplo n.º 26
0
 public void OnTransportInactive(IUdpSession session)
 {
     Console.WriteLine($"{nameof(UdpFilter_2)}.{nameof(OnTransportInactive)}");
 }
Ejemplo n.º 27
0
 public abstract void OnUdpInactive(IUdpSession session);
Ejemplo n.º 28
0
 public override void OnUdpInactive(IUdpSession session)
 {
 }
Ejemplo n.º 29
0
        static void ReceiveCompleted(object sender, ReceiveCommond e)
        {
            IUdpSession session = sender as IUdpSession;

            Console.WriteLine(Encoding.UTF8.GetString(e.Data));
        }