Advertisement
PlotnikovPhilipp

Untitled

Apr 14th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include <client.hpp>
  2.  
  3. using namespace Qt;
  4.  
  5. /**
  6. * Create the constructor of class Client
  7. */
  8.  
  9. Client::Client(QObject* pointer) : QObject(pointer), sizeOfData(0) {
  10. pTcpSocket = new QTcpSocket(this);
  11. pTcpSocket->connectToHost("77.223.89.11", 2323);
  12. connect(pTcpSocket, SIGNAL(readyRead()), SLOT(slotReadyRead()));
  13. }
  14.  
  15. /**
  16. * Create the slot the reception of data from server
  17. */
  18.  
  19. void Client::slotReadyRead () {
  20. QDataStream in(pTcpSocket);
  21. in.setVersion(QDataStream::Qt_5_3);
  22. in>>sizeOfData;
  23. while(pTcpSocket->bytesAvailable() < sizeOfData) {
  24. pTcpSocket->waitForReadyRead();
  25. }
  26. if (str[0] == "SigningIn") {
  27. str.clear();
  28. in>>number;
  29. in>>str;
  30. if (!str.isEmpty() && str[0] == name) {
  31. accesSigningIn(name, number);
  32. name = "";
  33. number = 0;
  34. }
  35. else {
  36. notAccesSigningIn(name);
  37. name = "";
  38. }
  39. }
  40. else if (str[0] == "SigningUp") {
  41. str.clear();
  42. in>>number;
  43. in>>str;
  44. if (!str.isEmpty() && str[0] == name) {
  45. accesSigningUp(name, number);
  46. name = "";
  47. number = 0;
  48. }
  49. else {
  50. notAccesSigningUp(name);
  51. name = "";
  52. }
  53. }
  54. name = "";
  55. str.clear();
  56. }
  57.  
  58. /**
  59. * Create the slot that send to server messages
  60. */
  61.  
  62. void Client::slotSendToServer(QString argName, QString argPassword) {
  63. name = argName.trimmed();
  64. password = argPassword.trimmed();
  65. if (name.isEmpty() || password.isEmpty()) {
  66. somethingEmpty(name);
  67. return;
  68. }
  69. QByteArray arrBlock;
  70. QDataStream out(&arrBlock, QIODevice::WriteOnly);
  71. out.setVersion(QDataStream::Qt_5_3);
  72. QString info;
  73. str << sender()->objectName();
  74. if (str[0] == "SigningIn") {
  75. info = QString("SELECT \"Name\" FROM \"Authorization\" WHERE \"Name\" = '%1' AND \"password\" = '%2';|SELECT * FROM \"Amount\"").arg(name).arg(password);
  76. }
  77. else {
  78. info = QString("INSERT INTO \"Authorization\"(\"Name\", \"password\") VALUES('%1', '%2');|SELECT \"Name\" FROM \"Authorization\" WHERE \"password\" = '%2';|SELECT * FROM \"Amount\";").arg(name).arg(password);
  79. }
  80. out<<int(0)<<info;
  81. out.device()->seek(0);
  82. out << int(arrBlock.size() - sizeof(int));
  83. pTcpSocket->write(arrBlock);
  84. }
  85.  
  86. /**
  87. * Create the deconstructor
  88. */
  89.  
  90. Client::~Client() {
  91. delete this;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement