HERRCODE ChatClient::sendFileTransferRequest(const std::wstring& email, const RequestFilesInfo& fileInfo, time_t timestamp) { SockStream os; os.writeInt(net::kCommandType_FileTransferRequest); os.writeInt(0); os.writeString(email); os.writeString(fileInfo.fileName); os.writeInt(fileInfo.fileSize); os.writeInt64(timestamp); os.flushSize(); queueSendRequest(sock_, os); return H_OK; }
HERRCODE ChatClient::logout() { SockStream stream; stream.writeInt(net::kCommandType_Logout); stream.writeInt(0); stream.writeString(email_); stream.flushSize(); auto ret = ::send(sock_, stream.getBuf(), stream.getSize(), 0); if (ret == SOCKET_ERROR) { return H_NETWORK_ERROR; } quit(true); return H_OK; }
HERRCODE ChatClient::loginImpl(int type, const std::wstring& username, const std::wstring& credential) { sockaddr_in addr = { 0 }; addr.sin_port = htons(serverPort_); addr.sin_family = AF_INET; IN_ADDR in_addr; InetPton(AF_INET, serverAddr_.c_str(), &in_addr); addr.sin_addr = in_addr; if (::connect(sock_, (const sockaddr*)&addr, sizeof(sockaddr_in)) != 0) { return H_NETWORK_ERROR; } SockStream stream; stream.writeInt(net::kCommandType_Login); stream.writeInt(0); stream.writeInt(type); stream.writeString(username); stream.writeString(credential); stream.flushSize(); auto ret = ::send(sock_, stream.getBuf(), stream.getSize(), 0); if (ret == SOCKET_ERROR) { return H_NETWORK_ERROR; } buffer buf(200); int rc = ::recv(sock_, buf.data(), buf.size(), 0); if (rc) { SockStream ss(buf.data(), rc); assert(net::kCommandType_LoginAck == ss.getInt()); auto size = ss.getInt(); auto ack = ss.getInt(); if (ack == net::kLoginAck_Succeeded) { email_ = username; authKey_ = ss.getString(); auto hr = initSocks(sock_); if (hr != H_OK) return hr; return H_OK; } else { return H_AUTH_FAILED; } } else { return H_NETWORK_ERROR; } }
HERRCODE ChatClient::sendMessage(const std::wstring& username, const std::wstring& message, time_t timestamp) { if (message.find(L"<img") == -1) { SockStream stream; stream.writeInt(net::kCommandType_Message); stream.writeInt(0); stream.writeString(email_); stream.writeString(username); stream.writeInt64(timestamp); stream.writeString(message); stream.flushSize(); queueSendRequest(sock_, stream); msgMan_.addMessageRequest(timestamp, username, timestamp); } else { ChatOverlappedData* ol = new ChatOverlappedData(net::kAction_SendMessage); ImageMessageForSend* msg = new ImageMessageForSend; msg->setRawMessage(message, username, timestamp); ol->setProp((int)msg); PostQueuedCompletionStatus(hComp_, 0, kServerKey, ol); } return H_OK; }
HERRCODE ChatClient::confirmFileTransferRequest(const std::wstring& username, time_t timestamp, bool recv, const std::wstring& savePath) { if (!recv) { SockStream os; os.writeInt(net::kCommandType_FileTransferRequestAck); os.writeInt(0); os.writeString(username); os.writeInt64(timestamp); os.writeBool(false); os.flushSize(); queueSendRequest(sock_, os); } else { SockStream os; os.writeInt(net::kCommandType_FileTransferRequestAck); os.writeInt(0); os.writeString(username); os.writeInt64(timestamp); os.writeBool(true); os.flushSize(); queueSendRequest(sock_, os); } return H_OK; }