gator: Version 5.21.1
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / OlySocket.h
1 /**
2  * Copyright (C) ARM Limited 2010-2015. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #ifndef __OLY_SOCKET_H__
10 #define __OLY_SOCKET_H__
11
12 #include <stddef.h>
13
14 #ifdef WIN32
15 typedef int socklen_t;
16 #else
17 #include <sys/socket.h>
18 #endif
19
20 class OlySocket {
21 public:
22 #ifndef WIN32
23   static int connect(const char* path, const size_t pathSize, const bool calculateAddrlen = false);
24 #endif
25
26   OlySocket(int socketID);
27   ~OlySocket();
28
29   void closeSocket();
30   void shutdownConnection();
31   void send(const char* buffer, int size);
32   int receive(char* buffer, int size);
33   int receiveNBytes(char* buffer, int size);
34   int receiveString(char* buffer, int size);
35
36   bool isValid() const { return mSocketID >= 0; }
37
38 private:
39   int mSocketID;
40 };
41
42 class OlyServerSocket {
43 public:
44   OlyServerSocket(int port);
45 #ifndef WIN32
46   OlyServerSocket(const char* path, const size_t pathSize, const bool calculateAddrlen = false);
47 #endif
48   ~OlyServerSocket();
49
50   int acceptConnection();
51   void closeServerSocket();
52
53   int getFd() { return mFDServer; }
54
55 private:
56   int mFDServer;
57
58   void createServerSocket(int port);
59 };
60
61 int socket_cloexec(int domain, int type, int protocol);
62 int accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
63
64 #endif //__OLY_SOCKET_H__