Merge PR #29
[deb_libcec.git] / src / lib / platform / sockets / socket.h
CommitLineData
abbca718
LOK
1#pragma once
2/*
3 * This file is part of the libCEC(R) library.
4 *
16f47961 5 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
abbca718
LOK
6 * libCEC(R) is an original work, containing original code.
7 *
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 *
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 *
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
27 *
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
32 */
33
2b44051c 34#include "lib/platform/threads/mutex.h"
ba65909d
LOK
35
36#if defined(__WINDOWS__)
2b44051c 37#include "lib/platform/windows/os-socket.h"
ba65909d 38#else
2b44051c 39#include "lib/platform/posix/os-socket.h"
ba65909d
LOK
40#endif
41
2b44051c
LOK
42#include <string>
43
ba65909d 44// Common socket operations
abbca718 45
f00ff009 46namespace PLATFORM
b9187cc6 47{
99666519 48 class ISocket : public PreventCopy
ba65909d 49 {
99666519
LOK
50 public:
51 ISocket(void) {};
52 virtual ~ISocket(void) {}
cb2397e9 53
99666519
LOK
54 virtual bool Open(uint64_t iTimeoutMs = 0) = 0;
55 virtual void Close(void) = 0;
56 virtual void Shutdown(void) = 0;
57 virtual bool IsOpen(void) = 0;
58 virtual ssize_t Write(void* data, size_t len) = 0;
59 virtual ssize_t Read(void* data, size_t len, uint64_t iTimeoutMs = 0) = 0;
2b44051c 60 virtual std::string GetError(void) = 0;
99666519 61 virtual int GetErrorNumber(void) = 0;
2b44051c 62 virtual std::string GetName(void) = 0;
99666519 63 };
25701fa6 64
99666519
LOK
65 template <typename _SType>
66 class CCommonSocket : public ISocket
67 {
68 public:
2b44051c 69 CCommonSocket(_SType initialSocketValue, const std::string &strName) :
99666519
LOK
70 m_socket(initialSocketValue),
71 m_strName(strName),
72 m_iError(0) {}
25701fa6 73
99666519 74 virtual ~CCommonSocket(void) {}
abbca718 75
2b44051c 76 virtual std::string GetError(void)
99666519 77 {
2b44051c
LOK
78 std::string strError;
79 strError = m_strError.empty() && m_iError != 0 ? strerror(m_iError) : m_strError;
99666519
LOK
80 return strError;
81 }
82
83 virtual int GetErrorNumber(void)
84 {
85 return m_iError;
86 }
87
2b44051c 88 virtual std::string GetName(void)
99666519 89 {
2b44051c 90 std::string strName;
99666519
LOK
91 strName = m_strName;
92 return strName;
93 }
94
95 protected:
96 _SType m_socket;
2b44051c
LOK
97 std::string m_strError;
98 std::string m_strName;
99666519
LOK
99 int m_iError;
100 CMutex m_mutex;
101 };
102
103 template <typename _Socket>
104 class CProtectedSocket : public ISocket
105 {
106 public:
107 CProtectedSocket(_Socket *socket) :
108 m_socket(socket),
960f33c6 109 m_bIsIdle(true) {}
99666519
LOK
110
111 virtual ~CProtectedSocket(void)
112 {
113 Close();
114 delete m_socket;
115 }
abbca718 116
99666519
LOK
117 virtual bool Open(uint64_t iTimeoutMs = 0)
118 {
119 bool bReturn(false);
120 if (m_socket && WaitReady())
abbca718 121 {
99666519
LOK
122 bReturn = m_socket->Open(iTimeoutMs);
123 MarkReady();
ba65909d 124 }
99666519
LOK
125 return bReturn;
126 }
ba65909d 127
99666519
LOK
128 virtual void Close(void)
129 {
130 if (m_socket && WaitReady())
ba65909d 131 {
99666519
LOK
132 m_socket->Close();
133 MarkReady();
abbca718 134 }
99666519 135 }
abbca718 136
99666519
LOK
137 virtual void Shutdown(void)
138 {
139 if (m_socket && WaitReady())
cb2397e9 140 {
99666519
LOK
141 m_socket->Shutdown();
142 MarkReady();
cb2397e9 143 }
99666519
LOK
144 }
145
146 virtual bool IsOpen(void)
147 {
148 CLockObject lock(m_mutex);
149 return m_socket && m_socket->IsOpen();
150 }
151
152 virtual bool IsBusy(void)
153 {
154 CLockObject lock(m_mutex);
960f33c6 155 return m_socket && !m_bIsIdle;
99666519
LOK
156 }
157
960f33c6 158 virtual bool IsIdle(void)
99666519
LOK
159 {
160 CLockObject lock(m_mutex);
960f33c6 161 return m_socket && m_bIsIdle;
99666519
LOK
162 }
163
164 virtual ssize_t Write(void* data, size_t len)
165 {
166 if (!m_socket || !WaitReady())
15d01ae0 167 return -EINVAL;
99666519
LOK
168
169 ssize_t iReturn = m_socket->Write(data, len);
170 MarkReady();
171
172 return iReturn;
173 }
174
175 virtual ssize_t Read(void* data, size_t len, uint64_t iTimeoutMs = 0)
176 {
177 if (!m_socket || !WaitReady())
15d01ae0 178 return -EINVAL;
99666519
LOK
179
180 ssize_t iReturn = m_socket->Read(data, len, iTimeoutMs);
181 MarkReady();
182
183 return iReturn;
184 }
185
2b44051c 186 virtual std::string GetError(void)
99666519 187 {
2b44051c 188 std::string strError;
99666519
LOK
189 CLockObject lock(m_mutex);
190 strError = m_socket ? m_socket->GetError() : "";
191 return strError;
192 }
193
194 virtual int GetErrorNumber(void)
195 {
196 CLockObject lock(m_mutex);
15d01ae0 197 return m_socket ? m_socket->GetErrorNumber() : -EINVAL;
99666519
LOK
198 }
199
2b44051c 200 virtual std::string GetName(void)
99666519 201 {
2b44051c 202 std::string strName;
99666519
LOK
203 CLockObject lock(m_mutex);
204 strName = m_socket ? m_socket->GetName() : "";
205 return strName;
206 }
207
208 private:
209 bool WaitReady(void)
210 {
211 CLockObject lock(m_mutex);
960f33c6
LOK
212 m_condition.Wait(m_mutex, m_bIsIdle);
213 m_bIsIdle = false;
99666519
LOK
214 return true;
215 }
216
217 void MarkReady(void)
218 {
219 CLockObject lock(m_mutex);
960f33c6 220 m_bIsIdle = true;
008a816c 221 m_condition.Signal();
99666519 222 }
cb2397e9 223
dfc47c76
LOK
224 _Socket * m_socket;
225 CMutex m_mutex;
226 CCondition<bool> m_condition;
227 bool m_bIsIdle;
ba65909d 228 };
b9187cc6 229};