Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** \file ServerCatchcopy.cpp
- \brief Define the server of catchcopy
- \author alpha_one_x86
- \version 0002
- \date 2010 */
- #include "ServerCatchcopy.h"
- #include "VariablesCatchcopy.h"
- #include "ExtraSocketCatchcopy.h"
- #include <QFile>
- ServerCatchcopy::ServerCatchcopy()
- {
- name="Default avanced copier";
- autoReply=true;
- idNextClient=0;
- error_string="Unknown error";
- connect(&server, SIGNAL(newConnection()), this, SLOT(newConnection()));
- #ifdef Q_OS_WIN32
- connect(this,SIGNAL(trySetRights()),this,SLOT(setRights()));
- #endif
- }
- ServerCatchcopy::~ServerCatchcopy()
- {
- close();
- }
- bool ServerCatchcopy::isListening()
- {
- return server.isListening();
- }
- void ServerCatchcopy::setName(const QString & name)
- {
- this->name=name;
- }
- QString ServerCatchcopy::getName()
- {
- return name;
- }
- #ifdef Q_OS_WIN32
- int ServerCatchcopy::ChangeFileOwner()
- {
- emit error("Start: ChangeFileOwner");
- HANDLE token;
- wchar_t filename[1024];
- int size=QString("\\\\.\\pipe\\"+pathSocket).toWCharArray(filename);
- DWORD len;
- PSECURITY_DESCRIPTOR security = NULL;
- int retValue = 1;
- PSID sid;
- emit error("ChangeFileOwner: Get the privileges you need");
- // Get the privileges you need
- if (OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token)) {
- if(!SetPrivilege(L"SeTakeOwnershipPrivilege", 1))retValue=0;
- if(!SetPrivilege(L"SeSecurityPrivilege", 1))retValue=0;
- if(!SetPrivilege(L"SeBackupPrivilege", 1))retValue=0;
- if(!SetPrivilege(L"SeRestorePrivilege", 1))retValue=0;
- } else retValue = 0;
- emit error("ChangeFileOwner: retValue: "+QString::number(retValue));
- // Create the security descriptor
- if (retValue) {
- emit error("ChangeFileOwner: Create the security descriptor");
- GetFileSecurity(filename, OWNER_SECURITY_INFORMATION, security, 0, &len);
- security = (PSECURITY_DESCRIPTOR)malloc(len);
- if (!InitializeSecurityDescriptor(security,SECURITY_DESCRIPTOR_REVISION))
- retValue = 0;
- }
- // Get the sid for the username
- if (retValue) {
- emit error("ChangeFileOwner: Get the sid for the username");
- GetLogonSID(token, &sid) ;
- }
- // Set the sid to be the new owner
- if (retValue && !SetSecurityDescriptorOwner(security, sid, 0))
- {
- emit error("ChangeFileOwner: Set the sid to be the new owner: failed");
- retValue = 0;
- }
- else
- emit error("ChangeFileOwner: Set the sid to be the new owner: success");
- // Save the security descriptor
- if (retValue)
- {
- retValue = SetFileSecurity(filename, OWNER_SECURITY_INFORMATION, security);
- emit error("ChangeFileOwner: Save the security descriptor: "+QString::number(retValue));
- #ifdef CATCHCOPY_EXPLORER_PLUGIN_DEBUG
- void* lpBuffer;
- FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- ::GetLastError(),
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPTSTR) &lpBuffer,
- 0,
- NULL );
- MessageBox(NULL,(LPCTSTR)lpBuffer, L"GetUserName Failed", MB_OK);
- LocalFree( lpBuffer );
- #endif // CATCHCOPY_EXPLORER_PLUGIN_DEBUG
- }
- if (security) free(security);
- return retValue;
- }
- BOOL ServerCatchcopy::GetLogonSID (HANDLE hToken, PSID *ppsid)
- {
- BOOL bSuccess = FALSE;
- DWORD dwIndex;
- DWORD dwLength = 0;
- PTOKEN_GROUPS ptg = NULL;
- // Get required buffer size and allocate the TOKEN_GROUPS buffer.
- GetTokenInformation(hToken,TokenGroups,(LPVOID) ptg,0,&dwLength) ;
- ptg = (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(),
- HEAP_ZERO_MEMORY, dwLength);
- // Get the token group information from the access token.
- GetTokenInformation(hToken,TokenGroups,(LPVOID) ptg,dwLength,&dwLength) ;
- // Loop through the groups to find the logon SID.
- for (dwIndex = 0; dwIndex < ptg->GroupCount; dwIndex++)
- {
- if ((ptg->Groups[dwIndex].Attributes & SE_GROUP_LOGON_ID)
- == SE_GROUP_LOGON_ID)
- {
- // Found the logon SID; make a copy of it.
- dwLength = GetLengthSid(ptg->Groups[dwIndex].Sid);
- *ppsid = (PSID) HeapAlloc(GetProcessHeap(),
- HEAP_ZERO_MEMORY, dwLength);
- CopySid(dwLength, *ppsid, ptg->Groups[dwIndex].Sid);
- break;
- }
- }
- return TRUE;
- }
- int ServerCatchcopy::SetPrivilege(WCHAR *privilege, int enable)
- {
- TOKEN_PRIVILEGES tp;
- LUID luid;
- HANDLE token;
- if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) return 0;
- if (!LookupPrivilegeValue(NULL, privilege, &luid)) return 0;
- tp.PrivilegeCount = 1;
- tp.Privileges[0].Luid = luid;
- if (enable) tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- else tp.Privileges[0].Attributes = 0;
- // Enable the privilege or disable all privileges.
- return AdjustTokenPrivileges(token, 0, &tp, NULL, NULL, NULL);
- }
- void ServerCatchcopy::setRights()
- {
- ChangeFileOwner();
- QFlags<QFile::Permission> perms = ( QFile::ReadOwner | QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner | QFile::ReadUser | QFile::WriteUser | QFile::ExeUser | QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup | QFile::ReadOther | QFile::WriteOther | QFile::ExeOther );
- if(!QFile::setPermissions("\\\\.\\pipe\\"+pathSocket,perms))
- {
- error_string="Socket error: Qt unable to set the rights";
- emit error(error_string);
- }
- wchar_t lpPath[1024];
- int size=QString("\\\\.\\pipe\\"+pathSocket).toWCharArray(lpPath);
- emit error(QString::fromWCharArray(lpPath,size));
- HANDLE windows_pipe = CreateFile(lpPath,READ_CONTROL|WRITE_DAC|WRITE_OWNER,0,NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL);
- //HANDLE windows_pipe = CreateFileW(lpPath, GENERIC_READ|GENERIC_WRITE|FILE_FLAG_OVERLAPPED , 0, NULL, OPEN_EXISTING, 0, NULL);
- if(windows_pipe == INVALID_HANDLE_VALUE)
- {
- void* lpBuffer;
- FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- ::GetLastError(),
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPTSTR) &lpBuffer,
- 0,
- NULL );
- MessageBox(NULL,(LPCTSTR)lpBuffer, L"GetUserName Failed", MB_OK);
- LocalFree( lpBuffer );
- error_string="Socket error: INVALID_HANDLE_VALUE for windows";
- emit error(error_string);
- return;
- }
- ACL* pOldDACL;
- SECURITY_DESCRIPTOR* pSD = NULL;
- #if defined(_M_X64) //_WIN64
- //windows 64Bits
- GetSecurityInfo(windows_pipe, SE_FILE_OBJECT , DACL_SECURITY_INFORMATION,NULL, NULL, &pOldDACL, NULL, (void**)&pSD);
- #else
- //windows 32Bits
- GetSecurityInfo(windows_pipe, SE_FILE_OBJECT , DACL_SECURITY_INFORMATION,NULL, NULL, &pOldDACL, NULL, &pSD);
- #endif
- PSID pSid = NULL;
- SID_IDENTIFIER_AUTHORITY authNt = SECURITY_WORLD_SID_AUTHORITY;
- AllocateAndInitializeSid(&authNt,2,SECURITY_BUILTIN_DOMAIN_RID,DOMAIN_ALIAS_RID_USERS,0,0,0,0,0,0,&pSid);
- EXPLICIT_ACCESS ea={0};
- ea.grfAccessMode = GRANT_ACCESS;
- ea.grfAccessPermissions = GENERIC_ALL;
- ea.grfInheritance = CONTAINER_INHERIT_ACE|OBJECT_INHERIT_ACE;
- ea.Trustee.TrusteeType = TRUSTEE_IS_COMPUTER;
- ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
- ea.Trustee.ptstrName = (LPTSTR)pSid;
- ACL* pNewDACL = 0;
- DWORD err = SetEntriesInAcl(1,&ea,pOldDACL,&pNewDACL);
- if(pNewDACL)
- SetSecurityInfo(windows_pipe,SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,NULL, NULL, pNewDACL, NULL);
- FreeSid(pSid);
- LocalFree(pNewDACL);
- LocalFree(pSD);
- LocalFree(pOldDACL);
- CloseHandle(windows_pipe);
- }
- #endif
- bool ServerCatchcopy::listen()
- {
- QLocalSocket socketTestConnection;
- pathSocket=ExtraSocketCatchcopy::pathSocket();
- socketTestConnection.connectToServer(pathSocket);
- if(socketTestConnection.waitForConnected(CATCHCOPY_COMMUNICATION_TIMEOUT))
- {
- error_string="Other server is listening";
- emit error(error_string);
- return false;
- }
- else
- {
- if(!server.removeServer(pathSocket))
- {
- error_string="Unable to remove the old server";
- emit error(error_string);
- }
- if(server.listen(pathSocket))
- {
- #ifdef Q_OS_WIN32
- emit trySetRights();
- #endif
- return true;
- }
- else
- {
- error_string=QString("Unable to listen %1: %2").arg(pathSocket).arg(server.errorString());
- emit error(error_string);
- return false;
- }
- }
- }
- void ServerCatchcopy::close()
- {
- if(server.isListening())
- {
- int index=0;
- while(index<ClientList.size())
- {
- ClientList.at(index).socket->disconnectFromServer();
- index++;
- }
- server.close();
- if(!server.removeServer(pathSocket))
- {
- error_string="Unable to remove the old server";
- emit error(error_string);
- }
- }
- }
- const QString ServerCatchcopy::errorStringServer()
- {
- return server.errorString();
- }
- const QString ServerCatchcopy::errorString()
- {
- return error_string;
- }
- /// \brief New connexion
- void ServerCatchcopy::newConnection()
- {
- while(server.hasPendingConnections())
- {
- QLocalSocket *clientSocket = server.nextPendingConnection();
- if(clientSocket!=NULL)
- {
- do
- {
- idNextClient++;
- if(idNextClient>2000000000)
- idNextClient=0;
- } while(clientIdFound(idNextClient));
- Client newClient;
- newClient.id = idNextClient;
- newClient.socket = clientSocket;
- newClient.haveData = false;
- newClient.firstProtocolReplied = false;
- newClient.detectTimeOut = new QTimer(this);
- newClient.detectTimeOut->setSingleShot(true);
- newClient.detectTimeOut->setInterval(CATCHCOPY_COMMUNICATION_TIMEOUT);
- connect(newClient.socket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(connectionError(QLocalSocket::LocalSocketError)));
- connect(newClient.socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
- connect(newClient.socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
- connect(newClient.detectTimeOut,SIGNAL(timeout()), this, SLOT(checkTimeOut()));
- ClientList << newClient;
- emit connectedClient(newClient.id);
- }
- }
- }
- bool ServerCatchcopy::clientIdFound(quint32 id)
- {
- int index=0;
- while(index<ClientList.size())
- {
- if(ClientList.at(index).id==id)
- return true;
- index++;
- }
- return false;
- }
- /// \brief new error at connexion
- void ServerCatchcopy::connectionError(QLocalSocket::LocalSocketError error)
- {
- QLocalSocket *socket=qobject_cast<QLocalSocket *>(QObject::sender());
- if(socket==NULL)
- {
- qWarning() << "Unlocated client socket!";
- return;
- }
- int index=0;
- while(index<ClientList.size())
- {
- if(ClientList.at(index).socket==socket)
- {
- if(error!=QLocalSocket::PeerClosedError)
- qWarning() << "error detected for the client: " << index << ", type: " << error;
- ClientList.at(index).socket->disconnectFromServer();
- return;
- }
- index++;
- }
- }
- void ServerCatchcopy::disconnected()
- {
- QLocalSocket *socket=qobject_cast<QLocalSocket *>(QObject::sender());
- if(socket==NULL)
- {
- qWarning() << "Unlocated client socket!";
- return;
- }
- int index=0;
- while(index<ClientList.size())
- {
- if(ClientList.at(index).socket==socket)
- {
- emit disconnectedClient(ClientList.at(index).id);
- disconnect(ClientList.at(index).socket);
- disconnect(ClientList.at(index).detectTimeOut);
- delete ClientList.at(index).detectTimeOut;
- ClientList.at(index).socket->abort();
- ClientList.at(index).socket->disconnectFromServer();
- ClientList.at(index).socket->deleteLater();
- ClientList.removeAt(index);
- return;
- }
- index++;
- }
- qWarning() << "Unlocated client!";
- }
- void ServerCatchcopy::disconnectClient(quint32 id)
- {
- int index=0;
- while(index<ClientList.size())
- {
- if(ClientList.at(index).id==id)
- {
- ClientList.at(index).socket->disconnectFromServer();
- return;
- }
- index++;
- }
- qWarning() << "Unlocated client!";
- }
- void ServerCatchcopy::readyRead()
- {
- QLocalSocket *socket=qobject_cast<QLocalSocket *>(QObject::sender());
- if(socket==NULL)
- {
- qWarning() << "Unlocated client socket!";
- return;
- }
- int index=0;
- while(index<ClientList.size())
- {
- if(ClientList.at(index).socket==socket)
- {
- while(socket->bytesAvailable()>0)
- {
- if(!ClientList.at(index).haveData)
- {
- if(socket->bytesAvailable()<(int)sizeof(int))//ignore because first int is cuted!
- {
- /*error_string="Bytes available is not sufficient to do a int";
- emit error(error_string);
- disconnectClient(ClientList.at(index).id);*/
- return;
- }
- QDataStream in(socket);
- in.setVersion(QDataStream::Qt_4_4);
- in >> ClientList[index].dataSize;
- ClientList[index].dataSize-=sizeof(int);
- if(ClientList.at(index).dataSize>64*1024*1024) // 64MB
- {
- error_string="Reply size is >64MB, seam corrupted";
- emit error(error_string);
- disconnectClient(ClientList.at(index).id);
- return;
- }
- if(ClientList.at(index).dataSize<(int)(sizeof(int) //orderId
- + sizeof(quint32) //returnCode
- + sizeof(quint32) //string list size
- ))
- {
- error_string="Reply size is too small to have correct code";
- emit error(error_string);
- disconnectClient(ClientList.at(index).id);
- return;
- }
- ClientList[index].haveData=true;
- }
- if(ClientList.at(index).dataSize<(ClientList.at(index).data.size()+socket->bytesAvailable()))
- ClientList[index].data.append(socket->read(ClientList.at(index).dataSize-ClientList.at(index).data.size()));
- else
- ClientList[index].data.append(socket->readAll());
- if(ClientList.at(index).dataSize==(quint32)ClientList.at(index).data.size())
- {
- if(!checkDataIntegrity(ClientList.at(index).data))
- {
- emit error("Data integrity wrong: "+QString(ClientList.at(index).data.toHex()));
- ClientList[index].data.clear();
- ClientList[index].haveData=false;
- qWarning() << "Data integrity wrong";
- return;
- }
- QStringList returnList;
- quint32 orderId;
- QDataStream in(ClientList.at(index).data);
- in.setVersion(QDataStream::Qt_4_4);
- in >> orderId;
- in >> returnList;
- ClientList[index].data.clear();
- ClientList[index].haveData=false;
- if(ClientList.at(index).queryNoReplied.contains(orderId))
- {
- emit error("Duplicate query id");
- qWarning() << "Duplicate query id";
- return;
- }
- ClientList[index].queryNoReplied << orderId;
- if(!ClientList.at(index).firstProtocolReplied && returnList.size()==2 && returnList.first()=="protocol" && autoReply)
- {
- ClientList[index].firstProtocolReplied=true;
- protocolSupported(ClientList.at(index).id,orderId,(returnList.last()==CATCHCOPY_PROTOCOL_VERSION));
- }
- else
- parseInput(ClientList.at(index).id,orderId,returnList);
- }
- }
- if(ClientList.at(index).haveData)
- ClientList.at(index).detectTimeOut->start();
- else
- ClientList.at(index).detectTimeOut->stop();
- return;
- }
- index++;
- }
- emit error("Unallocated client!");
- qWarning() << "Unallocated client!";
- }
- bool ServerCatchcopy::checkDataIntegrity(QByteArray data)
- {
- quint32 orderId;
- qint32 listSize;
- QDataStream in(data);
- in.setVersion(QDataStream::Qt_4_4);
- in >> orderId;
- in >> listSize;
- if(listSize>65535)
- {
- emit error("List size is wrong");
- qWarning() << "List size is wrong";
- return false;
- }
- int index=0;
- while(index<listSize)
- {
- qint32 stringSize;
- in >> stringSize;
- if(stringSize>65535)
- {
- emit error("String size is wrong");
- return false;
- }
- if(stringSize>(in.device()->size()-in.device()->pos()))
- {
- emit error(QString("String size is greater than the data: %1>(%2-%3)").arg(stringSize).arg(in.device()->size()).arg(in.device()->pos()));
- return false;
- }
- in.device()->seek(in.device()->pos()+stringSize);
- index++;
- }
- if(in.device()->size()!=in.device()->pos())
- {
- emit error("Remaining data after string list parsing");
- return false;
- }
- return true;
- }
- void ServerCatchcopy::parseInput(quint32 client,quint32 orderId,QStringList returnList)
- {
- switch(parseInputCurrentProtocol(client,orderId,returnList))
- {
- case Ok:
- emit newQuery(client,orderId,returnList);
- break;
- case Replied:
- break;
- case ExtensionWrong:
- if(autoReply)
- protocolExtensionSupported(client,orderId,false);
- else
- emit newQuery(client,orderId,returnList);
- break;
- case WrongArgument:
- if(autoReply)
- incorrectArgument(client,orderId);
- else
- emit newQuery(client,orderId,returnList);
- break;
- case WrongArgumentListSize:
- if(autoReply)
- incorrectArgumentListSize(client,orderId);
- else
- emit newQuery(client,orderId,returnList);
- break;
- case UnknowOrder:
- emit error("Unknown query");
- qWarning() << "Unknown query";
- if(autoReply)
- unknowOrder(client,orderId);
- else
- emit newQuery(client,orderId,returnList);
- break;
- }
- }
- ServerCatchcopy::inputReturnType ServerCatchcopy::parseInputCurrentProtocol(quint32 client,quint32 orderId,QStringList returnList)
- {
- if(returnList.size()==0)
- return WrongArgumentListSize;
- //if is supported
- QString firstArgument=returnList.first();
- if(firstArgument=="protocol")
- {
- if(returnList.size()!=2)
- return WrongArgumentListSize;
- emit askProtocolCompatibility(client,orderId,returnList.last());
- return Ok;
- }
- else if(firstArgument=="protocol extension")
- {
- if(returnList.size()>3 || returnList.size()<2)
- return WrongArgumentListSize;
- if(!autoReply)
- {
- if(returnList.size()==2)
- emit askProtocolExtension(client,orderId,returnList.last());
- else
- emit askProtocolExtension(client,orderId,returnList.at(1),returnList.last());
- }
- return ExtensionWrong;
- }
- else if(firstArgument=="client")
- {
- if(returnList.size()!=2)
- return WrongArgumentListSize;
- emit clientName(client,returnList.last());
- if(autoReply)
- {
- clientRegistered(client,orderId);
- return Replied;
- }
- else
- {
- emit clientName(client,orderId,returnList.last());
- return Ok;
- }
- }
- else if(firstArgument=="server")
- {
- if(returnList.size()!=2)
- return WrongArgumentListSize;
- if(returnList.last()!="name?")
- return WrongArgument;
- if(autoReply)
- {
- serverName(client,orderId,name);
- return Replied;
- }
- else
- {
- askServerName(client,orderId);
- return Ok;
- }
- }
- else if(firstArgument=="cp")
- {
- if(returnList.size()<3)
- return WrongArgumentListSize;
- QStringList sourceList=returnList;
- sourceList.removeFirst();
- sourceList.removeLast();
- emitNewCopy(client,orderId,sourceList,returnList.last());
- return Ok;
- }
- else if(firstArgument=="cp-?")
- {
- if(returnList.size()<2)
- return WrongArgumentListSize;
- QStringList sourceList=returnList;
- sourceList.removeFirst();
- emitNewCopy(client,orderId,sourceList);
- return Ok;
- }
- else if(firstArgument=="mv")
- {
- if(returnList.size()<3)
- return WrongArgumentListSize;
- QStringList sourceList=returnList;
- sourceList.removeFirst();
- sourceList.removeLast();
- emitNewMove(client,orderId,sourceList,returnList.last());
- return Ok;
- }
- else if(firstArgument=="mv-?")
- {
- if(returnList.size()<2)
- return WrongArgumentListSize;
- QStringList sourceList=returnList;
- sourceList.removeFirst();
- emitNewMove(client,orderId,sourceList);
- return Ok;
- }
- else //if is not supported
- return UnknowOrder;
- }
- void ServerCatchcopy::emitNewCopy(quint32 client,quint32 orderId,QStringList sources)
- {
- emit newCopy(client,orderId,sources);
- LinkGlobalToLocalClient newAssociation;
- newAssociation.idClient=client;
- newAssociation.orderId=orderId;
- newAssociation.globalOrderId=incrementOrderId();
- LinkGlobalToLocalClientList << newAssociation;
- emit newCopy(newAssociation.globalOrderId,sources);
- }
- void ServerCatchcopy::emitNewCopy(quint32 client,quint32 orderId,QStringList sources,QString destination)
- {
- emit newCopy(client,orderId,sources,destination);
- LinkGlobalToLocalClient newAssociation;
- newAssociation.idClient=client;
- newAssociation.orderId=orderId;
- newAssociation.globalOrderId=incrementOrderId();
- LinkGlobalToLocalClientList << newAssociation;
- emit newCopy(newAssociation.globalOrderId,sources,destination);
- }
- void ServerCatchcopy::emitNewMove(quint32 client,quint32 orderId,QStringList sources)
- {
- emit newMove(client,orderId,sources);
- LinkGlobalToLocalClient newAssociation;
- newAssociation.idClient=client;
- newAssociation.orderId=orderId;
- newAssociation.globalOrderId=incrementOrderId();
- LinkGlobalToLocalClientList << newAssociation;
- emit newMove(newAssociation.globalOrderId,sources);
- }
- void ServerCatchcopy::emitNewMove(quint32 client,quint32 orderId,QStringList sources,QString destination)
- {
- emit newMove(client,orderId,sources,destination);
- LinkGlobalToLocalClient newAssociation;
- newAssociation.idClient=client;
- newAssociation.orderId=orderId;
- newAssociation.globalOrderId=incrementOrderId();
- LinkGlobalToLocalClientList << newAssociation;
- emit newMove(newAssociation.globalOrderId,sources,destination);
- }
- void ServerCatchcopy::copyFinished(quint32 globalOrderId,bool withError)
- {
- int index=0;
- while(index<LinkGlobalToLocalClientList.size())
- {
- if(LinkGlobalToLocalClientList.at(index).globalOrderId==globalOrderId)
- {
- copyFinished(LinkGlobalToLocalClientList.at(index).idClient,LinkGlobalToLocalClientList.at(index).orderId,withError);
- LinkGlobalToLocalClientList.removeAt(index);
- orderList.removeOne(globalOrderId);
- return;
- }
- index++;
- }
- }
- void ServerCatchcopy::copyCanceled(quint32 globalOrderId)
- {
- int index=0;
- while(index<LinkGlobalToLocalClientList.size())
- {
- if(LinkGlobalToLocalClientList.at(index).globalOrderId==globalOrderId)
- {
- copyCanceled(LinkGlobalToLocalClientList.at(index).idClient,LinkGlobalToLocalClientList.at(index).orderId);
- LinkGlobalToLocalClientList.removeAt(index);
- orderList.removeOne(globalOrderId);
- return;
- }
- index++;
- }
- }
- void ServerCatchcopy::setAutoReply(bool value)
- {
- autoReply=value;
- }
- bool ServerCatchcopy::getAutoReply()
- {
- return autoReply;
- }
- void ServerCatchcopy::reply(quint32 client,quint32 orderId,quint32 returnCode,QString returnString)
- {
- reply(client,orderId,returnCode,QStringList() << returnString);
- }
- void ServerCatchcopy::reply(quint32 client,quint32 orderId,quint32 returnCode,QStringList returnList)
- {
- int index=0;
- while(index<ClientList.size())
- {
- if(ClientList.at(index).id==client)
- {
- if(ClientList.at(index).socket->isValid() && ClientList.at(index).socket->state()==QLocalSocket::ConnectedState)
- {
- if(!ClientList.at(index).queryNoReplied.contains(orderId))
- {
- qWarning() << "Reply to missing query or previously replied";
- return;
- }
- ClientList[index].queryNoReplied.removeOne(orderId);
- //cut string list and send it as block of 32KB
- QByteArray block;
- QDataStream out(&block, QIODevice::WriteOnly);
- out.setVersion(QDataStream::Qt_4_4);
- out << int(0);
- out << orderId;
- out << returnCode;
- out << returnList;
- out.device()->seek(0);
- out << block.size();
- emit dataSend(client,orderId,returnCode,block);
- emit dataSend(client,orderId,returnCode,returnList);
- do
- {
- QByteArray blockToSend;
- int byteWriten;
- blockToSend=block.left(32*1024);//32KB
- block.remove(0,blockToSend.size());
- byteWriten = ClientList[index].socket->write(blockToSend);
- if(!ClientList[index].socket->isValid())
- {
- error_string="Socket is not valid";
- emit error(error_string);
- return;
- }
- if(ClientList[index].socket->errorString()!="Unknown error" && ClientList[index].socket->errorString()!="")
- {
- error_string="Error with socket: "+ClientList[index].socket->errorString();
- emit error(error_string);
- return;
- }
- if(blockToSend.size()!=byteWriten)
- {
- error_string="All the bytes have not be written";
- emit error(error_string);
- return;
- }
- }
- while(block.size());
- }
- else
- {
- error_string="Socket is not valid or not connected";
- emit error(error_string);
- }
- return;
- }
- index++;
- }
- qWarning() << "Client id not found:" << client;
- }
- void ServerCatchcopy::protocolSupported(quint32 client,quint32 orderId,bool value)
- {
- if(value)
- reply(client,orderId,1000,"protocol supported");
- else
- reply(client,orderId,5003,"protocol not supported");
- }
- void ServerCatchcopy::incorrectArgumentListSize(quint32 client,quint32 orderId)
- {
- reply(client,orderId,5000,"incorrect argument list size");
- }
- void ServerCatchcopy::incorrectArgument(quint32 client,quint32 orderId)
- {
- reply(client,orderId,5001,"incorrect argument");
- }
- void ServerCatchcopy::protocolExtensionSupported(quint32 client,quint32 orderId,bool value)
- {
- if(value)
- reply(client,orderId,1001,"protocol extension supported");
- else
- reply(client,orderId,1002,"protocol extension not supported");
- }
- void ServerCatchcopy::clientRegistered(quint32 client,quint32 orderId)
- {
- reply(client,orderId,1003,"client registered");
- }
- void ServerCatchcopy::serverName(quint32 client,quint32 orderId,QString name)
- {
- reply(client,orderId,1004,name);
- }
- void ServerCatchcopy::copyFinished(quint32 client,quint32 orderId,bool withError)
- {
- if(!withError)
- reply(client,orderId,1005,"finished");
- else
- reply(client,orderId,1006,"finished with error(s)");
- }
- void ServerCatchcopy::copyCanceled(quint32 client,quint32 orderId)
- {
- reply(client,orderId,1007,"canceled");
- }
- void ServerCatchcopy::unknowOrder(quint32 client,quint32 orderId)
- {
- reply(client,orderId,5002,"unknown order");
- }
- void ServerCatchcopy::checkTimeOut()
- {
- QTimer *timer=qobject_cast<QTimer *>(QObject::sender());
- if(timer==NULL)
- {
- qWarning() << "Unallocated client timer!";
- return;
- }
- int index=0;
- while(index<ClientList.size())
- {
- if(ClientList.at(index).detectTimeOut==timer)
- {
- ClientList.at(index).detectTimeOut->stop();
- if(ClientList.at(index).haveData)
- {
- error_string="The client is too long to send the next part of the reply: "+ClientList.at(index).data;
- ClientList[index].haveData=false;
- ClientList[index].data.clear();
- ClientList.at(index).socket->disconnectFromServer();
- emit error(error_string);
- }
- return;
- }
- index++;
- }
- }
- quint32 ServerCatchcopy::incrementOrderId()
- {
- do
- {
- nextOrderId++;
- if(nextOrderId>2000000)
- nextOrderId=0;
- } while(orderList.contains(nextOrderId));
- return nextOrderId;
- }
Add Comment
Please, Sign In to add comment