本文整理汇总了C++中Disk::write方法的典型用法代码示例。如果您正苦于以下问题:C++ Disk::write方法的具体用法?C++ Disk::write怎么用?C++ Disk::write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Disk
的用法示例。
在下文中一共展示了Disk::write方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: dispatch
int RPC::dispatch(Disk& disk)
{
char buf[NDISK_BLOCK_SIZE];
char packet[NDISK_BLOCK_SIZE + 8];
size_t len = NDISK_BLOCK_SIZE + 8;
char *msg = packet;
if (disk.block_size() != NDISK_BLOCK_SIZE)
return DISK_ERROR;
// Read the packet (with 8-byte header)
do
{
ssize_t nread = recv(sock, msg, len, 0);
if (nread <= 0)
return DISK_ERROR;
len -= nread;
msg += nread;
}
while (len > 0);
// Get the command from the packet's first four bytes
char command[5];
memcpy(command, packet, 4);
command[4] = '\0';
// Get the block number parameter from the packet
int block_num = ston(packet + 4);
if (!strcmp(command, "NUMB"))
{
// Write num blocks into packet data
ntos(packet + 8, disk.num_blocks());
}
else if (!strcmp(command, "FRMT"))
{
disk.format(block_num);
}
else if (!strcmp(command, "READ"))
{
disk.read(block_num, buf);
memcpy(packet + 8, buf, NDISK_BLOCK_SIZE);
}
else if (!strcmp(command, "WRTE"))
{
memcpy(buf, packet + 8, NDISK_BLOCK_SIZE);
disk.write(block_num, buf);
}
else
{
message1("Unknown RPC command %s\n", command);
}
message2("RPC command %s(%d)\n", command, block_num);
len = NDISK_BLOCK_SIZE + 8;
msg = packet;
// Write the packet (with 8-byte header)
do
{
ssize_t nwritten = send(sock, msg, len, 0);
if (nwritten < 0)
return DISK_ERROR;
len -= nwritten;
msg += nwritten;
}
while (len > 0);
return DISK_OK;
}