示例#1
0
        private SocketSession GetSession(HandleContext context, string userId, ref bool accept)
        {
            SocketSession session = null;

            lock (sessions.SyncObj) {
                session = sessions.Where(x => x.Id == userId).FirstOrDefault();

                if (session == null)
                {
                    session = GetSession(context.Session.Socket, userId);

                    sessions.Add(session);

                    accept = true;
                }
            }

            if (accept)
            {
                session.Handlers.Accept(new HandleContext {
                    Session = session
                });
            }

            return(session);
        }
示例#2
0
        protected virtual void OnAccepted(SocketSession session)
        {
            session.Closed += OnClosed;
            session.StartReceive();
            _sessions.Add(session);

            session.Handlers?.Accept(new HandleContext {
                Session = session
            });
        }
示例#3
0
        public async void Raise(SocketSession session, string name, string e, string instance, params object[] args)
        {
            Console.WriteLine("触发事件");

            await session.SendAsync(new EventInfo {
                Event     = e,
                Name      = name,
                Id        = Guid.NewGuid(),
                Arguemnts = args,
                Instance  = instance
            });
        }
示例#4
0
        public override async void Handle(HandleContext context)
        {
            var packet = context.Packet;

            var transfer = context.Packet as ITransferable;

            if (transfer == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(transfer.DestUserId))
            {
                return;
            }

            context.Cancel = true;

            transfer.SrcUserId = context.Session.Id;

            Console.WriteLine("中转消息:{0}->{1}", transfer.SrcUserId, transfer.DestUserId);

            SocketSession session = Find(transfer);

            if (session != null)
            {
                lock (sessions) {
                    if (!sessions.ContainsKey(session))
                    {
                        sessions.Add(session, null);
                    }
                }

                await session.SendAsync(transfer);
            }
            else
            {
                var request = packet as IRequest;

                if (request != null)
                {
                    Response response = new Response {
                        Id      = request.Id,
                        Message = "客户端不在线"
                    };
                    await context.Session.SendAsync(response);
                }
            }
        }
示例#5
0
        public async Task SendAsync(SocketSession session, Packet packet)
        {
            var packets = Split(packet);

            if (packets == null)
            {
                await innerConverter.SendAsync(session, packet);
            }
            else
            {
                foreach (var packet0 in packets)
                {
                    await innerConverter.SendAsync(session, packet0);
                }
            }
        }
示例#6
0
        public async Task <Packet> ReceiveAsync(SocketSession session)
        {
            Packet packet = await innerConverter.ReceiveAsync(session);

            while (packet.Type < 0)
            {
                packets.Add(packet);
                packet = await innerConverter.ReceiveAsync(session);

                if (packet.Type == PacketPartPackets.PacketEnd)
                {
                    Packet packet2 = GetPacket();
                    return(packet2);
                }
            }

            return(packet);
        }
示例#7
0
        public override void Handle(HandleContext context)
        {
            var packet = context.Packet;


            var closeInfo = context.Packet as ClientCloseInfo;

            if (closeInfo != null)
            {
                context.Cancel = true;

                string        userId2  = closeInfo.SrcUserId;
                bool          value    = false;
                SocketSession session2 = GetSession(context, userId2, ref value);
                session2.Handlers.Close(new HandleContext {
                    Session = session2
                });

                Console.WriteLine("----客户端关闭");
            }
            else
            {
                var transfer = context.Packet as ITransferable;

                if (transfer != null)
                {
                    context.Cancel = true;

                    string userId = transfer.SrcUserId;

                    bool accept = false;

                    SocketSession session = GetSession(context, userId, ref accept);

                    session.Handlers.Handle(new HandleContext {
                        Session = session,
                        Packet  = packet
                    });
                }
            }
        }
示例#8
0
 protected virtual void OnClosed(SocketSession session)
 {
     session.Closed -= OnClosed;
     _sessions.Remove(session);
     //session.Handlers?.Close(new HandleContext { Session = session });
 }
示例#9
0
        private void OnClosed(object sender, EventArgs e)
        {
            SocketSession session = sender as SocketSession;

            OnClosed(session);
        }
示例#10
0
        private void OnAccepted(Socket client)
        {
            SocketSession session = GetSession(client);

            OnAccepted(session);
        }
示例#11
0
 protected override void OnClosed(SocketSession session)
 {
     Raise(() => {
         base.OnClosed(session);
     });
 }
示例#12
0
 public override void Accept(HandleContext context)
 {
     session = context.Session;
 }
示例#13
0
 public SocketConverter(SocketSession session)
 {
     this.session = session;
     this.ns      = new NetworkStream(session.Socket);
 }
示例#14
0
 public ReceivedEventArgs(SocketSession session, object packet)
 {
     Session = session;
     Packet  = packet;
 }
示例#15
0
 public ClosedEventArgs(SocketSession session)
 {
     Session = session;
 }
示例#16
0
 public AcceptdEventArgs(SocketSession session)
 {
     Session = session;
 }