summaryrefslogtreecommitdiff
path: root/tools/gator/daemon/OlySocket.h
blob: 3f8baa6f7e4c285b63abfff5e1fa13bfba00398c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
 * Copyright (C) ARM Limited 2010-2016. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#ifndef __OLY_SOCKET_H__
#define __OLY_SOCKET_H__

#include <stddef.h>

#ifdef WIN32
typedef int socklen_t;
#else
#include <sys/socket.h>
#endif

#include "Config.h"

class OlySocket {
public:
#ifndef WIN32
  static int connect(const char* path, const size_t pathSize, const bool calculateAddrlen = false);
#endif

  OlySocket(int socketID);
  ~OlySocket();

  void closeSocket();
  void shutdownConnection();
  void send(const char* buffer, int size);
  int receive(char* buffer, int size);
  int receiveNBytes(char* buffer, int size);
  int receiveString(char* buffer, int size);

  bool isValid() const { return mSocketID >= 0; }

private:
  int mSocketID;
};

class OlyServerSocket {
public:
  OlyServerSocket(int port);
#ifndef WIN32
  OlyServerSocket(const char* path, const size_t pathSize, const bool calculateAddrlen = false);
#endif
  ~OlyServerSocket();

  int acceptConnection();
  void closeServerSocket();

  int getFd() { return mFDServer; }

private:
  int mFDServer;

  void createServerSocket(int port);
};

int socket_cloexec(int domain, int type, int protocol);
int accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

#endif //__OLY_SOCKET_H__