Apollo 10.0
自动驾驶开放平台
session.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#ifndef CYBER_IO_SESSION_H_
18#define CYBER_IO_SESSION_H_
19
20#include <fcntl.h>
21#include <sys/socket.h>
22#include <sys/types.h>
23#include <unistd.h>
24
25#include <memory>
26
28
29namespace apollo {
30namespace cyber {
31namespace io {
32
33class Session {
34 public:
35 using SessionPtr = std::shared_ptr<Session>;
36 using PollHandlerPtr = std::unique_ptr<PollHandler>;
37
38 Session();
39 explicit Session(int fd);
40 virtual ~Session() = default;
41
42 int Socket(int domain, int type, int protocol);
43 int Listen(int backlog);
44 int Bind(const struct sockaddr *addr, socklen_t addrlen);
45 SessionPtr Accept(struct sockaddr *addr, socklen_t *addrlen);
46 int Connect(const struct sockaddr *addr, socklen_t addrlen);
47 int Close();
48
49 // timeout_ms < 0, keep trying until the operation is successfully
50 // timeout_ms == 0, try once
51 // timeout_ms > 0, keep trying while there is still time left
52 ssize_t Recv(void *buf, size_t len, int flags, int timeout_ms = -1);
53 ssize_t RecvFrom(void *buf, size_t len, int flags, struct sockaddr *src_addr,
54 socklen_t *addrlen, int timeout_ms = -1);
55
56 ssize_t Send(const void *buf, size_t len, int flags, int timeout_ms = -1);
57 ssize_t SendTo(const void *buf, size_t len, int flags,
58 const struct sockaddr *dest_addr, socklen_t addrlen,
59 int timeout_ms = -1);
60
61 ssize_t Read(void *buf, size_t count, int timeout_ms = -1);
62 ssize_t Write(const void *buf, size_t count, int timeout_ms = -1);
63
64 int fd() const { return fd_; }
65
66 private:
67 void set_fd(int fd) {
68 fd_ = fd;
69 poll_handler_->set_fd(fd);
70 }
71
72 int fd_;
73 PollHandlerPtr poll_handler_;
74};
75
76} // namespace io
77} // namespace cyber
78} // namespace apollo
79
80#endif // CYBER_IO_SESSION_H_
ssize_t RecvFrom(void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen, int timeout_ms=-1)
Definition session.cc:118
ssize_t Write(const void *buf, size_t count, int timeout_ms=-1)
Definition session.cc:203
std::shared_ptr< Session > SessionPtr
Definition session.h:35
std::unique_ptr< PollHandler > PollHandlerPtr
Definition session.h:36
ssize_t SendTo(const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen, int timeout_ms=-1)
Definition session.cc:160
int Listen(int backlog)
Definition session.cc:43
virtual ~Session()=default
int Bind(const struct sockaddr *addr, socklen_t addrlen)
Definition session.cc:48
ssize_t Recv(void *buf, size_t len, int flags, int timeout_ms=-1)
Definition session.cc:98
ssize_t Read(void *buf, size_t count, int timeout_ms=-1)
Definition session.cc:183
SessionPtr Accept(struct sockaddr *addr, socklen_t *addrlen)
Definition session.cc:54
ssize_t Send(const void *buf, size_t len, int flags, int timeout_ms=-1)
Definition session.cc:140
int Connect(const struct sockaddr *addr, socklen_t addrlen)
Definition session.cc:70
int Socket(int domain, int type, int protocol)
Definition session.cc:31
class register implement
Definition arena_queue.h:37