protected override void OnRecieveData(ServerHostRecieveEventArgs e, string command, string data) { base.OnRecieveData(e, command, data); switch (command) { // 设置名称 case "$": // 判断名称是否存在 if (base.SsrHost.StorageEntity.ContainsKey(data)) { // 修改存储 base.SsrHost.SendSuccess(e, base.SsrHost.StorageEntity[data]); } else { // 添加存储 base.SsrHost.SendSuccess(e); } // 设置为空业务 base.SsrHost.SetHostNone(); break; default: if (Server.IsDebug) { Console.WriteLine($"> 未知命令类型:{command}"); } base.SsrHost.SendFail(e, "Unknow Command"); break; } }
protected override void OnRecieveCommand(ServerHostRecieveEventArgs e, string command, string info) { base.OnRecieveCommand(e, command, info); switch (command) { // 设置名称 case "$": int len = int.Parse(info); if (len <= 0) { if (Server.IsDebug) { Console.WriteLine($"> 名称长度为0"); } base.SsrHost.SendFail(e, "Unknow Name"); } else { // 设置读取模式,读取Sid base.Command = command; e.Entity.SetDataMode(len); } break; default: if (Server.IsDebug) { Console.WriteLine($"> 未知命令类型:{command} 完整语句:{e.Content}"); } base.SsrHost.SendFail(e, "Unknow Command"); break; } }
protected override void OnRecieveCommand(ServerHostRecieveEventArgs e, string command, string info) { base.OnRecieveCommand(e, command, info); switch (command) { // 指定连接密码 case "$": int len = int.Parse(info); if (len <= 0) { // 发送错误信息 base.SsrHost.SendFail(e, "Invalid Pwd"); System.Threading.Thread.Sleep(10); e.Entity.Close(); return; } // 设置读取模式,读取密码 base.Command = command; e.Entity.SetDataMode(len); break; default: if (Server.IsDebug) { Console.WriteLine($"> 未知命令类型:{command} 完整语句:{e.Content}"); } string errMsg = "Unknow Command"; e.Entity.Send($"-{errMsg.Length}\r\n{errMsg}"); break; } }
public void OnRecieve(HostRecieveEventArgs e) { // 此demo进行了一次简单的定长数据获取示例 if (_data) { // 数据模式 // 输出内容 Console.WriteLine($"-> 接受数据 -> {e.Content}"); // 测试协议,原封内容发回客户端 ServerHostRecieveEventArgs args = (ServerHostRecieveEventArgs)e; args.Entity.Send($"${e.Content.Length}\r\n{e.Content}"); } else { //命令模式 // 输出内容 Console.WriteLine($"-> 接受定义命令 -> {e.Content}"); //此处以$开头定义数据长度 if (e.Content.StartsWith("$")) { _data = true; int len = int.Parse(e.Content.Substring(1)); // 输出内容 Console.WriteLine($"-> 定义数据长度:{len}"); ServerHostRecieveEventArgs args = (ServerHostRecieveEventArgs)e; args.Entity.SetDataMode(len); } } }
protected override void OnRecieveData(ServerHostRecieveEventArgs e, string command, string data) { base.OnRecieveData(e, command, data); switch (command) { case "$": // 检测密码 if (data == Server.Password) { // 设置登录状态 base.SsrHost.IsLogin = true; base.SsrHost.SendSuccess(e); // 重置业务 base.SsrHost.SetHostNone(); } else { // 发送错误信息 base.SsrHost.SendFail(e, "Invalid Pwd"); System.Threading.Thread.Sleep(10); e.Entity.Close(); } break; default: if (Server.IsDebug) { Console.WriteLine($"> 未知命令类型:{command} 完整语句:{e.Content}"); } string errMsg = "Unknow Command"; e.Entity.Send($"-{errMsg.Length}\r\n{errMsg}"); break; } }
protected override void OnRecieveData(ServerHostRecieveEventArgs e, string command, string data) { base.OnRecieveData(e, command, data); switch (command) { // 设置名称 case "$": _name = data; break; // 设置值 case "&": // 判断名称是否定义 if (_name == null) { if (Server.IsDebug) { System.Console.WriteLine($"> 名称未定义"); } base.SsrHost.SendFail(e, "None Name"); // 设置为空业务 base.SsrHost.SetHostNone(); return; } // 判断名称是否存 if (base.SsrHost.StorageEntity.ContainsKey(_name)) { // 修改存储 base.SsrHost.StorageEntity[_name] = data; } else { // 添加存储 base.SsrHost.StorageEntity.Add(_name, data); } // 更新有效时间 base.SsrHost.StorageEntity.UpdateValidTime(); // 向客户端发送结果 base.SsrHost.SendSuccess(e); // 设置为空业务 base.SsrHost.SetHostNone(); break; default: if (Server.IsDebug) { System.Console.WriteLine($"> 未知命令类型:{command}"); } base.SsrHost.SendFail(e, "Unknow Command"); break; } }
protected override void OnRecieveData(ServerHostRecieveEventArgs e, string command, string data) { base.OnRecieveData(e, command, data); switch (command) { case "@": // 清理存储 Server.Storages.CleanUp(); // 判断交互标识是否存在 if (Server.Storages.ContainsKey(data)) { // 设置交互存储对象 var entity = Server.Storages[data]; base.SsrHost.StorageEntity = entity; // 更新存储对象操作时间 entity.UpdateValidTime(); if (Server.IsDebug) { System.Console.WriteLine($"[{Time.GetTimeString()}] -+- 当前存储对象失效时间 {base.SsrHost.StorageEntity.ValidTime}"); } // 返回信息 base.SsrHost.SendSuccess(e, entity.Sid); // 设置为空业务 base.SsrHost.SetHostNone(); } else { // 发送错误信息 base.SsrHost.SendFail(e, "Invalid Sid"); // 设置为空业务 base.SsrHost.SetHostNone(); } break; default: if (Server.IsDebug) { System.Console.WriteLine($"> 未知命令类型:{command} 完整语句:{e.Content}"); } string errMsg = "Unknow Command"; e.Entity.Send($"-{errMsg.Length}\r\n{errMsg}"); break; } }
public void OnRecieve(HostRecieveEventArgs e) { // 此demo进行了一次简单的定长数据获取示例 ServerHostRecieveEventArgs args = (ServerHostRecieveEventArgs)e; if (args.Entity.DataMode) { // 数据模式 OnRecieveData(args, this.Command, e.Content); } else { // 命令模式 OnRecieveCommand(args, e.Content.Substring(0, 1), e.Content.Substring(1)); } }
/// <summary> /// 发送失败信息 /// </summary> /// <param name="e"></param> /// <param name="data"></param> public void SendFail(ServerHostRecieveEventArgs e, string data = null) { if (data == null) { if (Server.IsDebug) { Console.WriteLine($"[{Time.GetTimeString()}] <-- [-]"); } e.Entity.Send($"-0\r\n"); } else { if (Server.IsDebug) { Console.WriteLine($"[{Time.GetTimeString()}] <-- [-] {data}"); } byte[] bs = System.Text.Encoding.UTF8.GetBytes(data); e.Entity.Sendln($"-{bs.Length}"); e.Entity.Send(bs); } }
protected override void OnRecieveCommand(ServerHostRecieveEventArgs e, string command, string info) { base.OnRecieveCommand(e, command, info); switch (command) { // 指定交互标识 case "@": int len = int.Parse(info); if (len <= 0) { // 申请一个新的Sid var entity = Server.Storages.GetNew(); base.SsrHost.StorageEntity = entity; // 返回信息 base.SsrHost.SendSuccess(e, entity.Sid); // 设置为空业务 base.SsrHost.SetHostNone(); } else { // 设置读取模式,读取Sid base.Command = command; e.Entity.SetDataMode(len); } break; default: if (Server.IsDebug) { System.Console.WriteLine($"> 未知命令类型:{command} 完整语句:{e.Content}"); } string errMsg = "Unknow Command"; e.Entity.Send($"-{errMsg.Length}\r\n{errMsg}"); break; } }
/// <summary> /// 获取数据 /// </summary> /// <param name="e"></param> /// <param name="command"></param> /// <param name="info"></param> protected virtual void OnRecieveData(ServerHostRecieveEventArgs e, string command, string data) { }
/// <summary> /// 获取命令 /// </summary> /// <param name="e"></param> /// <param name="command"></param> /// <param name="data"></param> protected virtual void OnRecieveCommand(ServerHostRecieveEventArgs e, string command, string info) { }
protected override void OnRecieveCommand(ServerHostRecieveEventArgs e, string command, string info) { base.OnRecieveCommand(e, command, info); switch (command) { // 设置名称 case "$": int len = int.Parse(info); if (len <= 0) { if (Server.IsDebug) { System.Console.WriteLine($"> 名称长度为0"); } base.SsrHost.SendFail(e, "Unknow Name"); } else { // 设置读取模式,读取Sid base.Command = command; e.Entity.SetDataMode(len); } break; // 设置值 case "&": len = int.Parse(info); if (len <= 0) { // 判断名称是否定义 if (_name == null) { if (Server.IsDebug) { System.Console.WriteLine($"> 名称未定义"); } base.SsrHost.SendFail(e, "None Name"); // 设置为空业务 base.SsrHost.SetHostNone(); return; } // 判断名称是否存 if (base.SsrHost.StorageEntity.ContainsKey(_name)) { // 修改存储 base.SsrHost.StorageEntity[_name] = ""; } else { // 添加存储 base.SsrHost.StorageEntity.Add(_name, ""); } // 更新有效时间 base.SsrHost.StorageEntity.UpdateValidTime(); // 向客户端发送结果 base.SsrHost.SendSuccess(e); // 设置为空业务 base.SsrHost.SetHostNone(); } else { // 设置读取模式,读取Sid base.Command = command; e.Entity.SetDataMode(len); } break; default: if (Server.IsDebug) { System.Console.WriteLine($"> 未知命令类型:{command} 完整语句:{e.Content}"); } base.SsrHost.SendFail(e, "Unknow Command"); break; } }