public void AddProt(List <WeaveServerPort> listServerPort) { foreach (WeaveServerPort sp in listServerPort) { IWeaveTcpBase p2psev = null; WeaveTcpToken tt = new WeaveTcpToken(); if (sp.PortType == WeavePortTypeEnum.Web) { p2psev = new WeaveWebServer(); if (sp.Certificate != null) { ((WeaveWebServer)p2psev).Certificate = sp.Certificate; } } else if (sp.PortType == WeavePortTypeEnum.Json) { p2psev = new WeaveP2Server("127.0.0.1"); } else if (sp.PortType == WeavePortTypeEnum.Bytes) { p2psev = new WeaveP2Server(WeaveDataTypeEnum.Bytes); if (sp.BytesDataparsing == null) { throw new Exception("BytesDataparsing对象不能是空,请完成对应接口的实现。"); } tt.BytesDataparsing = sp.BytesDataparsing; p2psev.weaveReceiveBitEvent += P2psev_receiveeventbit; } //else if (sp.PortType == WeavePortTypeEnum.jsonudp) //{ // p2psev = new WeaveUDPServer(WeaveDataTypeEnum.Json); //} //else if (sp.PortType == WeavePortTypeEnum.Json) //{ // p2psev = new WeaveUDPServer("127.0.0.1"); //} else if (sp.PortType == WeavePortTypeEnum.Http) { p2psev = new HttpServer(sp.Port); } p2psev.waveReceiveEvent += P2ServerReceiveHander; p2psev.weaveUpdateSocketListEvent += P2ServerUpdateSocketHander; p2psev.weaveDeleteSocketListEvent += P2ServerDeleteSocketHander; // p2psev.NATthroughevent += tcp_NATthroughevent;//p2p事件,不需要使用 p2psev.Start(Convert.ToInt32(sp.Port));//myI.Parameter[4]是端口号 tt.PortType = sp.PortType; tt.P2Server = p2psev; tt.IsToken = sp.IsToken; tt.WPTE = sp.PortType; TcpTokenList.Add(tt); P2ServerList.Add(p2psev); } }
void P2ServerUpdateSocketHander(System.Net.Sockets.Socket soc) { CallWeaveServerUpdateSocketCallBackEvent(soc); CallWeaveTCPCommandClassUpdateSocketEvent(soc); WeaveTcpToken token = TcpToken; { if (token.IsToken) { //生成一个token,后缀带随机数 string Token = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(1000, 9999);// EncryptDES(clientipe.Address.ToString() + "|" + DateTime.Now.ToString(), "lllssscc"); if (token.P2Server.Port == ((System.Net.IPEndPoint)soc.LocalEndPoint).Port) { //向客户端发送生成的token bool sendok = false; if (token.PortType == WeavePortTypeEnum.Bytes) { sendok = token.P2Server.Send(soc, 0xff, token.BytesDataparsing.Get_ByteBystring("token|" + Token + "")); } else { sendok = token.P2Server.Send(soc, 0xff, "token|" + Token + ""); } #region if(sendok) 如果发送token成功 if (sendok) { WeaveOnLine onLine = CreatWeaveOnLine(Token, soc); AddWeaveOnLine(onLine); ForeachWeaveExecuteRuncommandMethod(onLine); return; } #endregion } } else { WeaveOnLine online = CreatWeaveOnLine(soc); weaveOnline.Add(online); } } }