public void _RegSession(ZYNetServer Server, ReadBytes read, ZYNetSession session) { RegSession regSession; if (read.ReadObject <RegSession>(out regSession)) { session.LANIP = regSession.LocalHost; session.Group = regSession.Group; if (!Server.SessionDiy.ContainsKey(session.Id)) { regSession.Id = session.Id; regSession.IsSuccess = true; regSession.Port = Server.RegPort; Server.SessionDiy.AddOrUpdate(session.Id, session, (a, b) => session); Server.Send(session, BufferFormat.FormatFCA(regSession)); AddSession add = new AddSession() { Id = regSession.Id }; Server.SendAll(BufferFormat.FormatFCA(add)); } else { regSession.IsSuccess = false; regSession.Msg = "User Id Is Have"; Server.Send(session, BufferFormat.FormatFCA(regSession)); Server.Service.Disconnect(session.Asyn.AcceptSocket); } } }
/// <summary> /// 用户断开处理回调 /// </summary> /// <param name="msg"></param> /// <param name="socketAsync"></param> /// <param name="error"></param> private void Service_UserDisconnect(string msg, SocketAsyncEventArgs socketAsync, int error) { ZYNetSession session = socketAsync.UserToken as ZYNetSession; if (session != null) { if (UserDisconnect != null) { UserDisconnect(session); } SessionDiy.TryRemove(session.Id, out session); RemoveSession tmp = new RemoveSession() { Id = session.Id }; SendAll(BufferFormat.FormatFCA(tmp)); } socketAsync.UserToken = null; socketAsync.AcceptSocket.Close(); socketAsync.AcceptSocket.Dispose(); LLOG("Servv:" + msg, EventLogType.INFO); }
public void _SetGroup(ZYNetServer Server, ReadBytes read, ZYNetSession session) { int group; if (read.ReadInt32(out group)) { session.Group = group; } }
/// <summary> /// 发送数据包给制定的客户端 /// </summary> /// <param name="Session"></param> /// <param name="data"></param> public void SendDataToClient(ZYNetSession Session, byte[] data) { ProxyData pdata = new ProxyData() { Data = data }; byte[] buff = BufferFormat.FormatFCA(pdata); Send(Session, buff); }
/// <summary> /// 数据包输入 以处理 /// </summary> /// <param name="data"></param> /// <param name="session"></param> private void DataOn(byte[] data, ZYNetSession session) { ReadBytes read = new ReadBytes(data); int length; int cmd; if (read.ReadInt32(out length) && length == read.Length && read.ReadInt32(out cmd)) { if (cmd != -2000) { if (!CmdToCallManager <ZYNetServer, ReadBytes, ZYNetSession> .GetInstance().pointerRun(this, cmd, read, session)) { LLOG("Not Find CMD:" + cmd, EventLogType.ERR); } } else { ProxyData tmp; if (read.ReadObject <ProxyData>(out tmp)) { if (tmp.Source == session.Id) { if (tmp.Ids != null) { if (tmp.Ids.Contains(0)) { if (UserDataInput != null) { UserDataInput(session, tmp.Data); } } foreach (var Id in tmp.Ids) { if (Id != 0 && SessionDiy.ContainsKey(Id)) { Service.SendData(SessionDiy[Id].Asyn.AcceptSocket, data); } } } } } } } }
public void _LEFTConnect(ZYNetServer Server, ReadBytes read, ZYNetSession session) { long ToId; if (read.ReadInt64(out ToId)) { if (Server.SessionDiy.ContainsKey(ToId) && !string.IsNullOrEmpty(session.WANIP) && session.WANPort != 0) { ZYNetSession toUser = Server.SessionDiy[ToId]; if (toUser.Group != session.Group) { return; } if (!session.WANIP.Equals(toUser.WANIP)) //如果不再一个局域网内 { LEFTConnect tmp = new LEFTConnect() { Id = session.Id, Host = session.WANIP, Port = session.WANPort, IsSuccess = true }; Server.Send(toUser, BufferFormat.FormatFCA(tmp)); } else //同局域网内 { LEFTConnect tmp = new LEFTConnect() { Id = session.Id, Host = session.LANIP, Port = session.NatNextPort, IsSuccess = true }; Server.Send(toUser, BufferFormat.FormatFCA(tmp)); } } } }
public void _GetAllSession(ZYNetServer Server, ReadBytes read, ZYNetSession session) { GetAllSession pack; if (read.ReadObject <GetAllSession>(out pack)) { var userlist = Server.SessionDiy.Values.Where(p => p.Group == session.Group && p.Id != session.Id); pack.UserIds = new List <long>(); foreach (var item in userlist) { pack.UserIds.Add(item.Id); } pack.IsSuccess = true; Server.Send(session, BufferFormat.FormatFCA(pack)); } }
/// <summary> /// 输入包输入回调 未处理 /// </summary> /// <param name="data"></param> /// <param name="socketAsync"></param> private void Service_BinaryInput(byte[] data, int offset, int count, SocketAsyncEventArgs socketAsync) { try { ZYNetSession usertmp = (socketAsync.UserToken as ZYNetSession); if (usertmp != null) { usertmp.BufferQueue.Write(data, offset, count); byte[] datax; while (usertmp.BufferQueue.Read(out datax)) { DataOn(datax, usertmp); } } } catch (Exception er) { LLOG(er.ToString(), EventLogType.ERR); } }
/// <summary> /// 有用户连接端口 /// </summary> /// <param name="socketAsync"></param> /// <returns></returns> private bool Service_Connection(SocketAsyncEventArgs socketAsync) { bool IsConnect = true; var Ipaddress = ((IPEndPoint)socketAsync.AcceptSocket.RemoteEndPoint); if (UserConnectAuthority != null) { IsConnect = UserConnectAuthority(Ipaddress.Address, Ipaddress.Port); } if (IsConnect) { ZYNetSession session = new ZYNetSession(MakeId(), socketAsync, new ZYNetRingBufferPool(BufferMaxLength)); session.WANIP = Ipaddress.Address.ToString(); socketAsync.UserToken = session; LLOG("Servv: " + Ipaddress.ToString() + " Connect", EventLogType.INFO); } return(IsConnect); }
/// <summary> /// 发送数据包给制定的客户端 内部专用 /// </summary> /// <param name="Session"></param> /// <param name="data"></param> internal void Send(ZYNetSession Session, byte[] data) { Service.SendData(Session.Asyn.AcceptSocket, data); }
private static void Server_UserDataInput(ZYNetSession session, byte[] data) { Console.WriteLine(session.Id + ":" + Encoding.Default.GetString(data)); }