Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public override async void Close(HandleContext context)
        {
            Console.WriteLine("----Close");

            foreach (var session in sessions)
            {
                await session.Key.SendAsync(new ClientCloseInfo {
                    SrcUserId = context.Session.Id
                });
            }
        }
Exemplo n.º 3
0
 public void Close(HandleContext context)
 {
     foreach (var handler in this)
     {
         if (context.Cancel)
         {
             break;
         }
         handler?.Close(context);
     }
 }
Exemplo n.º 4
0
 public void Send(HandleContext context)
 {
     foreach (var handler in this)
     {
         if (context.Cancel)
         {
             break;
         }
         handler?.Send(context);
     }
 }
Exemplo n.º 5
0
 public void Accept(HandleContext context)
 {
     foreach (var handler in this)
     {
         if (context.Cancel)
         {
             break;
         }
         handler?.Accept(context);
     }
 }
Exemplo n.º 6
0
        protected virtual void OnReceived(object packet)
        {
            Raise(() => {
                HandleContext context = new HandleContext {
                    Packet  = packet,
                    Session = this
                };

                Handlers?.Handle(context);
                Received?.Invoke(this, new ReceivedEventArgs(this, packet));
            });
        }
Exemplo n.º 7
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);
                }
            }
        }
Exemplo n.º 8
0
        public virtual async Task SendAsync(object packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException("packet");
            }

            HandleContext context = new HandleContext {
                Session = this,
                Packet  = packet
            };

            Handlers.Send(context);


            await GetConverter().WriteAsync(context.Packet);
        }
Exemplo n.º 9
0
 public override void Close(HandleContext context)
 {
     foreach (var def in serviceDefs)
     {
         foreach (var instance in def.Value.Instances.ToList())
         {
             if (instance.Value.Dynamic)
             {
                 var dispose = instance.Value.Service as IDisposable;
                 if (dispose != null)
                 {
                     dispose.Dispose();
                 }
                 def.Value.Instances.Remove(instance.Key);
             }
         }
     }
 }
Exemplo n.º 10
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
                    });
                }
            }
        }
Exemplo n.º 11
0
        public override void Handle(HandleContext context)
        {
            var packet  = context.Packet;
            var session = context.Session;

            var response = packet as IResponse;

            if (response != null)
            {
                Console.WriteLine("调用回复");

                Invoked?.Invoke(response);
            }

            var e = packet as EventInfo;

            if (e != null)
            {
                Console.WriteLine("事件");

                Raised?.Invoke(e);
            }
        }
Exemplo n.º 12
0
        public override async void Handle(HandleContext context)
        {
            var packet  = context.Packet;
            var session = context.Session;

            ServiceInvoke info = packet as ServiceInvoke;

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

                Console.WriteLine("收到调用{0}.{1} {2}->{3}",
                                  info.Name, info.Action, info.SrcUserId, info.DestUserId);

                var result = Invoke(info);

                await session.SendAsync(result);
            }
            else
            {
                Subscribe sub = packet as Subscribe;

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

                    Console.WriteLine("收到事件注册");

                    object service = GetService(sub.Name, sub.Instance);

                    var e = service.GetType().GetEvent(sub.Action);

                    //AddEventHandler(sub.Name, service);
                }
            }
        }
Exemplo n.º 13
0
 public virtual void Handle(HandleContext context)
 {
 }
Exemplo n.º 14
0
 public virtual void Close(HandleContext context)
 {
 }
Exemplo n.º 15
0
 public virtual void Accept(HandleContext context)
 {
 }
Exemplo n.º 16
0
 public virtual void Send(HandleContext context)
 {
 }
Exemplo n.º 17
0
 public override void Accept(HandleContext context)
 {
     session = context.Session;
 }