Apollo 10.0
自动驾驶开放平台
apollo::cyber::io::Session类 参考

#include <session.h>

apollo::cyber::io::Session 的协作图:

Public 类型

using SessionPtr = std::shared_ptr< Session >
 
using PollHandlerPtr = std::unique_ptr< PollHandler >
 

Public 成员函数

 Session ()
 
 Session (int fd)
 
virtual ~Session ()=default
 
int Socket (int domain, int type, int protocol)
 
int Listen (int backlog)
 
int Bind (const struct sockaddr *addr, socklen_t addrlen)
 
SessionPtr Accept (struct sockaddr *addr, socklen_t *addrlen)
 
int Connect (const struct sockaddr *addr, socklen_t addrlen)
 
int Close ()
 
ssize_t Recv (void *buf, size_t len, int flags, int timeout_ms=-1)
 
ssize_t RecvFrom (void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen, int timeout_ms=-1)
 
ssize_t Send (const void *buf, size_t len, int flags, int timeout_ms=-1)
 
ssize_t SendTo (const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen, int timeout_ms=-1)
 
ssize_t Read (void *buf, size_t count, int timeout_ms=-1)
 
ssize_t Write (const void *buf, size_t count, int timeout_ms=-1)
 
int fd () const
 

详细描述

在文件 session.h33 行定义.

成员类型定义说明

◆ PollHandlerPtr

在文件 session.h36 行定义.

◆ SessionPtr

在文件 session.h35 行定义.

构造及析构函数说明

◆ Session() [1/2]

apollo::cyber::io::Session::Session ( )

在文件 session.cc25 行定义.

◆ Session() [2/2]

apollo::cyber::io::Session::Session ( int  fd)
explicit

在文件 session.cc27 行定义.

27 : fd_(fd), poll_handler_(nullptr) {
28 poll_handler_.reset(new PollHandler(fd_));
29}

◆ ~Session()

virtual apollo::cyber::io::Session::~Session ( )
virtualdefault

成员函数说明

◆ Accept()

auto apollo::cyber::io::Session::Accept ( struct sockaddr *  addr,
socklen_t *  addrlen 
)

在文件 session.cc54 行定义.

54 {
55 ACHECK(fd_ != -1);
56
57 int sock_fd = accept4(fd_, addr, addrlen, SOCK_NONBLOCK);
58 while (sock_fd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
59 poll_handler_->Block(-1, true);
60 sock_fd = accept4(fd_, addr, addrlen, SOCK_NONBLOCK);
61 }
62
63 if (sock_fd == -1) {
64 return nullptr;
65 }
66
67 return std::make_shared<Session>(sock_fd);
68}
#define ACHECK(cond)
Definition log.h:80

◆ Bind()

int apollo::cyber::io::Session::Bind ( const struct sockaddr *  addr,
socklen_t  addrlen 
)

在文件 session.cc48 行定义.

48 {
49 ACHECK(fd_ != -1);
50 ACHECK(addr != nullptr);
51 return bind(fd_, addr, addrlen);
52}

◆ Close()

int apollo::cyber::io::Session::Close ( )

在文件 session.cc89 行定义.

89 {
90 ACHECK(fd_ != -1);
91
92 poll_handler_->Unblock();
93 int res = close(fd_);
94 fd_ = -1;
95 return res;
96}

◆ Connect()

int apollo::cyber::io::Session::Connect ( const struct sockaddr *  addr,
socklen_t  addrlen 
)

在文件 session.cc70 行定义.

70 {
71 ACHECK(fd_ != -1);
72
73 int optval;
74 socklen_t optlen = sizeof(optval);
75 int res = connect(fd_, addr, addrlen);
76 if (res == -1 && errno == EINPROGRESS) {
77 poll_handler_->Block(-1, false);
78 getsockopt(fd_, SOL_SOCKET, SO_ERROR, reinterpret_cast<void *>(&optval),
79 &optlen);
80 if (optval == 0) {
81 res = 0;
82 } else {
83 errno = optval;
84 }
85 }
86 return res;
87}

◆ fd()

int apollo::cyber::io::Session::fd ( ) const
inline

在文件 session.h64 行定义.

64{ return fd_; }

◆ Listen()

int apollo::cyber::io::Session::Listen ( int  backlog)

在文件 session.cc43 行定义.

43 {
44 ACHECK(fd_ != -1);
45 return listen(fd_, backlog);
46}

◆ Read()

ssize_t apollo::cyber::io::Session::Read ( void *  buf,
size_t  count,
int  timeout_ms = -1 
)

在文件 session.cc183 行定义.

183 {
184 ACHECK(buf != nullptr);
185 ACHECK(fd_ != -1);
186
187 ssize_t nbytes = read(fd_, buf, count);
188 if (timeout_ms == 0) {
189 return nbytes;
190 }
191
192 while ((nbytes == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)) {
193 if (poll_handler_->Block(timeout_ms, true)) {
194 nbytes = read(fd_, buf, count);
195 }
196 if (timeout_ms > 0) {
197 break;
198 }
199 }
200 return nbytes;
201}

◆ Recv()

ssize_t apollo::cyber::io::Session::Recv ( void *  buf,
size_t  len,
int  flags,
int  timeout_ms = -1 
)

在文件 session.cc98 行定义.

98 {
99 ACHECK(buf != nullptr);
100 ACHECK(fd_ != -1);
101
102 ssize_t nbytes = recv(fd_, buf, len, flags);
103 if (timeout_ms == 0) {
104 return nbytes;
105 }
106
107 while (nbytes == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
108 if (poll_handler_->Block(timeout_ms, true)) {
109 nbytes = recv(fd_, buf, len, flags);
110 }
111 if (timeout_ms > 0) {
112 break;
113 }
114 }
115 return nbytes;
116}

◆ RecvFrom()

ssize_t apollo::cyber::io::Session::RecvFrom ( void *  buf,
size_t  len,
int  flags,
struct sockaddr *  src_addr,
socklen_t *  addrlen,
int  timeout_ms = -1 
)

在文件 session.cc118 行定义.

120 {
121 ACHECK(buf != nullptr);
122 ACHECK(fd_ != -1);
123
124 ssize_t nbytes = recvfrom(fd_, buf, len, flags, src_addr, addrlen);
125 if (timeout_ms == 0) {
126 return nbytes;
127 }
128
129 while (nbytes == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
130 if (poll_handler_->Block(timeout_ms, true)) {
131 nbytes = recvfrom(fd_, buf, len, flags, src_addr, addrlen);
132 }
133 if (timeout_ms > 0) {
134 break;
135 }
136 }
137 return nbytes;
138}

◆ Send()

ssize_t apollo::cyber::io::Session::Send ( const void *  buf,
size_t  len,
int  flags,
int  timeout_ms = -1 
)

在文件 session.cc140 行定义.

140 {
141 ACHECK(buf != nullptr);
142 ACHECK(fd_ != -1);
143
144 ssize_t nbytes = send(fd_, buf, len, flags);
145 if (timeout_ms == 0) {
146 return nbytes;
147 }
148
149 while ((nbytes == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)) {
150 if (poll_handler_->Block(timeout_ms, false)) {
151 nbytes = send(fd_, buf, len, flags);
152 }
153 if (timeout_ms > 0) {
154 break;
155 }
156 }
157 return nbytes;
158}

◆ SendTo()

ssize_t apollo::cyber::io::Session::SendTo ( const void *  buf,
size_t  len,
int  flags,
const struct sockaddr *  dest_addr,
socklen_t  addrlen,
int  timeout_ms = -1 
)

在文件 session.cc160 行定义.

162 {
163 ACHECK(buf != nullptr);
164 ACHECK(dest_addr != nullptr);
165 ACHECK(fd_ != -1);
166
167 ssize_t nbytes = sendto(fd_, buf, len, flags, dest_addr, addrlen);
168 if (timeout_ms == 0) {
169 return nbytes;
170 }
171
172 while ((nbytes == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)) {
173 if (poll_handler_->Block(timeout_ms, false)) {
174 nbytes = sendto(fd_, buf, len, flags, dest_addr, addrlen);
175 }
176 if (timeout_ms > 0) {
177 break;
178 }
179 }
180 return nbytes;
181}

◆ Socket()

int apollo::cyber::io::Session::Socket ( int  domain,
int  type,
int  protocol 
)

在文件 session.cc31 行定义.

31 {
32 if (fd_ != -1) {
33 AINFO << "session has hold a valid fd[" << fd_ << "]";
34 return -1;
35 }
36 int sock_fd = socket(domain, type | SOCK_NONBLOCK, protocol);
37 if (sock_fd != -1) {
38 set_fd(sock_fd);
39 }
40 return sock_fd;
41}
#define AINFO
Definition log.h:42

◆ Write()

ssize_t apollo::cyber::io::Session::Write ( const void *  buf,
size_t  count,
int  timeout_ms = -1 
)

在文件 session.cc203 行定义.

203 {
204 ACHECK(buf != nullptr);
205 ACHECK(fd_ != -1);
206
207 ssize_t nbytes = write(fd_, buf, count);
208 if (timeout_ms == 0) {
209 return nbytes;
210 }
211
212 while ((nbytes == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)) {
213 if (poll_handler_->Block(timeout_ms, false)) {
214 nbytes = write(fd_, buf, count);
215 }
216 if (timeout_ms > 0) {
217 break;
218 }
219 }
220 return nbytes;
221}

该类的文档由以下文件生成: