• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright (C) 2018 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef CFP_CLIENT_SOCKET_
18 #define CFP_CLIENT_SOCKET_
19 
20 #include <arpa/inet.h>
21 #include <netinet/in.h>
22 #include <sys/socket.h>
23 #include <unistd.h>
24 
25 #include <cstdint>
26 #include <cstring>
27 #include <iostream>
28 #include <vector>
29 
30 namespace cfp {
31 
32 class ClientSocket {
33  public:
34   ClientSocket(std::uint16_t port);
35   ~ClientSocket();
36 
37   ClientSocket(ClientSocket&& other);
38   ClientSocket& operator=(ClientSocket&& other);
39 
40   ClientSocket(const ClientSocket&) = delete;
41   ClientSocket& operator=(const ClientSocket&) = delete;
42 
valid()43   bool valid() const { return socket_fd_ >= 0; }
44 
45   std::vector<unsigned char> RecvAll(ssize_t count);
46 
47   std::uint16_t RecvUInt16();
48   std::uint32_t RecvUInt32();
49 
50  private:
51   void close();
52   int socket_fd_ = -1;
53 };
54 
55 }  // namespace cfp
56 
57 #endif
58