bump version numbers to 1.7.0 and updated changelog
[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 *
9b53a148 5 * libCEC(R) is Copyright (C) 2011-2012 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
ba65909d
LOK
34#include "../threads/mutex.h"
35#include "../util/StdString.h"
36
37#if defined(__WINDOWS__)
38#include "../windows/os-socket.h"
39#else
40#include "../posix/os-socket.h"
41#endif
42
43// Common socket operations
abbca718 44
f00ff009 45namespace PLATFORM
b9187cc6 46{
99666519 47 class ISocket : public PreventCopy
ba65909d 48 {
99666519
LOK
49 public:
50 ISocket(void) {};
51 virtual ~ISocket(void) {}
cb2397e9 52
99666519
LOK
53 virtual bool Open(uint64_t iTimeoutMs = 0) = 0;
54 virtual void Close(void) = 0;
55 virtual void Shutdown(void) = 0;
56 virtual bool IsOpen(void) = 0;
57 virtual ssize_t Write(void* data, size_t len) = 0;
58 virtual ssize_t Read(void* data, size_t len, uint64_t iTimeoutMs = 0) = 0;
59 virtual CStdString GetError(void) = 0;
60 virtual int GetErrorNumber(void) = 0;
61 virtual CStdString GetName(void) = 0;
62 };
25701fa6 63
99666519
LOK
64 template <typename _SType>
65 class CCommonSocket : public ISocket
66 {
67 public:
68 CCommonSocket(_SType initialSocketValue, const CStdString &strName) :
69 m_socket(initialSocketValue),
70 m_strName(strName),
71 m_iError(0) {}
25701fa6 72
99666519 73 virtual ~CCommonSocket(void) {}
abbca718 74
99666519
LOK
75 virtual CStdString GetError(void)
76 {
77 CStdString strError;
78 strError = m_strError.IsEmpty() && m_iError != 0 ? strerror(m_iError) : m_strError;
79 return strError;
80 }
81
82 virtual int GetErrorNumber(void)
83 {
84 return m_iError;
85 }
86
87 virtual CStdString GetName(void)
88 {
89 CStdString strName;
90 strName = m_strName;
91 return strName;
92 }
93
94 protected:
95 _SType m_socket;
96 CStdString m_strError;
97 CStdString m_strName;
98 int m_iError;
99 CMutex m_mutex;
100 };
101
102 template <typename _Socket>
103 class CProtectedSocket : public ISocket
104 {
105 public:
106 CProtectedSocket(_Socket *socket) :
107 m_socket(socket),
960f33c6 108 m_bIsIdle(true) {}
99666519
LOK
109
110 virtual ~CProtectedSocket(void)
111 {
112 Close();
113 delete m_socket;
114 }
abbca718 115
99666519
LOK
116 virtual bool Open(uint64_t iTimeoutMs = 0)
117 {
118 bool bReturn(false);
119 if (m_socket && WaitReady())
abbca718 120 {
99666519
LOK
121 bReturn = m_socket->Open(iTimeoutMs);
122 MarkReady();
ba65909d 123 }
99666519
LOK
124 return bReturn;
125 }
ba65909d 126
99666519
LOK
127 virtual void Close(void)
128 {
129 if (m_socket && WaitReady())
ba65909d 130 {
99666519
LOK
131 m_socket->Close();
132 MarkReady();
abbca718 133 }
99666519 134 }
abbca718 135
99666519
LOK
136 virtual void Shutdown(void)
137 {
138 if (m_socket && WaitReady())
cb2397e9 139 {
99666519
LOK
140 m_socket->Shutdown();
141 MarkReady();
cb2397e9 142 }
99666519
LOK
143 }
144
145 virtual bool IsOpen(void)
146 {
147 CLockObject lock(m_mutex);
148 return m_socket && m_socket->IsOpen();
149 }
150
151 virtual bool IsBusy(void)
152 {
153 CLockObject lock(m_mutex);
960f33c6 154 return m_socket && !m_bIsIdle;
99666519
LOK
155 }
156
960f33c6 157 virtual bool IsIdle(void)
99666519
LOK
158 {
159 CLockObject lock(m_mutex);
960f33c6 160 return m_socket && m_bIsIdle;
99666519
LOK
161 }
162
163 virtual ssize_t Write(void* data, size_t len)
164 {
165 if (!m_socket || !WaitReady())
15d01ae0 166 return -EINVAL;
99666519
LOK
167
168 ssize_t iReturn = m_socket->Write(data, len);
169 MarkReady();
170
171 return iReturn;
172 }
173
174 virtual ssize_t Read(void* data, size_t len, uint64_t iTimeoutMs = 0)
175 {
176 if (!m_socket || !WaitReady())
15d01ae0 177 return -EINVAL;
99666519
LOK
178
179 ssize_t iReturn = m_socket->Read(data, len, iTimeoutMs);
180 MarkReady();
181
182 return iReturn;
183 }
184
185 virtual CStdString GetError(void)
186 {
187 CStdString strError;
188 CLockObject lock(m_mutex);
189 strError = m_socket ? m_socket->GetError() : "";
190 return strError;
191 }
192
193 virtual int GetErrorNumber(void)
194 {
195 CLockObject lock(m_mutex);
15d01ae0 196 return m_socket ? m_socket->GetErrorNumber() : -EINVAL;
99666519
LOK
197 }
198
199 virtual CStdString GetName(void)
200 {
201 CStdString strName;
202 CLockObject lock(m_mutex);
203 strName = m_socket ? m_socket->GetName() : "";
204 return strName;
205 }
206
207 private:
208 bool WaitReady(void)
209 {
210 CLockObject lock(m_mutex);
960f33c6
LOK
211 m_condition.Wait(m_mutex, m_bIsIdle);
212 m_bIsIdle = false;
99666519
LOK
213 return true;
214 }
215
216 void MarkReady(void)
217 {
218 CLockObject lock(m_mutex);
960f33c6 219 m_bIsIdle = true;
008a816c 220 m_condition.Signal();
99666519 221 }
cb2397e9 222
dfc47c76
LOK
223 _Socket * m_socket;
224 CMutex m_mutex;
225 CCondition<bool> m_condition;
226 bool m_bIsIdle;
ba65909d 227 };
b9187cc6 228};