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); }
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 }); } }
public void Close(HandleContext context) { foreach (var handler in this) { if (context.Cancel) { break; } handler?.Close(context); } }
public void Send(HandleContext context) { foreach (var handler in this) { if (context.Cancel) { break; } handler?.Send(context); } }
public void Accept(HandleContext context) { foreach (var handler in this) { if (context.Cancel) { break; } handler?.Accept(context); } }
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)); }); }
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); } } }
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); }
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); } } } }
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 }); } } }
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); } }
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); } } }
public virtual void Handle(HandleContext context) { }
public virtual void Close(HandleContext context) { }
public virtual void Accept(HandleContext context) { }
public virtual void Send(HandleContext context) { }
public override void Accept(HandleContext context) { session = context.Session; }