读书笔记

网络服务器编程相关

  • 服务端主动关闭连接的缺点之一是会多占用服务器资源。服务端主动关闭连接之后会进入TIME_WAIT状态,在一段时间之内持有(hold)一些内核资源。如果并发访问量很高,就会影响服务端的处理能力。这似乎暗示我们应该把协议设计为客户端主动关闭,让TIME_WAIT状态分散到多台客户机器上,化整为零。(引用自<<多线程服务器端编程>>附录A1.9)

static_cast

  • 对于类类型在编译阶段不进行类型,所以在错误的使用类的向下造型的时候,并不会在编译阶段报错,但会使程序运行期间,可能出现段错误。可以使用muduo网络库中的implicit_cast代替static_cast

pair 小于号操作

template <class T1, class T2>
bool operator== (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
{ return lhs.first==rhs.first && lhs.second==rhs.second; }

template <class T1, class T2>
bool operator!= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
{ return !(lhs==rhs); }

template <class T1, class T2>
bool operator<  (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
{ return lhs.first<rhs.first || (!(rhs.first<lhs.first) && lhs.second<rhs.second); }

template <class T1, class T2>
bool operator<= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
{ return !(rhs<lhs); }

template <class T1, class T2>
bool operator>  (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
{ return rhs<lhs; }

template <class T1, class T2>
bool operator>= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
{ return !(lhs<rhs); }

std::back_inserter

template <class Container>
back_insert_iterator<Container> back_inserter (Container& x);

Construct back insert iterator
Constructs a back-insert iterator that inserts new elements at the end of x.
  • 示例
#include <iostream>     // std::cout
#include <iterator>     // std::back_inserter
#include <vector>       // std::vector
#include <algorithm>    // std::copy

int main () {
std::vector<int> foo,bar;
for (int i=1; i<=5; i++)
{ foo.push_back(i); bar.push_back(i*10); }

std::copy (bar.begin(),bar.end(),back_inserter(foo));

std::cout << "foo contains:";
for ( std::vector<int>::iterator it = foo.begin(); it!= foo.end(); ++it )
    std::cout << ' ' << *it;
std::cout << '
';

return 0;
}
  • 运行结果
foo contains: 1 2 3 4 5 10 20 30 40 50 

Signalfd、timerfd、eventfd

  • 三种使用文件描述符模拟I/O操作,使其可以使用select epoll 和poll统一管理

SIGPIPE信号

  • 在linux下写socket的程序的时候,如果尝试send到一个disconnected socket上,就会让底层抛出一个SIGPIPE信号。一般在网络编程中需要忽略该信号

poll函数的事件标志符值

常量 说明
POLLIN 普通或优先级带数据可读
POLLRDNORM 普通数据可读
POLLRDBAND 优先级带数据可读
POLLPRI 高优先级数据可读
POLLOUT 普通数据可写
POLLWRNORM 普通数据可写
POLLWRBAND 优先级带数据可写
POLLERR 发生错误
POLLHUP 对方描述符挂起
POLLNVAL 描述字不是一个打开的文件

select,poll,epoll (详见网络编程卷一)

  • select,poll,epoll本质上都是同步I/O,因为他们都需要在读写事件就绪后自己负责进行读写,也就是说这个读写过程是阻塞的,而异步I/O则无需自己负责进行读写,异步I/O的实现会负责把数据从内核空间拷贝到用户空间。(这里的同步指的是,读写操作是同步的,及需要读写操作的发起者自己完成这个动作.) 参考博客 Epoll水平触发+非阻塞IO的理解

SOL_TCP 等价 IPPROTO_TCP

[root@lhx-master ~]# grep "SOL_TCP" /usr/include/netinet/tcp.h
# define SOL_TCP		6	/* TCP level */
[root@lhx-master ~]# grep "IPPROTO_TCP" /usr/include/netinet/tcp.h
[root@lhx-master ~]# grep "IPPROTO_TCP" /usr/include/netinet/tcp.h
[root@lhx-master ~]# grep "IPPROTO_TCP" /usr/include/sys/socket.h
[root@lhx-master ~]# grep "IPPROTO_TCP" /usr/include/netinet/in.h
    IPPROTO_TCP = 6,	   /* Transmission Control Protocol.  */
#define IPPROTO_TCP		IPPROTO_TCP

tcp_info结构体,及其获取该结构体

bool Socket::getTcpInfo(struct tcp_info* tcpi) const
{
  socklen_t len = sizeof(*tcpi);
  memZero(tcpi, len);
  return ::getsockopt(sockfd_, SOL_TCP, TCP_INFO, tcpi, &len) == 0;//SOL_TCP等价IPPROTO_TCP  TCP_INFO见man 7 tcp
} 
struct tcp_info {
	__u8	tcpi_state;		   //tcp state: TCP_SYN_SENT,TCP_SYN_RECV,TCP_FIN_WAIT1,TCP_CLOSE etc
	__u8	tcpi_ca_state;     //congestion state:
	__u8	tcpi_retransmits;  //重传数,表示当前待重传的包数,这个值在重传完毕后清零
	__u8	tcpi_probes;		///* 持续定时器或保活定时器发送且未确认的段数*/
	__u8	tcpi_backoff;		//用来计算持续定时器的下一个设计值的指数退避算法指数,在传送超时是会递增。
	__u8	tcpi_options;		//tcp头部选项是否包含:扩展因子、时间戳、MSS等内容
	__u8	tcpi_snd_wscale : 4, tcpi_rcv_wscale : 4; //扩展因子数值
	__u8	tcpi_delivery_rate_app_limited:1;  //限速标志

	__u32	tcpi_rto;		//重传超时时间,这个和RTT有关系,RTT越大,rto越大
	__u32	tcpi_ato;		//用来延时确认的估值,单位为微秒. 
							//在收到TCP报文时,会根据本次与上次接收的时间间隔来调整改制,在设置延迟确认定时器也会根据
							//条件修改该值
	__u32	tcpi_snd_mss;	// 本端的MSS
	__u32	tcpi_rcv_mss;	// 对端的MSS

	__u32	tcpi_unacked;	//未确认的数据段数
	__u32	tcpi_sacked;    //2个含义:server端在listen阶段,可以接收连接的数量;收到的SACK报文数量
	__u32	tcpi_lost;		//本端在发送出去被丢失的报文数。重传完成后清零
	__u32	tcpi_retrans;   /* 重传且未确认的数据段数 */
	__u32	tcpi_fackets;

	/* Times. */
	__u32	tcpi_last_data_sent;	//当前时间-最近一个包的发送时间,单位是毫秒
	__u32	tcpi_last_ack_sent;     /* 未使用*/
	__u32	tcpi_last_data_recv;	//当前时间-最近接收数据包的时间,单位是毫秒
	__u32	tcpi_last_ack_recv;     //当前时间-最近接收ack的时间,单位是毫秒

	/* Metrics. */
	__u32	tcpi_pmtu;			/* 最后一次更新的路径MTU */
	__u32	tcpi_rcv_ssthresh;   //当前接收窗口的大小
	__u32	tcpi_rtt;			//smoothed round trip time,微妙	
	__u32	tcpi_rttvar;		//描述RTT的平均偏差,该值越大,说明RTT抖动越大
	__u32	tcpi_snd_ssthresh;  //拥塞控制慢开始阈值
	__u32	tcpi_snd_cwnd;		//拥塞控制窗口大小
	__u32	tcpi_advmss;		//本端的MSS上限
	__u32	tcpi_reordering;	/* 没有丢包时,可以重新排序的数据段数 */

	__u32	tcpi_rcv_rtt;		// 作为接收端,测出的RTT值,单位为微秒. 这个值不是对方计算并传送过来的rtt,而是作为接收端,在没发送数据的情况下
								// 通过接收发送端发送的数据的情况计算得到的rtt值。在数据发送方,如果不接受数据,这个值一般情况下为0。
	__u32	tcpi_rcv_space;		/* 当前接收缓存的大小 */

	__u32	tcpi_total_retrans;  //统计总重传的包数,持续增长。

	__u64	tcpi_pacing_rate;		//发送速率
	__u64	tcpi_max_pacing_rate;	//最大发送速率,默认是unlimited,可以通过SO_MAX_PACING_RATE来设置
	__u64	tcpi_bytes_acked;    /* RFC4898 tcpEStatsAppHCThruOctetsAcked */
	__u64	tcpi_bytes_received; /* RFC4898 tcpEStatsAppHCThruOctetsReceived */
	__u32	tcpi_segs_out;	     /* RFC4898 tcpEStatsPerfSegsOut */
	__u32	tcpi_segs_in;	     /* RFC4898 tcpEStatsPerfSegsIn */

	__u32	tcpi_notsent_bytes;
	__u32	tcpi_min_rtt;
	__u32	tcpi_data_segs_in;	/* RFC4898 tcpEStatsDataSegsIn */
	__u32	tcpi_data_segs_out;	/* RFC4898 tcpEStatsDataSegsOut */

	__u64   tcpi_delivery_rate;

	__u64	tcpi_busy_time;      /* Time (usec) busy sending data */
	__u64	tcpi_rwnd_limited;   /* Time (usec) limited by receive window */
	__u64	tcpi_sndbuf_limited; /* Time (usec) limited by send buffer */
};
原文地址:https://www.cnblogs.com/DXGG-Bond/p/14767413.html