Apollo  6.0
Open source self driving car software
session.h
Go to the documentation of this file.
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 
27 #include "cyber/io/poll_handler.h"
28 
29 namespace apollo {
30 namespace cyber {
31 namespace io {
32 
33 class 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_
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
std::unique_ptr< PollHandler > PollHandlerPtr
Definition: session.h:36
ssize_t Read(void *buf, size_t count, int timeout_ms=-1)
int Listen(int backlog)
SessionPtr Accept(struct sockaddr *addr, socklen_t *addrlen)
virtual ~Session()=default
ssize_t RecvFrom(void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen, 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)
int Socket(int domain, int type, int protocol)
ssize_t Write(const void *buf, size_t count, int timeout_ms=-1)
ssize_t Send(const void *buf, size_t len, int flags, int timeout_ms=-1)
std::shared_ptr< Session > SessionPtr
Definition: session.h:35
Definition: session.h:33
ssize_t Recv(void *buf, size_t len, int flags, int timeout_ms=-1)
int Connect(const struct sockaddr *addr, socklen_t addrlen)
int fd() const
Definition: session.h:64
int Bind(const struct sockaddr *addr, socklen_t addrlen)