Merge PR #29
[deb_libcec.git] / src / lib / platform / sockets / socket.h
1 #pragma once
2 /*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
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
34 #include "lib/platform/threads/mutex.h"
35
36 #if defined(__WINDOWS__)
37 #include "lib/platform/windows/os-socket.h"
38 #else
39 #include "lib/platform/posix/os-socket.h"
40 #endif
41
42 #include <string>
43
44 // Common socket operations
45
46 namespace PLATFORM
47 {
48 class ISocket : public PreventCopy
49 {
50 public:
51 ISocket(void) {};
52 virtual ~ISocket(void) {}
53
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;
60 virtual std::string GetError(void) = 0;
61 virtual int GetErrorNumber(void) = 0;
62 virtual std::string GetName(void) = 0;
63 };
64
65 template <typename _SType>
66 class CCommonSocket : public ISocket
67 {
68 public:
69 CCommonSocket(_SType initialSocketValue, const std::string &strName) :
70 m_socket(initialSocketValue),
71 m_strName(strName),
72 m_iError(0) {}
73
74 virtual ~CCommonSocket(void) {}
75
76 virtual std::string GetError(void)
77 {
78 std::string strError;
79 strError = m_strError.empty() && m_iError != 0 ? strerror(m_iError) : m_strError;
80 return strError;
81 }
82
83 virtual int GetErrorNumber(void)
84 {
85 return m_iError;
86 }
87
88 virtual std::string GetName(void)
89 {
90 std::string strName;
91 strName = m_strName;
92 return strName;
93 }
94
95 protected:
96 _SType m_socket;
97 std::string m_strError;
98 std::string m_strName;
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),
109 m_bIsIdle(true) {}
110
111 virtual ~CProtectedSocket(void)
112 {
113 Close();
114 delete m_socket;
115 }
116
117 virtual bool Open(uint64_t iTimeoutMs = 0)
118 {
119 bool bReturn(false);
120 if (m_socket && WaitReady())
121 {
122 bReturn = m_socket->Open(iTimeoutMs);
123 MarkReady();
124 }
125 return bReturn;
126 }
127
128 virtual void Close(void)
129 {
130 if (m_socket && WaitReady())
131 {
132 m_socket->Close();
133 MarkReady();
134 }
135 }
136
137 virtual void Shutdown(void)
138 {
139 if (m_socket && WaitReady())
140 {
141 m_socket->Shutdown();
142 MarkReady();
143 }
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);
155 return m_socket && !m_bIsIdle;
156 }
157
158 virtual bool IsIdle(void)
159 {
160 CLockObject lock(m_mutex);
161 return m_socket && m_bIsIdle;
162 }
163
164 virtual ssize_t Write(void* data, size_t len)
165 {
166 if (!m_socket || !WaitReady())
167 return -EINVAL;
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())
178 return -EINVAL;
179
180 ssize_t iReturn = m_socket->Read(data, len, iTimeoutMs);
181 MarkReady();
182
183 return iReturn;
184 }
185
186 virtual std::string GetError(void)
187 {
188 std::string strError;
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);
197 return m_socket ? m_socket->GetErrorNumber() : -EINVAL;
198 }
199
200 virtual std::string GetName(void)
201 {
202 std::string strName;
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);
212 m_condition.Wait(m_mutex, m_bIsIdle);
213 m_bIsIdle = false;
214 return true;
215 }
216
217 void MarkReady(void)
218 {
219 CLockObject lock(m_mutex);
220 m_bIsIdle = true;
221 m_condition.Signal();
222 }
223
224 _Socket * m_socket;
225 CMutex m_mutex;
226 CCondition<bool> m_condition;
227 bool m_bIsIdle;
228 };
229 };