CS144-check0

setup

macos用户指北

  • 安装utm和下载官方对应的压缩包,解压后在utm中打开
  • 我的虚拟机默认没有打开端口?(不清楚具体的原理),通过google学习后,在utm的虚拟机的设置中,找到network,将network mode变成Emulated Vlan,这时候会出现port的选项,进去将guest port设为22, host port设为2222。
  • 在mac的终端,现在就可以通过ssh连接了,ssh -p 2222 cs144@localhost

networking by hand

按照要求去玩一下,但后面需要校内学生帐号。

webget

因为用vim太麻烦,不方便搞project,查了一下,可以使用ssh,将vscode连接虚拟主机,按照vscode官方的文档(连接时的命令一定是上面那一条),就可以连上,在物理机的vscode就可以打开虚拟机的文件夹并操作了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void get_URL( const string& host, const string& path )
{
//cerr << "Function called: get_URL(" << host << ", " << path << ")\n";
TCPSocket tcp_socket;
const Address host_addr(host, "http");

tcp_socket.connect(host_addr);

const string request = "GET "+path+" HTTP/1.1\r\nHost:"+host+"\r\nConnection: close\r\n\r\n";
tcp_socket.write(request);

while(!tcp_socket.eof()) {
string buf;
tcp_socket.read(buf);
cout << buf;
}
tcp_socket.close();
//cerr << "Warning: get_URL() has not been implemented yet.\n";
}

刚好10行(不算括号)。

byte stream

开始我的byte stream是用string来存的,顺利通过了前7个测试,但到了stress_test的时候超时了,猜测应该是过多的字符串的复制修改导致的,所以试图寻找c++ std里面更快的数据结构。

然而在边用queue边调错时发现,导致超时的原因压根不是string的复制操作效率过低,而是理解错了peek的意思,并不是只传首个字节,而是要将buffer将只读的方法传回。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// byte_stream.hh
class ByteStream
{
public:
explicit ByteStream( uint64_t capacity );

// Helper functions (provided) to access the ByteStream's Reader and Writer interfaces
Reader& reader();
const Reader& reader() const;
Writer& writer();
const Writer& writer() const;

void set_error() { error_ = true; }; // Signal that the stream suffered an error.
bool has_error() const { return error_; }; // Has the stream had an error?

protected:
// Please add any additional state to the ByteStream here, and not to the Writer and Reader interfaces.
std::string buffer_{};
uint64_t capacity_;
uint64_t bytes_pushed_{};
uint64_t bytes_popped_{};
bool error_ {};
bool closed_{};
};

还是折腾了一会。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// byte_stream.cc

#include "byte_stream.hh"

using namespace std;

ByteStream::ByteStream( uint64_t capacity ) : capacity_( capacity ) {}

bool Writer::is_closed() const
{
return writer().closed_;
}

void Writer::push( string data )
{
uint64_t cap = writer().available_capacity();
if(data.length() > cap)
{
data = data.substr(0, cap);
}

writer().buffer_ += data;
writer().bytes_pushed_ += data.size();
return;
}

void Writer::close()
{
writer().closed_ = true;
}

uint64_t Writer::available_capacity() const
{
return (writer().capacity_ - writer().buffer_.size());
}

uint64_t Writer::bytes_pushed() const
{
return writer().bytes_pushed_;
}

bool Reader::is_finished() const
{
bool b = (reader().buffer_.size() == 0? true: false)
&& (reader().closed_);
return b;
}

uint64_t Reader::bytes_popped() const
{
return reader().bytes_popped_;
}

string_view Reader::peek() const
{
string_view view = reader().buffer_;
return view;
}

void Reader::pop( uint64_t len )
{
// Your code here.
reader().buffer_ = reader().buffer_.substr(len);
reader().bytes_popped_ += len;
}

uint64_t Reader::bytes_buffered() const
{
// Your code here.
return reader().buffer_.size();
}

check0算是热身,不需要特别了解网络的工作机制就可以实现。对于我来说,主要的时间是在熟悉c++的语法和思想,尽量去模仿框架代码的风格。


CS144-check0
https://pactheman123.github.io/2024/09/12/CS144-check0/
作者
Xiaopac
发布于
2024年9月12日
许可协议