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); }
protected virtual void OnAccepted(SocketSession session) { session.Closed += OnClosed; session.StartReceive(); _sessions.Add(session); session.Handlers?.Accept(new HandleContext { Session = session }); }
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 }); }
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 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); } } }
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); }
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 }); } } }
protected virtual void OnClosed(SocketSession session) { session.Closed -= OnClosed; _sessions.Remove(session); //session.Handlers?.Close(new HandleContext { Session = session }); }
private void OnClosed(object sender, EventArgs e) { SocketSession session = sender as SocketSession; OnClosed(session); }
private void OnAccepted(Socket client) { SocketSession session = GetSession(client); OnAccepted(session); }
protected override void OnClosed(SocketSession session) { Raise(() => { base.OnClosed(session); }); }
public override void Accept(HandleContext context) { session = context.Session; }
public SocketConverter(SocketSession session) { this.session = session; this.ns = new NetworkStream(session.Socket); }
public ReceivedEventArgs(SocketSession session, object packet) { Session = session; Packet = packet; }
public ClosedEventArgs(SocketSession session) { Session = session; }
public AcceptdEventArgs(SocketSession session) { Session = session; }