Skip to content

Commit

Permalink
OpenAuction Request::receive and Response::send + refactor assetSize
Browse files Browse the repository at this point in the history
  • Loading branch information
pereira0x committed Dec 8, 2023
1 parent d105a24 commit 0c3857f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/client/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ void OpenAuctionCommand::handleCommand(std::string args, UserState &state) {
openAuctionRequest.assetFilename = asset_fname;
openAuctionRequest.startValue = (uint32_t)std::stoi(start_value);
openAuctionRequest.timeActive = (uint32_t)std::stoi(timeactive);
openAuctionRequest.assetSize = getFileSize(asset_fname);

OpenAuctionResponse openAuctionResponse;
state.sendTcpPacketAndWaitForReply(openAuctionRequest, openAuctionResponse);
Expand Down
41 changes: 34 additions & 7 deletions src/utils/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,6 @@ void ShowAssetResponse::send(int fd) {
}
stream << std::endl;
writeString(fd, stream.str());

}

void ShowAssetResponse::receive(int fd) {
Expand Down Expand Up @@ -752,8 +751,7 @@ void OpenAuctionRequest::send(int fd) {
stream << OpenAuctionRequest::ID << " " << this->userID << " "
<< this->password << " " << this->auctionName << " "
<< this->startValue << " " << this->timeActive << " "
<< this->assetFilename << " " << getFileSize(this->assetFilename)
<< " ";
<< this->assetFilename << " " << assetSize << " ";
writeString(fd, stream.str());

stream.str(std::string());
Expand All @@ -765,14 +763,43 @@ void OpenAuctionRequest::send(int fd) {
}

void OpenAuctionRequest::receive(int fd) {
// Serverbound packets don't read their ID
readSpace(fd);
userID = readString(fd);
readSpace(fd);
password = readString(fd);
readSpace(fd);
auctionName = readString(fd);
readSpace(fd);
startValue = readInt(fd);
readSpace(fd);
timeActive = readInt(fd);
readSpace(fd);
assetFilename = readString(fd);
readSpace(fd);
assetSize = readInt(fd);
readSpace(fd);
readAndSaveToFile(fd, assetFilename, assetSize);
readPacketDelimiter(fd);
}

void OpenAuctionResponse::send(int fd) {
if (fd == -1)
return;
return;
std::stringstream stream;
stream << OpenAuctionResponse::ID << " ";

if (status == OK) {
stream << "OK";
stream << " " << auctionID;
} else if (status == NOK) {
stream << "NOK";
} else if (status == NLG) {
stream << "NLG";
} else if (status == ERR) {
stream << "ERR";
} else {
throw InvalidPacketException();
}
stream << std::endl;
writeString(fd, stream.str());
}

void OpenAuctionResponse::receive(int fd) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ class OpenAuctionRequest : public TcpPacket {
std::string password;
std::string auctionName;
std::string assetFilename;
uint32_t assetSize;
uint32_t startValue;
uint32_t timeActive;

Expand Down

0 comments on commit 0c3857f

Please sign in to comment.