예제 #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;
 }