/// <summary> /// 组包发送 /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="count"></param> public bool SendCommand() { //OutDataParser.GetProtocolText()结合使用 下 OutDataParser.Clear(); OutDataParser.AddResponse(); OutDataParser.AddValue(ProtocolKeys.UserName, "admin"); OutDataParser.AddValue(ProtocolKeys.Password, HelpCommonLib.ComminClass.MD5Encrypt("admin", 16)); string commandText = OutDataParser.GetProtocolText(); byte[] bufferUTF8 = Encoding.UTF8.GetBytes(commandText); int totalLength = bufferUTF8.Length; //获取总大小 UserToken.SendBuffer.StartPacket(); UserToken.SendBuffer.m_dynamicBufferManager.WriteInt(totalLength); //写入总大小 UserToken.SendBuffer.m_dynamicBufferManager.WriteBuffer(bufferUTF8); //写入命令内容 UserToken.SendBuffer.EndPacket(); int packetOffset = 0; int packetCount = 0; if (UserToken.SendBuffer.GetFirstPacket(ref packetOffset, ref packetCount)) { m_tcpClient.Client.Send(UserToken.SendBuffer.m_dynamicBufferManager.Buffer, 0, UserToken.SendBuffer.m_dynamicBufferManager.DataCount, SocketFlags.None); //使用阻塞模式,Socket会一次发送完所有数据后才返回 while (UserToken.SendBuffer.GetFirstPacket(ref packetOffset, ref packetCount)) { UserToken.SendBuffer.ClearFirstPacket(); m_tcpClient.Client.Send(UserToken.SendBuffer.m_dynamicBufferManager.Buffer, 0, UserToken.SendBuffer.m_dynamicBufferManager.DataCount, SocketFlags.None); //使用阻塞模式,Socket会一次发送完所有数据后才返回 } } return(true); }
//登录 public bool DoLogin() { socketUserToken.ActiveDateTime = DateTime.Now; string userName = ""; string password = ""; if (InDataParser.GetValue(ProtocolKeys.UserName, ref userName) & InDataParser.GetValue(ProtocolKeys.Password, ref password)) { if (password.Equals(HelpCommonLib.ComminClass.MD5Encrypt("admin", 16), StringComparison.CurrentCultureIgnoreCase)) { socketUserToken.UserName = "******"; if (password.Length > 4)//物联网时,区分是设备还是用户 密码大于4的是用户 { socketUserToken.isDevice = false; SocketUserSearchHorse(); DelegateState.ServerStateInfo(socketUserToken.ConnectSocket.RemoteEndPoint.ToString() + "用户登录成功"); DelegateState.AddTCPuserStateInfo(socketUserToken); } else { socketUserToken.isDevice = true; m_asyncSocketServer.AsyncSocketDeviceList.Add(socketUserToken); DelegateState.ServerStateInfo(socketUserToken.ConnectSocket.RemoteEndPoint.ToString() + "设备连接成功"); DelegateState.AddTCPdeviceStateInfo(socketUserToken); } socketUserToken.LoginFlag = true; socketUserToken.ReceiveBuffer.Clear(); socketUserToken.SendBuffer.ClearPacket(); OutDataParser.Clear(); OutDataParser.AddResponse(); OutDataParser.AddCommand(ProtocolCodes.Success.ToString());//添加返回信息 插入-1 return(true); } socketUserToken.LoginFlag = false; return(false); } socketUserToken.LoginFlag = false; return(false); }