コード例 #1
0
ファイル: TransferHandler.cs プロジェクト: jhk2011/Dao.Net
        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
ファイル: TransferHandler.cs プロジェクト: jhk2011/Dao.Net
        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
                });
            }
        }
コード例 #3
0
 public void Close(HandleContext context)
 {
     foreach (var handler in this)
     {
         if (context.Cancel)
         {
             break;
         }
         handler?.Close(context);
     }
 }
コード例 #4
0
 public void Send(HandleContext context)
 {
     foreach (var handler in this)
     {
         if (context.Cancel)
         {
             break;
         }
         handler?.Send(context);
     }
 }
コード例 #5
0
 public void Accept(HandleContext context)
 {
     foreach (var handler in this)
     {
         if (context.Cancel)
         {
             break;
         }
         handler?.Accept(context);
     }
 }
コード例 #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));
            });
        }
コード例 #7
0
ファイル: TransferHandler.cs プロジェクト: jhk2011/Dao.Net
        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);
                }
            }
        }
コード例 #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);
        }
コード例 #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);
             }
         }
     }
 }
コード例 #10
0
ファイル: TransferHandler.cs プロジェクト: jhk2011/Dao.Net
        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
                    });
                }
            }
        }
コード例 #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);
            }
        }
コード例 #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);
                }
            }
        }
コード例 #13
0
 public virtual void Handle(HandleContext context)
 {
 }
コード例 #14
0
 public virtual void Close(HandleContext context)
 {
 }
コード例 #15
0
 public virtual void Accept(HandleContext context)
 {
 }
コード例 #16
0
 public virtual void Send(HandleContext context)
 {
 }
コード例 #17
0
 public override void Accept(HandleContext context)
 {
     session = context.Session;
 }