/// <summary> /// 用户断开代理(你可以根据socketAsync 读取到断开的 /// </summary> /// <param name="message">断开消息</param> /// <param name="socketAsync">断开的SOCKET</param> /// <param name="erorr">错误的ID</param> static void MessageInputHandler(string message, SocketAsyncEventArgs socketAsync, int erorr) { if (socketAsync.UserToken != null) { User.UserInfo user = socketAsync.UserToken as User.UserInfo; Console.WriteLine(user.UserName + " 退了"); socketAsync.UserToken = null; //这里一定要设置为NULL 否则出现的错误 很爽 } }
public void LogOn(ZYSocketSuper server, ReadBytes read, SocketAsyncEventArgs socketAsync) { Login p; if (read.ReadObject<Login>(out p)) { if (p != null) { if (User.UserManger.GetUserDataManger().CheckUser(p.UserName, p.PassWord))//检查用户名密码是否正确 { User.UserInfo user = new User.UserInfo() //建立一个新的用户对象 并且初始化 用户名 { UserName = p.UserName }; socketAsync.UserToken = user; //设置USERTOKEN Message err = new Message() //初始化MESSAGE数据包类 { Type = 2, MessageStr = "登入成功" }; server.SendData(socketAsync.AcceptSocket, BufferFormat.FormatFCA(err)); //发送此类 Console.WriteLine(user.UserName + " 登入"); } else { Message err = new Message() //初始化用户名密码错误数据包 { Type = 1, MessageStr = "用户名或密码错误" }; server.SendData(socketAsync.AcceptSocket, BufferFormat.FormatFCA(err)); } } } }
public void LogOn(ZYSocketSuper server, ReadBytes read, SocketAsyncEventArgs socketAsync) { Login p; if (read.ReadObject <Login>(out p)) { if (p != null) { if (User.UserManger.GetUserDataManger().CheckUser(p.UserName, p.PassWord)) //检查用户名密码是否正确 { User.UserInfo user = new User.UserInfo() //建立一个新的用户对象 并且初始化 用户名 { UserName = p.UserName }; socketAsync.UserToken = user; //设置USERTOKEN Message err = new Message() //初始化MESSAGE数据包类 { Type = 2, MessageStr = "登入成功" }; server.SendData(socketAsync.AcceptSocket, BufferFormat.FormatFCA(err)); //发送此类 Console.WriteLine(user.UserName + " 登入"); } else { Message err = new Message() //初始化用户名密码错误数据包 { Type = 1, MessageStr = "用户名或密码错误" }; server.SendData(socketAsync.AcceptSocket, BufferFormat.FormatFCA(err)); } } } }
/// <summary> /// 数据包输入 /// </summary> /// <param name="data">输入数据</param> /// <param name="socketAsync">该数据包的通讯SOCKET</param> static void BinaryInputHandler(byte[] data, SocketAsyncEventArgs socketAsync) { if (socketAsync.UserToken == null) //如果用户第一次登入 { ReadBytes read = new ReadBytes(data); int length; int cmd; if (read.ReadInt32(out length) && length == read.Length && read.ReadInt32(out cmd)) { if (!CmdToCallManager <ZYSocketSuper, int, ReadBytes, SocketAsyncEventArgs> .GetInstance().pointerRun(server, cmd, read, socketAsync)) //如果用户发送的是登入数据包 { server.Disconnect(socketAsync.AcceptSocket); } } else //无法读取数据包 断开连接 { server.Disconnect(socketAsync.AcceptSocket); } } else { User.UserInfo user = socketAsync.UserToken as User.UserInfo; //读取用户USERTRKEN if (user != null) { if (user.BuffManger.Write(data)) //这里的 4表示 数据包长度是用4字节的整数存储的 Int { byte[] pdata; while (user.BuffManger.Read(out pdata)) { DataOn(pdata, socketAsync); } } } } }
/// <summary> /// 数据包输入 /// </summary> /// <param name="data">输入数据</param> /// <param name="socketAsync">该数据包的通讯SOCKET</param> static void BinaryInputHandler(byte[] data, SocketAsyncEventArgs socketAsync) { if (socketAsync.UserToken == null) //如果用户第一次登入 { ReadBytes read = new ReadBytes(data); int length; int cmd; if (read.ReadInt32(out length) && length == read.Length && read.ReadInt32(out cmd)) { if (cmd == 1000) //如果用户发送的是登入数据包 { Login p; if (read.ReadObject <Login>(out p)) { if (p != null) { if (User.UserManger.GetUserDataManger().CheckUser(p.UserName, p.PassWord))//检查用户名密码是否正确 { User.UserInfo user = new User.UserInfo() //建立一个新的用户对象 并且初始化 用户名 { UserName = p.UserName }; socketAsync.UserToken = user; //设置USERTOKEN Message err = new Message() //初始化MESSAGE数据包类 { Type = 2, MessageStr = "登入成功" }; server.SendData(socketAsync.AcceptSocket, BufferFormat.FormatFCA(err)); //发送此类 Console.WriteLine(user.UserName + " 登入"); } else { Message err = new Message() //初始化用户名密码错误数据包 { Type = 1, MessageStr = "用户名或密码错误" }; server.SendData(socketAsync.AcceptSocket, BufferFormat.FormatFCA(err)); } } } } else //现在还没登入 如果有其他命令的请求那么 断开连接 { server.Disconnect(socketAsync.AcceptSocket); } } else //无法读取数据包 断开连接 { server.Disconnect(socketAsync.AcceptSocket); } } else { User.UserInfo user = socketAsync.UserToken as User.UserInfo; //读取用户USERTRKEN if (user != null) { user.BuffManger.Write(data); byte[] pdata; while (user.BuffManger.Read(out pdata)) { DataOn(pdata, socketAsync); } } } }
/// <summary> /// 数据包输入 /// </summary> /// <param name="data">输入数据</param> /// <param name="socketAsync">该数据包的通讯SOCKET</param> static void BinaryInputHandler(byte[] data, SocketAsyncEventArgs socketAsync) { if (socketAsync.UserToken == null) //如果用户第一次登入 { ReadBytes read = new ReadBytes(data); int length; int cmd; if (read.ReadInt32(out length) && length == read.Length && read.ReadInt32(out cmd)) { if (cmd == 1000) //如果用户发送的是登入数据包 { Login p; if (read.ReadObject <Login>(out p)) { if (p != null) { if (User.UserManger.GetUserDataManger().CheckUser(p.UserName, p.PassWord)) //检查用户名密码是否正确 { User.UserInfo user = new User.UserInfo() //建立一个新的用户对象 并且初始化 用户名 { UserName = p.UserName }; socketAsync.UserToken = user; //设置USERTOKEN Message err = new Message() //初始化MESSAGE数据包类 { Type = 2, MessageStr = "登入成功" }; server.SendData(socketAsync.AcceptSocket, BufferFormat.FormatFCA(err)); //发送此类 Console.WriteLine(user.UserName + " 登入"); } else { Message err = new Message() //初始化用户名密码错误数据包 { Type = 1, MessageStr = "用户名或密码错误" }; server.SendData(socketAsync.AcceptSocket, BufferFormat.FormatFCA(err)); } } } } else //现在还没登入 如果有其他命令的请求那么 断开连接 { server.Disconnect(socketAsync.AcceptSocket); } } else //无法读取数据包 断开连接 { server.Disconnect(socketAsync.AcceptSocket); } } else { User.UserInfo user = socketAsync.UserToken as User.UserInfo; //读取用户USERTRKEN if (user != null) { user.BuffManger.Write(data); byte[] pdata; while (user.BuffManger.Read(out pdata)) { DataOn(pdata, socketAsync); } } } }