public void CreateFile(Socket soc, WeaveSession _0x01) { filestream fstream = _0x01.GetRoot <filestream>(); if (!Directory.Exists(_0x01.Token)) { Directory.CreateDirectory(_0x01.Token); } if (!File.Exists(_0x01.Token + "/" + fstream.Filename + ".tmp")) { // File.Create(_0x01.Token + "/" + fstream.Filename, (int)fstream.Maxlen).Close(); lock (this) { FileStream fss = File.Create(_0x01.Token + "/" + fstream.Filename + ".tmp"); long offset = fss.Seek(fstream.Maxlen - 1, SeekOrigin.Begin); fss.WriteByte(new byte()); fss.Close(); long count = fstream.Maxlen % (1024 * 4) == 0 ? fstream.Maxlen / (1024 * 4) : (fstream.Maxlen / (1024 * 4)) + 1; filestate fs = new filestate(); fs.Filename = fstream.Filename; fs.State = new byte[count]; fs.Path = _0x01.Token; fs.Dt = DateTime.Now; fs.Soc = soc; listfilestate.Add(fs); } } }
private void P2p_receiveServerEvent(byte command, string text) { try { WeaveSession _0x01 = Newtonsoft.Json.JsonConvert.DeserializeObject <WeaveSession>(text); try { int count = ConnObjlist.Count; ConnObj[] coobjs = new ConnObj[count]; ConnObjlist.CopyTo(0, coobjs, 0, count); foreach (ConnObj coob in coobjs) { if (coob != null) { if (coob.Token == _0x01.Token) { coob.Soc.Send(_0x01.GetRoot <byte[]>()); return; } } } } catch (Exception ex) { EventMylog("转发", ex.Message); } } catch { } }
public void login(Socket soc, WeaveSession _0x01) { WeaveOnLine[] onlieuser = GetOnline(); User[] listsoctemp = new User[listsoc.Count]; listsoc.CopyTo(0, listsoctemp, 0, listsoctemp.Length); //为什么写这两句,是因为多线程中,添加和删除集合的操作,都会对其他线程有影响,所以先 //拷贝一份副本 foreach (User u in listsoctemp) { if (u.token == _0x01.Token) { return; } } String Token = _0x01.Token;//这个就是上线人员的Token了 User du = new User(); du.soc = soc; du.token = Token; du.name = _0x01.Root; listsoc.Add(du); //发送回执 SendRoot <String>(soc, 0x31, "login", "ok", 0, _0x01.Token); SendRoot <List <User> >(soc, 0x31, "userlist", listsoc, 0, _0x01.Token); foreach (User u in listsoctemp) { try { SendRoot <List <User> >(u.soc, 0x31, "userlist", listsoc, 0, u.token); } catch { } } }
public void pushdata(Socket soc, WeaveSession _0x01) { filestream fstre = _0x01.GetRoot <filestream>(); byte[] byteData = new byte[1024 * 4]; //从文件中读取数据写入到数组中(数组对象,从第几个开始读,读多少个) //返回读取的文件内容真实字节数 FileStream fstream = File.OpenRead(fstre.Filename); fstream.Seek(fstre.Index * 1024 * 4, SeekOrigin.Begin); int length = fstream.Read(byteData, (int)0, byteData.Length); //如果字节数大于0,则转码 if (length > 0) { filestream fs = new filestream(); fs.Filename = fstre.Filename; fs.Index = fstre.Index; fs.Len = length; fs.Stream = byteData; fs.Filetype = ""; pclient.Tokan = "fileupdata_gedian"; pclient.SendRoot <filestream>(0x31, "SaveFile", fs, 0); System.Threading.Thread.Sleep(2); } }
private void Xmhelper_errorMessageEvent(Socket soc, WeaveSession _0x01, string message) { if (ErrorMge != null) { ErrorMge(0, message); } }
public void setvalue(Socket soc, WeaveSession _0x01) { try { this.GlobalQueueTable["iotvalue"] = _0x01.Root; } catch { } }
private void OnWeaveReceiveSocketMessage(byte command, string data, WeaveOnLine _socket) { WeaveSession ws = Newtonsoft.Json.JsonConvert.DeserializeObject <WeaveSession>(data); SetServerReceiveText("Socket发来数据--触发了一次(OnWeaveReceiveSocketMessage)" + Environment.NewLine); SetServerReceiveText("收到的数据为: " + ws.Root + Environment.NewLine); }
[InstallFun("forever")]//这是一个接收的方法,注册到了通讯类中 public void login(Socket soc, WeaveSession _0x01) { //在这里你可以用 users u = _0x01.GetRoot <users>(); //去验证登录是否正确、 //并且用_0x01.Token当成SESSION进行验证。 SendRoot <bool>(soc, 0x01, "Islogin", Convert.ToBoolean(new Random().Next(0, 1)), 0, _0x01.Token); }
private static void Client_receiveServerEvent(byte command, string text) { WeaveSession bm = Newtonsoft.Json.JsonConvert.DeserializeObject <WeaveSession>(text);//我的传输类型是这样的类 if (bm.Request == "say") { Console.WriteLine(bm.Root); } }
public bool SendRoot <T>(byte command, String Request, T Root, int Querycount) { WeaveSession b = new WeaveSession(); b.Request = Request; b.Token = this.Tokan; b.SetRoot <T>(Root); b.Querycount = Querycount; return(send(command, b.Getjson())); }
public bool SendParameter <T>(byte command, String Request, T Parameter, int Querycount) { WeaveSession b = new WeaveSession(); b.Request = Request; b.Token = this.Tokan; b.SetParameter <T>(Parameter); b.Querycount = Querycount; return(send(command, b.Getjson())); }
private void OnWeaveServerReceiveOnLineUnityPlayerMessageEvent(byte command, string data, UnityPlayerOnClient gamer) { // 登陆用户发送过来的数据 SetServerReceiveText("Unity玩家登陆事件--触发了一次(OnWeaveServerReceiveOnLineUnityPlayerMessageEvent)" + Environment.NewLine); WeaveSession ws = Newtonsoft.Json.JsonConvert.DeserializeObject <WeaveSession>(data); SetServerReceiveText("收到【" + gamer.UserName + "】发来的数据: " + ws.Root + Environment.NewLine); }
[InstallFun("forever")]//这是一个接收的方法,注册到了通讯类中 public void Islogin(Socket soc, WeaveSession _0x01) { if (_0x01.GetRoot <bool>()) { MessageBox.Show("登录成功"); } else { MessageBox.Show("登录失败"); } }
public bool SendParameter <T>(byte command, string Request, T Parameter, int Querycount) { WeaveSession b = new WeaveSession { Request = Request, Token = Tokan }; b.SetParameter(Parameter); b.Querycount = Querycount; return(Send(command, b.Getjson())); }
public bool SendRoot <T>(byte command, string Request, T Root, int Querycount) { WeaveSession b = new WeaveSession { Request = Request, Token = Tokan }; b.SetRoot(Root); b.Querycount = Querycount; return(Send(command, b.Getjson())); }
public void getequipmentData(Socket soc, WeaveSession _0x01) { tequipment tq = _0x01.GetRoot <tequipment>(); if (listtequipment[tq.SnID] == null) { listtequipment.Add(tq.SnID, tq); } else { listtequipment[tq.SnID] = tq; } }
/// <summary> /// 这里写接收到服务器发送的消息转发给客户端 /// </summary> /// <param name="command"></param> /// <param name="text"></param> protected void V_receiveServerEvent(byte command, string text) { try { if (text == "") { return; } WeaveSession _0x01 = null; try { _0x01 = Newtonsoft.Json.JsonConvert.DeserializeObject <WeaveSession>(text); } catch { return; } int temp = 0; String ip = ""; int port = 0; try { ip = _0x01.Token.Split(':')[0]; port = Convert.ToInt32(_0x01.Token.Split(':')[1]); _0x01.Querycount = temp; } catch { EventMylog("转发", "获取编号失败。" + _0x01.Token); return; } ConnObj cobj = GateHelper.GetConnItemlist(ConnItemlist, ip, port, Pipeline); if (cobj != null) { int error = 0; lb1122: if (!p2psev.Send(cobj.Soc, command, text)) { error += 1; EventMylog("转发" + error, "ConnObjlist:发送失败:" + text); if (error < 3) { goto lb1122; } } } else { EventMylog("转发", "ConnObjlist:" + temp + "是空的"); } return; } catch (Exception ex) { EventMylog("转发", ex.Message + "112223333333333356464122313" + text + "000000"); } }
protected void ReloadFliesway(object obj) { try { foreach (WayItem ci in WayItemS) { ci.Client.stop(); } WayItemS.Clear(); XmlDocument xml = new XmlDocument(); xml.Load("nodeway.xml"); foreach (XmlNode xn in xml.FirstChild.ChildNodes) { WayItem ci = new WayItem(); ci.Ip = xn.Attributes["ip"].Value; ci.Port = Convert.ToInt32(xn.Attributes["port"].Value); ci.Token = (xn.Attributes["token"].Value); ci.Client = new P2Pclient(false); ci.Client.receiveServerEvent += Client_receiveServerEvent; ci.Client.timeoutobjevent += Client_timeoutobjevent; ci.Client.ErrorMge += Client_ErrorMge; if (ci.Client.start(ci.Ip, ci.Port, false)) { WeaveSession oxff = new WeaveSession(); oxff.Request = "token"; oxff.Root = ci.Token; ci.Client.send(0xff, oxff.Getjson()); WayItemS.Add(ci); } else { if (EventMylog != null) { EventMylog("从网关连接失败", "从网关:" + ci.Ip + ":节点连接失败,抛弃此节点"); } } } System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(getwaynum)); } catch (Exception ex) { if (EventMylog != null) { EventMylog("加载异常", ex.Message); } } }
public void command(Socket soc, WeaveSession _0x01) { WeaveOnLine [] ol = this.GetOnline(); foreach (WeaveOnLine o in ol) { try { if (o != null && o.Name == "设备") { SendRoot <string>(o.Socket, 0x02, "command", _0x01.Root, 0, o.Token); } } catch { } } }
[InstallFun("forever")]//这是一个接收的方法,注册到了通讯类中 public void getdata(Socket soc, WeaveSession _0x01) { List <ViewData> listViewData = _0x01.GetRoot <List <ViewData> >(); listView1.Invoke(new Action(delegate(){ if (!listView1.InvokeRequired) { foreach (ViewData vd in listViewData) { ListViewItem lvi = new ListViewItem(vd.Name); lvi.SubItems.Add(vd.School); lvi.SubItems.Add(vd.Age); listView1.Items.Add(lvi); } } })); }
public void say(Socket soc, WeaveSession _0x01) { User[] listsoctemp = new User[listsoc.Count]; listsoc.CopyTo(0, listsoctemp, 0, listsoctemp.Length); say s = _0x01.GetRoot <say>(); s.time = DateTime.Now; //为什么写这两句,是因为多线程中,添加和删除集合的操作,都会对其他线程有影响,所以先 //拷贝一份副本 foreach (User du in listsoctemp) { if (du != null) { try { SendRoot <say>(du.soc, 0x31, "say", s, 0, du.token); } catch { } } } }
[InstallFun("forever")]//这是一个接收的方法,注册到了通讯类中 public void getdata(Socket soc, WeaveSession _0x01) { //这里可以验证_0x01.Token是否是登录过的。 List <ViewData> listViewData = new List <ViewData>(); ViewData vdata = new ViewData(); vdata.Name = "张三"; vdata.School = "小学"; vdata.Age = "18"; listViewData.Add(vdata); vdata = new ViewData(); vdata.Name = "李四"; vdata.School = "中学"; vdata.Age = "28"; listViewData.Add(vdata); SendRoot <List <ViewData> >(soc, 0x01, "getdata", listViewData, 0, _0x01.Token); }
private void OnReceiveMessageEvent(byte command, string text) { // throw new NotImplementedException(); Debug.Log("收到了新数据"); //throw new NotImplementedException(); server_msg = "指令:" + command + ".内容:" + text; Debug.Log("原始数据是:" + server_msg); try { WeaveSession ws = Newtonsoft.Json.JsonConvert.DeserializeObject <WeaveSession>(text); Debug.Log("接受到的WeaveSession数据是:" + ws.Request + " " + ws.Root); } catch { Debug.Log("Json转换对象出错了"); } // receiveMessage = "指令:" + command + ".内容:" + text; }
/// <summary> /// 将传输的BYTE字节内容转为基础类 /// </summary> /// <param name="data"></param> /// <returns></returns> public WeaveSession GetBaseModel(byte[] data) { //data 的内容为,协议中的内容段,不明白的查看 客户端协议的说明,内容段是自己定义的内容 WeaveSession bm = new WeaveSession(); byte[] bs = new byte[2]; Array.Copy(data, 0, bs, 0, bs.Length); int req = ConvertToInt(bs); if (req == 1) { bm.Request = "getdata"; } //bm.Request 注意这个属性必须要赋值,赋值的内容和你后端的执行处理的方法一致,代表你需要那个方法处理他。 byte[] b = new byte[data.Length - 2]; Array.Copy(data, 2, b, 0, b.Length); bm.SetRoot <byte[]>(b); //可以用SetRoot 方法把整段内容发送给后端方法处理 bm.Token = "这里不能是空的,如果没有,就自己随便加一个内容"; return(bm); }
private void P2psev_receiveeventbit(byte command, byte[] data, System.Net.Sockets.Socket soc) { try { // JSON.parse<_baseModel>(data);// WeaveSession _0x01 = null; try { foreach (WeaveTcpToken itp in TcpTokenList) { if (itp.PortType == WeavePortTypeEnum.Bytes) { _0x01 = itp.BytesDataparsing.GetBaseModel(data);//二进制转_baseModel if (command == 0xff) { WeaveExcCmdNoCheckCmdName(command, _0x01.Getjson(), soc); } else { if (itp.BytesDataparsing.Socketvalidation(_0x01)) //验证_baseModel { WeaveExcCmd(command, _0x01.Getjson(), soc); } } } } } catch { WeaveLogEvent("JSON解析错误:", "" + data); return; } } catch (Exception ex) { if (WeaveLogEvent != null) { WeaveLogEvent("p2psev_receiveevent----", ex.Message); } } }
public void GetUserScore(Socket soc, WeaveSession wsession) { MyTcpCommandLibrary.CommandUseTempModel.LoginTempModel loginModel = wsession.GetRoot <LoginTempModel>(); //执行查找数据的操作...... MyTcpCommandLibrary.CommandUseTempModel.GameScoreTempModel gsModel = GetUserGameScore(loginModel.userName); if (gsModel != null) { // 向客户端发送 查找积分成功的信息 SendRoot <GameScoreTempModel>(soc, (byte)CommandEnum.ServerSendGetGameScoreResult, "ServerSendGameScore", gsModel, 0, wsession.Token); } else { // 向客户端发送 查找积分失败的信息 //SendRoot<bool>(soc, (byte)CommandEnum.ServerSendUpdateGameScoreResult, "ServerSendUpdateGameScoreResult", updateSocreResult, 0, wsession.Token); //发送人数给客户端 //参数1,发送给客户端对象,参数2,发送给客户端对应的方法,参数3,人数的实例,参数4,此处无作用,参数5,客户端此次token } }
public void UpdateScore(Socket soc, WeaveSession wsession) { GameScoreTempModel gsModel = wsession.GetRoot <GameScoreTempModel>(); //执行数据更新的操作...... bool updateSocreResult = UpdateUserScore(gsModel.userName, gsModel.score, gsModel.missenemy); if (updateSocreResult) { // 向客户端发送 更新积分成功的信息 SendRoot <bool>(soc, (byte)CommandEnum.ServerSendUpdateGameScoreResult, "ServerSendUpdateGameScoreResult", updateSocreResult, 0, wsession.Token); } else { // 向客户端发送 更新积分失败的信息 SendRoot <bool>(soc, (byte)CommandEnum.ServerSendUpdateGameScoreResult, "ServerSendUpdateGameScoreResult", updateSocreResult, 0, wsession.Token); //发送人数给客户端 //参数1,发送给客户端对象,参数2,发送给客户端对应的方法,参数3,人数的实例,参数4,此处无作用,参数5,客户端此次token } }
protected void Client_receiveServerEvent(byte command, string text) { try { WeaveSession _0x01 = Newtonsoft.Json.JsonConvert.DeserializeObject <WeaveSession>(text); if (_0x01.Request == "setnum") { int count = WayItemS.Count; WayItem[] coobjs = new WayItem[count]; WayItemS.CopyTo(0, coobjs, 0, count); foreach (WayItem wi in coobjs) { if (wi.Token == _0x01.Token) { wi.Num = int.Parse(_0x01.Root); } } } } catch { } }
public void getwaynum(object obj) { while (true) { try { int count = WayItemS.Count; WayItem[] coobjs = new WayItem[count]; WayItemS.CopyTo(0, coobjs, 0, count); foreach (WayItem wi in coobjs) { WeaveSession oxff = new WeaveSession(); oxff.Request = "getnum"; oxff.Root = wi.Token; wi.Client.send(0xff, oxff.Getjson()); } } catch { } System.Threading.Thread.Sleep(2000); } }
public void finish(Socket soc, WeaveSession _0x01) { string filename = _0x01.GetRoot <string>(); try { System.Threading.Thread.Sleep(1000); File.Delete(@filename); if (addtext != null) { addtext(filename + "已上传!-----------"); } gg = false; pclient.stop(); //index++; } catch (Exception ex) { addtext(filename + ".错误信息\n" + ex.ToString()); } }