问题
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
TcpConnection class是muduo里唯一默认使用shared_ptr来管理的class,也是唯一继承enable_shared_from_this的class
。TcpConnection一旦连接断开,这个TcpConnection对象就没啥用了。它没有发起连接的功能
,其构造函数的参数是已经建立好连接的socket fd。
TcpConnection使用Channel来获得socket上的IO事件,构造TcpConnection对象的时候,Channel注册可读,可写,错误,关闭事件。
他会自己处理writable事件,而把readable事件通过MessageCallback传达给客户。TcpConnection拥有TCP socket,它的析构函数会closed(fd)。
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as:
a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example 1:
Given the following tree [3,9,20,null,null,15,7]:
3
/ \
9 20
/ \
15 7
Return true.
Example 2:
Given the following tree [1,2,2,3,3,null,null,4,4]:
1
/ \
2 2
/ \
3 3
/ \
4 4
Return false.
TCP网络编程最本质是的处理三个半事件
连接的建立
,包括服务端接受(accept)新连接和客户端成功发起(connect)连接。TCP 连接一旦建立,客户端和服务端是平等的,可以各自收发数据。
连接的断开
,包括主动断开(close或shutdown)和被动断开(read返回0)。
消息到达
,文件描述符可读。这是最为重要的一个事件,对它的处理方式决定了网络编程的风格(阻塞还是非阻塞,如何处理分包,应用层的缓冲如何设计等等)。
消息发送完毕
,这算半个。对于低流量的服务,可以不必关心这个事件;另外,这里“发送完毕”是指将数据写入操作系统的缓冲区,将由TCP协议栈负责数据的发送与重传,不代表对方已经收到数据。
TcpServer供用户直接使用
,生命期由用户控制。用户只需设置好callback,再调用start即可。
TcpServer内部使用Acceptor来获取新连接的fd
。TcpServer会为新连接创建对应的TcpConnection对象。
在TcpServer构造函数中先初始化acceptor成员,acceptor(new Acceptor(loop, listenAddr))
// Acceptor::handleRead函数中会回调用TcpServer::newConnection
// _1对应的是socket文件描述符,_2对应的是对等方的地址(InetAddress)
acceptor_->setNewConnectionCallback(
boost::bind(&TcpServer::newConnection, this, _1, _2));
Reactor模式是事件驱动模型的一个实现。它是一个同步的按照事件到达顺序处理事件的分发器。简单的说就是有一个线程不断轮询事件源,然后将其分发给对应的处理器
。
餐厅点菜的Reactor模式图