Skip to content

Commit 2ec0fb0

Browse files
committed
小白学习workflow-中篇-源码设计
1 parent da09ee7 commit 2ec0fb0

File tree

4 files changed

+704
-0
lines changed

4 files changed

+704
-0
lines changed

C++Notes/C++Primer_5th/15_面向对象程序设计.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,27 @@ int main()
142142
}
143143
```
144144
145+
### 虚函数与作用域
146+
147+
基类和派生类的虚函数接受的实参必须相同,否则就无法通过基类的引用或者指针调用派生类的虚函数。
148+
149+
```c++
150+
class Base{
151+
public:
152+
virtual int fun();
153+
};
154+
155+
class D1 : public Base{
156+
public:
157+
int fun(int); //@ 隐藏了Base中的fun函数
158+
virtual void f2();
159+
};
160+
161+
class D2 : public D1{
162+
public:
163+
int fun(int); //@ 隐藏了D1 中的 fun 函数
164+
int fun(); //@ 覆盖了Base 中的 fun 函数
165+
void f2(); //@ 覆盖了 D1 中的 f2 函数
166+
};
167+
```
168+

C++Notes/NetPrograming/net.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//
2+
// Created by hrw on 2021/03/05.
3+
//
4+
#include <sys/epoll.h>
5+
#include <sys/types.h> /* basic system data types */
6+
#include <sys/socket.h> /* basic socket definitions */
7+
#include <sys/time.h> /* timeval{} for select() */
8+
#include <time.h> /* timespec{} for pselect() */
9+
#include <netinet/in.h> /* sockaddr_in{} and other Internet defns */
10+
#include <arpa/inet.h> /* inet(3) functions */
11+
#include <errno.h>
12+
#include <fcntl.h> /* for nonblocking */
13+
#include <netdb.h>
14+
#include <signal.h>
15+
#include <stdio.h>
16+
#include <stdlib.h>
17+
#include <string.h>
18+
#include <sys/stat.h> /* for S_xxx file mode constants */
19+
#include <sys/uio.h> /* for iovec{} and readv/writev */
20+
#include <unistd.h>
21+
#include <sys/wait.h>
22+
#include <sys/un.h> /* for Unix domain sockets */
23+
24+
#include <sys/select.h> /* for convenience */
25+
#include <sys/sysctl.h>
26+
#include <poll.h> /* for convenience */
27+
#include <strings.h> /* for convenience */
28+
#include <sys/ioctl.h>
29+
#include <pthread.h>
30+
#include <assert.h>
31+
#include <linux/types.h>
32+
33+
#ifndef MAX_EVENT_NUMBER
34+
#define MAX_EVENT_NUMBER 1024
35+
36+
// 普通的 BSD 标准 socket 结构体
37+
// socket_state: socket 状态, 连接?不连接?
38+
// type: socket type (%SOCK_STREAM, etc)
39+
// flags: socket flags (%SOCK_NOSPACE, etc)
40+
// ops: 专用协议的socket的操作
41+
// file: 与socket 有关的指针列表
42+
// sk: 负责协议相关结构体,这样就让这个这个结构体和协议分开。
43+
// wq: 等待队列
44+
struct socket {
45+
socket_state state;
46+
kmemcheck_bitfield_begin(type);
47+
short type;
48+
kmemcheck_bitfield_end(type);
49+
unsigned long flags;
50+
struct socket_wa __rcu *wq;
51+
struct file *file;
52+
struct sock *sk;
53+
const struct proto_ops *ops;
54+
};
55+
56+
//file:net/socket.c
57+
SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
58+
{
59+
//......
60+
retval = sock_create(family, type, protocol, &sock);
61+
}
62+
63+
//sock_create 是创建 socket 的主要位置。其中 sock_create 又调用了 __sock_create。
64+
65+
//file:net/socket.c
66+
int __sock_create(struct net *net, int family, int type, int protocol,
67+
struct socket **res, int kern)
68+
{
69+
struct socket *sock;
70+
const struct net_proto_family *pf;
71+
72+
//......
73+
74+
//分配 socket 对象
75+
sock = sock_alloc();
76+
77+
//获得每个协议族的操作表
78+
pf = rcu_dereference(net_families[family]);
79+
80+
//调用每个协议族的创建函数, 对于 AF_INET 对应的是
81+
err = pf->create(net, sock, protocol, kern);
82+
}
83+
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+

0 commit comments

Comments
 (0)