blob: 757a2522f9262e237e621c9295b6342200c8cda4 [file] [log] [blame]
Jon Medhurstaaf37a32013-06-11 12:10:56 +01001/**
Jon Medhurstb1d07442015-05-08 12:04:18 +01002 * Copyright (C) ARM Limited 2010-2015. All rights reserved.
Jon Medhurstaaf37a32013-06-11 12:10:56 +01003 *
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
Jon Medhurste31266f2014-08-04 15:47:44 +010012#include <stddef.h>
13
Jon Medhurst96b56152014-10-30 18:01:15 +000014#ifdef WIN32
Jon Medhurstb1d07442015-05-08 12:04:18 +010015typedef int socklen_t;
Jon Medhurst96b56152014-10-30 18:01:15 +000016#else
17#include <sys/socket.h>
18#endif
19
Jon Medhurstaaf37a32013-06-11 12:10:56 +010020class OlySocket {
21public:
Jon Medhurst15ce78d2014-04-10 09:02:02 +010022#ifndef WIN32
Jon Medhurstb1d07442015-05-08 12:04:18 +010023 static int connect(const char* path, const size_t pathSize, const bool calculateAddrlen = false);
Jon Medhurst15ce78d2014-04-10 09:02:02 +010024#endif
Jon Medhurste31266f2014-08-04 15:47:44 +010025
26 OlySocket(int socketID);
Jon Medhurstaaf37a32013-06-11 12:10:56 +010027 ~OlySocket();
Jon Medhurst15ce78d2014-04-10 09:02:02 +010028
Jon Medhurstaaf37a32013-06-11 12:10:56 +010029 void closeSocket();
Jon Medhurstaaf37a32013-06-11 12:10:56 +010030 void shutdownConnection();
Jon Medhurst15ce78d2014-04-10 09:02:02 +010031 void send(const char* buffer, int size);
Jon Medhurstaaf37a32013-06-11 12:10:56 +010032 int receive(char* buffer, int size);
33 int receiveNBytes(char* buffer, int size);
34 int receiveString(char* buffer, int size);
Jon Medhurst15ce78d2014-04-10 09:02:02 +010035
36 bool isValid() const { return mSocketID >= 0; }
37
Jon Medhurstaaf37a32013-06-11 12:10:56 +010038private:
Jon Medhurst15ce78d2014-04-10 09:02:02 +010039 int mSocketID;
Jon Medhurst15ce78d2014-04-10 09:02:02 +010040};
41
42class OlyServerSocket {
43public:
44 OlyServerSocket(int port);
45#ifndef WIN32
Jon Medhurstb1d07442015-05-08 12:04:18 +010046 OlyServerSocket(const char* path, const size_t pathSize, const bool calculateAddrlen = false);
Jon Medhurst15ce78d2014-04-10 09:02:02 +010047#endif
48 ~OlyServerSocket();
49
50 int acceptConnection();
51 void closeServerSocket();
52
Jon Medhurste31266f2014-08-04 15:47:44 +010053 int getFd() { return mFDServer; }
54
Jon Medhurst15ce78d2014-04-10 09:02:02 +010055private:
56 int mFDServer;
57
Jon Medhurstaaf37a32013-06-11 12:10:56 +010058 void createServerSocket(int port);
59};
60
Jon Medhurst96b56152014-10-30 18:01:15 +000061int socket_cloexec(int domain, int type, int protocol);
62int accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
63
Jon Medhurstaaf37a32013-06-11 12:10:56 +010064#endif //__OLY_SOCKET_H__