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); }
private void timer1_Tick(object sender, EventArgs e) //一个TIMER 每过一段事件就 发送一个PING包 { Ping temp = new Ping() { UserSendTime = DateTime.Now }; SocketManager.client.SendTo(BufferFormat.FormatFCA(temp)); }
private void button1_Click(object sender, EventArgs e) { DrawBoardPACK.LogOn tmp = new DrawBoardPACK.LogOn() { UserName = this.textBox1.Text }; Client.SendDataToServer(BufferFormat.FormatFCA(tmp)); }
private void button3_Click(object sender, EventArgs e) { SaveImg tmp = new SaveImg() { FileName = Guid.NewGuid().ToString().Replace("-", "") + ".bmp" }; Client.SendDataToALL(BufferFormat.FormatFCA(tmp)); }
private void timer1_Tick(object sender, EventArgs e) //一个TIMER 每过一段事件就 发送一个PING包 { Ping temp = new Ping() { UserSendTime = DateTime.Now }; SocketManager.client.SendTo(BufferFormat.FormatFCA(temp, (pdata) => DES.EncryptDES(pdata, keys, "------TEST------"))); }
private void button1_Click(object sender, EventArgs e) { ReadDataSet temp = new ReadDataSet() //发送一个DATASET请求 { TableName = "table1" }; SocketManager.client.SendTo(BufferFormat.FormatFCA(temp, (pdata) => DES.EncryptDES(pdata, keys, "------TEST------"))); }
private void button1_Click(object sender, EventArgs e) { ReadDataSet temp = new ReadDataSet() //发送一个DATASET请求 { TableName = "table1" }; SocketManager.client.SendTo(BufferFormat.FormatFCA(temp)); }
public void CallMethod(string module, string MethodName, List <Type> argTypeList, List <byte[]> arglist, out object[] args) { args = null; RPCCallPack call = new RPCCallPack() { Id = MakeID.GetID(), CallTime = MakeID.GetTick(), CallModule = module, Method = MethodName, Arguments = arglist }; WaitReturnValue var = new WaitReturnValue(); using (var.waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset)) { ReturnValueDiy.Add(call.Id, var); byte[] data = BufferFormat.FormatFCA(call); if (CallBufferOutSend != null) { CallBufferOutSend(data); } if (var.waitHandle.WaitOne(OutTime)) { ZYClient_Result_Return returnx = var.returnvalue; if (returnx.Arguments != null && returnx.Arguments.Count > 0 && arglist.Count == returnx.Arguments.Count) { args = new object[returnx.Arguments.Count]; for (int i = 0; i < argTypeList.Count; i++) { args[i] = Serialization.UnpackSingleObject(argTypeList[i], returnx.Arguments[i]); } } return; } else { ReturnValueDiy.Remove(call.Id); if (ErrMsgOut != null) { ErrMsgOut(module + "->" + MethodName + " out time,Please set the timeout time."); } return; } } }
private void button1_Click(object sender, EventArgs e) { Login temp = new Login() //发送一个登入数据包 { UserName = this.textBox1.Text, PassWord = this.textBox2.Text }; SocketManager.client.SendTo(BufferFormat.FormatFCA(temp)); }
private void button1_Click(object sender, EventArgs e) { Login temp = new Login() //发送一个登入数据包 { UserName = this.textBox1.Text, PassWord = this.textBox2.Text }; SocketManager.client.SendTo(BufferFormat.FormatFCA(temp, (pdata) => DES.EncryptDES(pdata, keys, "------TEST------"))); }
private void button2_Click(object sender, EventArgs e) { gs.Clear(Color.White); gs2.Clear(Color.White); ClecrColor tmp = new ClecrColor(); tmp.Color = Color.White.ToArgb(); Client.SendDataToALL(BufferFormat.FormatFCA(tmp)); }
public void ReadDataSet(ZYSocketSuper server, ReadBytes read, SocketAsyncEventArgs socketAsync) { ReadDataSet rd; if (read.ReadObject <ReadDataSet>(out rd)) //读取请求DATASET 数据包 { if (rd != null) { rd.Data = new List <DataValue>(); rd.TableName = "table1"; rd.Data.Add(new DataValue() { V1 = "第1个", V2 = "第2个", V3 = "第3个", V4 = "第4个", V5 = "第5个" }); rd.Data.Add(new DataValue() { V1 = "第6个", V2 = "第7个", V3 = "第8个", V4 = "第9个", V5 = "第10个" }); rd.Data.Add(new DataValue() { V1 = "第11个", V2 = "第12个", V3 = "第13个", V4 = "第14个", V5 = "第15个" }); rd.Data.Add(new DataValue() { V1 = "第16个", V2 = "第17个", V3 = "第18个", V4 = "第19个", V5 = "第20个" }); server.SendData(socketAsync.AcceptSocket, BufferFormat.FormatFCA(rd)); //发送 Console.WriteLine((socketAsync.UserToken as User.UserInfo).UserName + " 读取了" + rd.TableName); } } }
public void CallMethod(string module, string MethodName, List <Type> argTypeList, List <byte[]> arglist, out object[] args) { args = null; RPCCallPack call = new RPCCallPack() { Id = MakeID.GetID(), CallTime = MakeID.GetTick(), CallModule = module, Method = MethodName, Arguments = arglist }; TaskCompletionSource <ZYClient_Result_Return> var = new TaskCompletionSource <ZYClient_Result_Return>(TaskCreationOptions.AttachedToParent); if (!ReturnValueDiy.TryAdd(call.Id, var)) { SpinWait.SpinUntil(() => ReturnValueDiy.TryAdd(call.Id, var)); } byte[] data = BufferFormat.FormatFCA(call); if (CallBufferOutSend != null) { CallBufferOutSend(data); } if (var.Task.Wait(OutTime)) { ZYClient_Result_Return returnx = var.Task.Result; if (returnx.Arguments != null && returnx.Arguments.Count > 0 && arglist.Count == returnx.Arguments.Count) { args = new object[returnx.Arguments.Count]; for (int i = 0; i < argTypeList.Count; i++) { args[i] = Serialization.UnpackSingleObject(argTypeList[i], returnx.Arguments[i]); } } } else { ReturnValueDiy.TryRemove(call.Id, out var); if (ErrMsgOut != null) { ErrMsgOut(module + "->" + MethodName + " out time,Please set the timeout time."); } } }
/// <summary> /// 发送数据包给所有客户端 /// </summary> /// <param name="data"></param> public void SendDataToAllClient(byte[] data) { ProxyData pdata = new ProxyData() { Data = data }; byte[] buff = BufferFormat.FormatFCA(pdata); SendAll(buff); }
/// <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); }
public void CallMethod(string module, string MethodName, List <RPCArgument> arglist, out object[] args) { args = null; RPCCallPack call = new RPCCallPack() { Id = MakeID.GetID(), CallTime = DateTime.Now, CallModule = module, Method = MethodName, Arguments = arglist, IsNeedReturn = true, }; WaitReturnValue var = new WaitReturnValue(); using (var.waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset)) { ReturnValueDiy.AddOrUpdate(call.Id, var, (a, b) => var); byte[] data = BufferFormat.FormatFCA(call); if (CallBufferOutSend != null) { CallBufferOutSend(data); } if (var.waitHandle.WaitOne(OutTime)) { ZYClient_Result_Return returnx = var.returnvalue; if (returnx.Arguments != null && returnx.Arguments.Count > 0 && arglist.Count == returnx.Arguments.Count) { args = new object[returnx.Arguments.Count]; for (int i = 0; i < returnx.Arguments.Count; i++) { args[i] = Serialization.UnpackSingleObject(returnx.Arguments[i].type, returnx.Arguments[i].Value); } } return; } else { ReturnValueDiy.TryRemove(call.Id, out var); throw new TimeoutException("out time,Please set the timeout time."); } } }
public void Ping(ZYSocketSuper server, ReadBytes read, SocketAsyncEventArgs socketAsync) { Ping pdata; if (read.ReadObject <Ping>(out pdata)) //读取PING 数据包 { if (pdata != null) { pdata.ServerReviceTime = DateTime.Now; //设置服务器时间 server.SendData(socketAsync.AcceptSocket, BufferFormat.FormatFCA(pdata)); //发送返回 } } }
static void Main(string[] args) { client.DataOn += new DataOn(client_DataOn); //数据包进入事件 client.Disconnection += new ExceptionDisconnection(client_Disconnection); //数据包断开事件 if (client.ConnectionTo("127.0.0.1", 9982)) //使用同步连接到服务器,一步就用Begin开头的那个 { while (true) { Console.ReadLine(); testClass.PPo temp = new testClass.PPo(); temp.Id = 1; temp.Message = "通过对象通讯"; temp.guid = new List <Guid>(); for (int i = 0; i < 100; i++) { temp.guid.Add(Guid.NewGuid()); } client.SendTo(BufferFormat.FormatFCA(temp)); //讲一个PPO对象发送出去 // Console.ReadLine(); BufferFormat buffmat = new BufferFormat(1001); buffmat.AddItem(2); buffmat.AddItem("通过组合数据包通讯,GUID is object"); buffmat.AddItem(Guid.NewGuid()); client.SendTo(buffmat.Finish()); //用组合数据包模拟PPO对象 // Console.ReadLine(); BufferFormat buffmat2 = new BufferFormat(1002); buffmat2.AddItem(3); buffmat2.AddItem("通过组合数据包通讯 all buff"); buffmat2.AddItem(Guid.NewGuid().ToString()); client.SendTo(buffmat2.Finish()); //用组合数据包模拟PPO对象 但GUID 是字符串类型 } } else { Console.WriteLine("无法连接服务器"); } Console.ReadLine(); }
/// <summary> /// 调用NULL返回值的函数,out ref将失效,不等待同步 /// </summary> /// <param name="Tag"></param> /// <param name="methodName"></param> /// <param name="argTypeList"></param> /// <param name="arglist"></param> public void CallMethod(string Tag, string methodName, Type[] argTypeList, List <byte[]> arglist) { RPCCallPack callpack = new RPCCallPack() { Tag = Tag, Method = methodName, Arguments = arglist, NeedReturn = false, }; byte[] data = BufferFormat.FormatFCA(callpack); if (CallBufferOutSend != null) { CallBufferOutSend(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)); } } } }
/// <summary> /// 异步调用,返回值后调用 Callback /// </summary> /// <typeparam name="Mode"></typeparam> /// <typeparam name="Result"></typeparam> /// <param name="action"></param> /// <param name="Callback"></param> public void CallAsyn <Mode, Result>(Expression <Func <Mode, Result> > action, Action <AsynReturn> Callback) { MethodCallExpression body = action.Body as MethodCallExpression; List <RPCArgument> argumentlist = new List <RPCArgument>(); var parameters = body.Method.GetParameters(); for (int i = 0; i < parameters.Length; i++) { dynamic p = Expression.Call(FormatValue.GetMethodInfo(body.Arguments[i].Type), body.Arguments[i]); dynamic x = Expression.Lambda <Func <dynamic> >(p).Compile()(); RPCArgument tmp = new RPCArgument(); tmp.type = body.Arguments[i].Type; tmp.RefType = parameters[i].ParameterType; tmp.Value = Serialization.PackSingleObject(body.Arguments[i].Type, x); argumentlist.Add(tmp); } RPCCallPack call = new RPCCallPack() { Id = MakeID.GetID(), CallTime = DateTime.Now, CallModule = body.Object.Type.Name, Method = body.Method.Name, Arguments = argumentlist, IsNeedReturn = true, }; byte[] data = BufferFormat.FormatFCA(call); if (CallBufferOutSend != null) { CallBufferOutSend(data); } AsynRetrunDiy.AddOrUpdate(call.Id, Callback, (a, b) => Callback); }
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)); } }
private void Call(RPCCallPack rpcPack, RPCUserInfo e) { try { object returnValue; CallContext.SetData("Current", e); string msg; if (e.RPC_Call.RunModule(rpcPack, out msg, out returnValue)) { ZYClient_Result_Return var = new ZYClient_Result_Return() { Id = rpcPack.Id, CallTime = rpcPack.CallTime, Arguments = rpcPack.Arguments }; if (returnValue != null) { var.Return = Serialization.PackSingleObject(returnValue.GetType(), returnValue); } e.BeginSendData(BufferFormat.FormatFCA(var)); } } catch (Exception er) { if (MsgOut != null) { if (e.Asyn.RemoteEndPoint != null) { MsgOut(e.Asyn.RemoteEndPoint.ToString() + "::" + rpcPack.CallModule + "->" + rpcPack.Method + "\r\n" + er.ToString(), MsgOutType.Err); } else { MsgOut(rpcPack.CallModule + "->" + rpcPack.Method + "\r\n" + er.ToString(), MsgOutType.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)); } } } }
private void Call(RPCCallPack rpcPack, RPCUserInfo e) { try { object returnValue; CallContext.SetData("Current", e); if (e.RPC_Call.RunModule(rpcPack, rpcPack.NeedReturn, out returnValue)) { if (rpcPack.NeedReturn) { Result_Have_Return var = new Result_Have_Return() { Id = rpcPack.Id, Arguments = rpcPack.Arguments, }; if (returnValue != null) { var.Return = Serialization.PackSingleObject(returnValue.GetType(), returnValue); } e.SendData(BufferFormat.FormatFCA(var)); } } } catch (Exception er) { if (e.Asyn.RemoteEndPoint != null) { LogAction.Err(e.Asyn.RemoteEndPoint.ToString() + "::" + rpcPack.Tag + "->" + rpcPack.Method + "\r\n" + er.ToString()); } else { LogAction.Err(rpcPack.Tag + "->" + rpcPack.Method + "\r\n" + er.ToString()); } } }
private void panel1_MouseMove(object sender, MouseEventArgs e) { if (IsDown) { gs.FillEllipse(Br, e.X, e.Y, 2, 2); gs.Flush(); gs.Save(); gs2.FillEllipse(Br, e.X, e.Y, 2, 2); gs2.Flush(); gs2.Save(); DrawPoint tmp = new DrawPoint() { X = e.X, Y = e.Y, Color = Br.Color.ToArgb() }; Client.SendDataToALL(BufferFormat.FormatFCA(tmp)); } }
private void Form1_Load(object sender, EventArgs e) { map = new Bitmap(this.panel1.Width, this.panel1.Height); gs = Graphics.FromImage(map); gs2 = this.panel1.CreateGraphics(); Br = new SolidBrush(Color.FromArgb(new Random().Next())); Client = new ZYNetClient(); if (Client.Connect(ZYSocket.share.RConfig.ReadString("host"), ZYSocket.share.RConfig.ReadInt("ServicePort"))) { LogOn tmp = new LogOn(Client); tmp.ShowDialog(); if (tmp.IsLogOn) { Client.ServerDisconnect += Client_ServerDisconnect; Client.DataInput += Client_DataInput; UserCount count = new UserCount(); Client.SendDataToServer(BufferFormat.FormatFCA(count)); this.Text = tmp.Names; } else { this.Close(); } } else { MessageBox.Show("无法连接服务器"); this.Close(); } }
public void BinaryInput(byte[] data) { ReadBytes read = new ReadBytes(data); int lengt; int cmd; if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd)) { switch (cmd) { case 1001001: { ZYClient_Result_Return val; if (read.ReadObject <ZYClient_Result_Return>(out val)) { RPC_Call.SetReturnValue(val); } } break; case 1001000: { RPCCallPack tmp; if (read.ReadObject <RPCCallPack>(out tmp)) { System.Threading.Tasks.Task.Factory.StartNew((pack) => { CallContext.SetData("Current", this); string msg; object returnValue; if (RPC_Call.RunModule(pack as RPCCallPack, out msg, out returnValue)) { ZYClient_Result_Return var = new ZYClient_Result_Return() { Id = tmp.Id, CallTime = tmp.CallTime, Arguments = tmp.Arguments }; if (returnValue != null) { var.Return = Serialization.PackSingleObject(returnValue.GetType(), returnValue); } Client.BeginSendData(BufferFormat.FormatFCA(var)); } else { if (MsgOut != null) { MsgOut(tmp.CallModule + "->" + tmp.Method + " Call Failure"); } } }, tmp, CancellationToken.None, TaskCreationOptions.None, OrderSchedulerRead).ContinueWith(task => { try { task.Wait(); } catch (Exception er) { if (MsgOut != null) { MsgOut(er.ToString()); } } }); } } break; default: { if (DataOn != null) { DataOn(cmd, read); } } break; } } }
private static void Server_UserDataInput(ZYSocket.ZYNet.ZYNetSession session, byte[] data) { ReadBytes read = new ReadBytes(data); int lengt; int cmd; if (read.ReadInt32(out lengt) && read.ReadInt32(out cmd) && read.Length == lengt) { switch (cmd) { case 1000: { LogOn logon; if (read.ReadObject <LogOn>(out logon)) { UserInfo tmp = new UserInfo() { Name = logon.UserName, Session = session }; session.UserToken = tmp; UserList.Add(tmp); logon.Success = true; Server.SendDataToClient(session, BufferFormat.FormatFCA(logon)); Console.WriteLine(tmp.Name + "登入"); } } break; case 2000: { if (session.UserToken == null) { return; } DrawPoint tmp; if (read.ReadObject <DrawPoint>(out tmp)) { if (Map == null) { Map = new Bitmap(848, 609); gs = Graphics.FromImage(Map); } Brush br = new SolidBrush(Color.FromArgb(tmp.Color)); gs.FillEllipse(br, tmp.X, tmp.Y, 2, 2); gs.Flush(); gs.Save(); } } break; case 3000: { if (session.UserToken == null) { return; } ClecrColor tmp; if (read.ReadObject <ClecrColor>(out tmp)) { gs.Clear(Color.FromArgb(tmp.Color)); gs.Flush(); gs.Save(); } } break; case 4000: { if (session.UserToken == null) { return; } SaveImg tmp; if (read.ReadObject <SaveImg>(out tmp)) { string path = (new System.IO.FileInfo(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)).Directory.FullName + "\\SAVE\\"; if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } Map.Save(path + tmp.FileName); } } break; } } }