gosu 0.7.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (249) hide show
  1. data/COPYING.txt +29 -0
  2. data/Gosu/Async.hpp +48 -0
  3. data/Gosu/Audio.hpp +145 -0
  4. data/Gosu/AutoLink.hpp +16 -0
  5. data/Gosu/Bitmap.hpp +85 -0
  6. data/Gosu/ButtonsMac.hpp +114 -0
  7. data/Gosu/ButtonsWin.hpp +111 -0
  8. data/Gosu/ButtonsX.hpp +115 -0
  9. data/Gosu/Color.hpp +172 -0
  10. data/Gosu/Directories.hpp +36 -0
  11. data/Gosu/Font.hpp +59 -0
  12. data/Gosu/Fwd.hpp +31 -0
  13. data/Gosu/Gosu.hpp +26 -0
  14. data/Gosu/Graphics.hpp +86 -0
  15. data/Gosu/GraphicsBase.hpp +45 -0
  16. data/Gosu/IO.hpp +255 -0
  17. data/Gosu/Image.hpp +148 -0
  18. data/Gosu/ImageData.hpp +45 -0
  19. data/Gosu/Input.hpp +116 -0
  20. data/Gosu/Math.hpp +95 -0
  21. data/Gosu/Platform.hpp +61 -0
  22. data/Gosu/RotFlip.hpp +116 -0
  23. data/Gosu/Sockets.hpp +129 -0
  24. data/Gosu/Text.hpp +47 -0
  25. data/Gosu/TextInput.hpp +57 -0
  26. data/Gosu/Timing.hpp +16 -0
  27. data/Gosu/Utility.hpp +24 -0
  28. data/Gosu/WinUtility.hpp +76 -0
  29. data/Gosu/Window.hpp +84 -0
  30. data/GosuImpl/Async.cpp +37 -0
  31. data/GosuImpl/AudioFmod.cpp +417 -0
  32. data/GosuImpl/AudioSDL.cpp +255 -0
  33. data/GosuImpl/DirectoriesMac.mm +38 -0
  34. data/GosuImpl/DirectoriesUnix.cpp +48 -0
  35. data/GosuImpl/DirectoriesWin.cpp +42 -0
  36. data/GosuImpl/FileUnix.cpp +100 -0
  37. data/GosuImpl/FileWin.cpp +83 -0
  38. data/GosuImpl/Graphics/Bitmap.cpp +116 -0
  39. data/GosuImpl/Graphics/BitmapBMP.cpp +232 -0
  40. data/GosuImpl/Graphics/BitmapColorKey.cpp +39 -0
  41. data/GosuImpl/Graphics/BitmapPNG.cpp +276 -0
  42. data/GosuImpl/Graphics/BitmapUtils.cpp +67 -0
  43. data/GosuImpl/Graphics/BlockAllocator.cpp +127 -0
  44. data/GosuImpl/Graphics/BlockAllocator.hpp +34 -0
  45. data/GosuImpl/Graphics/Color.cpp +126 -0
  46. data/GosuImpl/Graphics/Common.hpp +21 -0
  47. data/GosuImpl/Graphics/DrawOp.hpp +154 -0
  48. data/GosuImpl/Graphics/Font.cpp +110 -0
  49. data/GosuImpl/Graphics/Graphics.cpp +295 -0
  50. data/GosuImpl/Graphics/Image.cpp +159 -0
  51. data/GosuImpl/Graphics/LargeImageData.cpp +115 -0
  52. data/GosuImpl/Graphics/LargeImageData.hpp +37 -0
  53. data/GosuImpl/Graphics/RotFlip.cpp +184 -0
  54. data/GosuImpl/Graphics/TexChunk.cpp +77 -0
  55. data/GosuImpl/Graphics/TexChunk.hpp +40 -0
  56. data/GosuImpl/Graphics/Text.cpp +223 -0
  57. data/GosuImpl/Graphics/TextMac.cpp +242 -0
  58. data/GosuImpl/Graphics/TextPangoFT.cpp +186 -0
  59. data/GosuImpl/Graphics/TextWin.cpp +172 -0
  60. data/GosuImpl/Graphics/Texture.cpp +104 -0
  61. data/GosuImpl/Graphics/Texture.hpp +34 -0
  62. data/GosuImpl/IO.cpp +48 -0
  63. data/GosuImpl/InputMac.mm +677 -0
  64. data/GosuImpl/InputWin.cpp +444 -0
  65. data/GosuImpl/InputX.cpp +158 -0
  66. data/GosuImpl/MacUtility.hpp +48 -0
  67. data/GosuImpl/Math.cpp +49 -0
  68. data/GosuImpl/RubyGosu.swg +474 -0
  69. data/GosuImpl/RubyGosuStub.mm +17 -0
  70. data/GosuImpl/RubyGosu_DllMain.cxx +30 -0
  71. data/GosuImpl/RubyGosu_wrap.cxx +8521 -0
  72. data/GosuImpl/RubyGosu_wrap.h +31 -0
  73. data/GosuImpl/Sockets/CommSocket.cpp +304 -0
  74. data/GosuImpl/Sockets/ListenerSocket.cpp +60 -0
  75. data/GosuImpl/Sockets/MessageSocket.cpp +136 -0
  76. data/GosuImpl/Sockets/Socket.cpp +145 -0
  77. data/GosuImpl/Sockets/Sockets.hpp +66 -0
  78. data/GosuImpl/TextInputMac.mm +207 -0
  79. data/GosuImpl/TextInputWin.cpp +197 -0
  80. data/GosuImpl/TextInputX.cpp +201 -0
  81. data/GosuImpl/TextTTFWin.cpp +247 -0
  82. data/GosuImpl/TimingUnix.cpp +17 -0
  83. data/GosuImpl/TimingWin.cpp +28 -0
  84. data/GosuImpl/Utility.cpp +140 -0
  85. data/GosuImpl/WinMain.cpp +69 -0
  86. data/GosuImpl/WinUtility.cpp +137 -0
  87. data/GosuImpl/WindowMac.mm +466 -0
  88. data/GosuImpl/WindowWin.cpp +447 -0
  89. data/GosuImpl/WindowX.cpp +392 -0
  90. data/GosuImpl/X11vroot.h +118 -0
  91. data/README.txt +13 -0
  92. data/Rakefile +178 -0
  93. data/examples/ChipmunkIntegration.rb +275 -0
  94. data/examples/CptnRuby.rb +231 -0
  95. data/examples/MoreChipmunkAndRMagick.rb +155 -0
  96. data/examples/OpenGLIntegration.rb +232 -0
  97. data/examples/RMagickIntegration.rb +449 -0
  98. data/examples/TextInput.cpp +170 -0
  99. data/examples/TextInput.rb +139 -0
  100. data/examples/Tutorial.cpp +215 -0
  101. data/examples/Tutorial.rb +137 -0
  102. data/examples/media/Beep.wav +0 -0
  103. data/examples/media/CptnRuby Gem.png +0 -0
  104. data/examples/media/CptnRuby Map.txt +25 -0
  105. data/examples/media/CptnRuby Tileset.png +0 -0
  106. data/examples/media/CptnRuby.png +0 -0
  107. data/examples/media/Cursor.png +0 -0
  108. data/examples/media/Earth.png +0 -0
  109. data/examples/media/Explosion.wav +0 -0
  110. data/examples/media/LargeStar.png +0 -0
  111. data/examples/media/Sky.jpg +0 -0
  112. data/examples/media/Smoke.png +0 -0
  113. data/examples/media/Soldier.png +0 -0
  114. data/examples/media/Space.png +0 -0
  115. data/examples/media/Star.png +0 -0
  116. data/examples/media/Starfighter.bmp +0 -0
  117. data/linux/Makefile.in +98 -0
  118. data/linux/configure +5658 -0
  119. data/linux/configure.ac +126 -0
  120. data/linux/extconf.rb +11 -0
  121. data/mac/English.lproj/InfoPlist.strings +0 -0
  122. data/mac/Gosu-Info.plist +26 -0
  123. data/mac/Gosu.xcodeproj/project.pbxproj +1194 -0
  124. data/mac/RubyGosu Template-Info.plist +26 -0
  125. data/mac/libboost_thread_1_34_1_universal.a +0 -0
  126. data/mac/libboost_thread_d_1_34_1_universal.a +0 -0
  127. data/mac/libfmod_universal.a +0 -0
  128. data/mac/libpng_universal.a +0 -0
  129. data/mac/libz_universal.a +0 -0
  130. data/reference/Async_8hpp-source.html +70 -0
  131. data/reference/Audio_8hpp-source.html +114 -0
  132. data/reference/Audio_8hpp.html +50 -0
  133. data/reference/AutoLink_8hpp-source.html +38 -0
  134. data/reference/AutoLink_8hpp.html +34 -0
  135. data/reference/Bitmap_8hpp-source.html +85 -0
  136. data/reference/Bitmap_8hpp.html +58 -0
  137. data/reference/ButtonsMac_8hpp-source.html +133 -0
  138. data/reference/ButtonsWin_8hpp-source.html +133 -0
  139. data/reference/ButtonsX_8hpp-source.html +134 -0
  140. data/reference/Color_8hpp-source.html +169 -0
  141. data/reference/Color_8hpp.html +85 -0
  142. data/reference/Directories_8hpp-source.html +42 -0
  143. data/reference/Directories_8hpp.html +46 -0
  144. data/reference/Font_8hpp-source.html +65 -0
  145. data/reference/Font_8hpp.html +41 -0
  146. data/reference/Fwd_8hpp-source.html +52 -0
  147. data/reference/Fwd_8hpp.html +37 -0
  148. data/reference/Gosu_8hpp-source.html +48 -0
  149. data/reference/Gosu_8hpp.html +34 -0
  150. data/reference/GraphicsBase_8hpp-source.html +57 -0
  151. data/reference/GraphicsBase_8hpp.html +56 -0
  152. data/reference/Graphics_8hpp-source.html +96 -0
  153. data/reference/Graphics_8hpp.html +53 -0
  154. data/reference/IO_8hpp-source.html +255 -0
  155. data/reference/IO_8hpp.html +74 -0
  156. data/reference/ImageData_8hpp-source.html +62 -0
  157. data/reference/ImageData_8hpp.html +43 -0
  158. data/reference/Image_8hpp-source.html +126 -0
  159. data/reference/Image_8hpp.html +48 -0
  160. data/reference/Input_8hpp-source.html +118 -0
  161. data/reference/Input_8hpp.html +50 -0
  162. data/reference/Math_8hpp-source.html +92 -0
  163. data/reference/Math_8hpp.html +74 -0
  164. data/reference/Platform_8hpp-source.html +83 -0
  165. data/reference/Platform_8hpp.html +73 -0
  166. data/reference/RotFlip_8hpp-source.html +138 -0
  167. data/reference/RotFlip_8hpp.html +77 -0
  168. data/reference/Sockets_8hpp-source.html +130 -0
  169. data/reference/Sockets_8hpp.html +66 -0
  170. data/reference/TextInput_8hpp-source.html +64 -0
  171. data/reference/TextInput_8hpp.html +41 -0
  172. data/reference/Text_8hpp-source.html +51 -0
  173. data/reference/Text_8hpp.html +46 -0
  174. data/reference/Timing_8hpp-source.html +36 -0
  175. data/reference/Timing_8hpp.html +42 -0
  176. data/reference/Utility_8hpp-source.html +44 -0
  177. data/reference/Utility_8hpp.html +48 -0
  178. data/reference/WinUtility_8hpp-source.html +79 -0
  179. data/reference/WinUtility_8hpp.html +64 -0
  180. data/reference/Window_8hpp-source.html +91 -0
  181. data/reference/Window_8hpp.html +41 -0
  182. data/reference/annotated.html +51 -0
  183. data/reference/classGosu_1_1Audio-members.html +34 -0
  184. data/reference/classGosu_1_1Audio.html +46 -0
  185. data/reference/classGosu_1_1Bitmap-members.html +44 -0
  186. data/reference/classGosu_1_1Bitmap.html +263 -0
  187. data/reference/classGosu_1_1Buffer-members.html +44 -0
  188. data/reference/classGosu_1_1Buffer.html +78 -0
  189. data/reference/classGosu_1_1Buffer.png +0 -0
  190. data/reference/classGosu_1_1Button-members.html +36 -0
  191. data/reference/classGosu_1_1Button.html +143 -0
  192. data/reference/classGosu_1_1Color-members.html +56 -0
  193. data/reference/classGosu_1_1Color.html +387 -0
  194. data/reference/classGosu_1_1File-members.html +41 -0
  195. data/reference/classGosu_1_1File.html +69 -0
  196. data/reference/classGosu_1_1File.png +0 -0
  197. data/reference/classGosu_1_1Font-members.html +39 -0
  198. data/reference/classGosu_1_1Font.html +309 -0
  199. data/reference/classGosu_1_1Graphics-members.html +50 -0
  200. data/reference/classGosu_1_1Graphics.html +234 -0
  201. data/reference/classGosu_1_1Image-members.html +45 -0
  202. data/reference/classGosu_1_1Image.html +518 -0
  203. data/reference/classGosu_1_1ImageData-members.html +37 -0
  204. data/reference/classGosu_1_1ImageData.html +60 -0
  205. data/reference/classGosu_1_1Input-members.html +44 -0
  206. data/reference/classGosu_1_1Input.html +223 -0
  207. data/reference/classGosu_1_1MessageSocket-members.html +40 -0
  208. data/reference/classGosu_1_1MessageSocket.html +233 -0
  209. data/reference/classGosu_1_1Resource-members.html +39 -0
  210. data/reference/classGosu_1_1Resource.html +116 -0
  211. data/reference/classGosu_1_1Resource.png +0 -0
  212. data/reference/classGosu_1_1Sample-members.html +37 -0
  213. data/reference/classGosu_1_1Sample.html +200 -0
  214. data/reference/classGosu_1_1SampleInstance-members.html +38 -0
  215. data/reference/classGosu_1_1SampleInstance.html +169 -0
  216. data/reference/classGosu_1_1Song-members.html +43 -0
  217. data/reference/classGosu_1_1Song.html +260 -0
  218. data/reference/classGosu_1_1TextInput-members.html +38 -0
  219. data/reference/classGosu_1_1TextInput.html +121 -0
  220. data/reference/classGosu_1_1Window-members.html +50 -0
  221. data/reference/classGosu_1_1Window.html +271 -0
  222. data/reference/doxyfile +233 -0
  223. data/reference/doxygen.css +433 -0
  224. data/reference/doxygen.png +0 -0
  225. data/reference/files.html +54 -0
  226. data/reference/functions.html +236 -0
  227. data/reference/functions_enum.html +45 -0
  228. data/reference/functions_func.html +227 -0
  229. data/reference/functions_vars.html +47 -0
  230. data/reference/hierarchy.html +53 -0
  231. data/reference/index.html +26 -0
  232. data/reference/namespaceGosu.html +2890 -0
  233. data/reference/namespaceGosu_1_1Colors.html +70 -0
  234. data/reference/namespaceGosu_1_1Win.html +275 -0
  235. data/reference/namespacemembers.html +216 -0
  236. data/reference/namespacemembers_enum.html +52 -0
  237. data/reference/namespacemembers_eval.html +54 -0
  238. data/reference/namespacemembers_func.html +185 -0
  239. data/reference/namespacemembers_type.html +46 -0
  240. data/reference/namespacemembers_vars.html +46 -0
  241. data/reference/namespaces.html +35 -0
  242. data/reference/tab_b.gif +0 -0
  243. data/reference/tab_l.gif +0 -0
  244. data/reference/tab_r.gif +0 -0
  245. data/reference/tabs.css +102 -0
  246. data/windows/Gosu.sln +29 -0
  247. data/windows/Gosu.vcproj +553 -0
  248. data/windows/RubyGosu.vcproj +138 -0
  249. metadata +305 -0
@@ -0,0 +1,31 @@
1
+ /* ----------------------------------------------------------------------------
2
+ * This file was automatically generated by SWIG (http://www.swig.org).
3
+ * Version 1.3.31
4
+ *
5
+ * This file is not intended to be easily readable and contains a number of
6
+ * coding conventions designed to improve portability and efficiency. Do not make
7
+ * changes to this file unless you know what you are doing--modify the SWIG
8
+ * interface file instead.
9
+ * ----------------------------------------------------------------------------- */
10
+
11
+ #ifndef SWIG_Gosu_WRAP_H_
12
+ #define SWIG_Gosu_WRAP_H_
13
+
14
+ namespace Swig {
15
+ class Director;
16
+ }
17
+
18
+
19
+ class SwigDirector_Window : public Gosu::Window, public Swig::Director {
20
+
21
+ public:
22
+ SwigDirector_Window(VALUE self, unsigned int width, unsigned int height, bool fullscreen, double updateInterval = 16.666666);
23
+ virtual ~SwigDirector_Window();
24
+ virtual void update();
25
+ virtual void draw();
26
+ virtual void buttonDown(Gosu::Button arg0);
27
+ virtual void buttonUp(Gosu::Button arg0);
28
+ };
29
+
30
+
31
+ #endif
@@ -0,0 +1,304 @@
1
+ #include <Gosu/Sockets.hpp>
2
+ #include <GosuImpl/Sockets/Sockets.hpp>
3
+ #include <cassert>
4
+ #include <cstring>
5
+ #include <stdexcept>
6
+ #include <vector>
7
+
8
+ struct Gosu::CommSocket::Impl
9
+ {
10
+ Socket socket;
11
+ CommMode mode;
12
+
13
+ typedef std::vector<char> Buffer;
14
+ Buffer inbox, outbox;
15
+
16
+ void appendBuffer(const char* buffer, std::size_t size,
17
+ boost::function<void (const void*, std::size_t)>& event)
18
+ {
19
+ switch (mode)
20
+ {
21
+ case cmRaw:
22
+ {
23
+ // Raw = simple! Yay.
24
+ if (event)
25
+ event(buffer, size);
26
+
27
+ break;
28
+ }
29
+
30
+ case cmManaged:
31
+ {
32
+ // Append new data to inbox.
33
+ inbox.insert(inbox.end(), buffer, buffer + size);
34
+
35
+ for (;;) // IMPR.
36
+ {
37
+ const size_t sizeSize = sizeof(u_long);
38
+
39
+ // Not even enough bytes there to determine the size of the
40
+ // incoming message.
41
+ if (inbox.size() < sizeSize)
42
+ break;
43
+
44
+ // Message size is already here, convert it.
45
+ u_long msgSize = *reinterpret_cast<u_long*>(&inbox[0]);
46
+ msgSize = ntohl(msgSize);
47
+
48
+ // Can't really handle zero-size messages. IMPR?!
49
+ if (msgSize == 0)
50
+ throw std::logic_error("Cannot handle empty messages");
51
+
52
+ // Has the current message arrived completely?
53
+ if (inbox.size() < sizeSize + msgSize)
54
+ break;
55
+
56
+ // Current message is here: Call event...
57
+ if (event)
58
+ event(&inbox[sizeSize], msgSize);
59
+
60
+ // ...and delete it from the inbox.
61
+ inbox.erase(inbox.begin(),
62
+ inbox.begin() + sizeSize + msgSize);
63
+ }
64
+
65
+ break;
66
+ }
67
+ }
68
+ }
69
+ };
70
+
71
+ Gosu::CommSocket::CommSocket(CommMode mode, SocketAddress targetAddress,
72
+ SocketPort targetPort)
73
+ : pimpl(new Impl)
74
+ {
75
+ pimpl->mode = mode;
76
+
77
+ sockaddr_in addr;
78
+ std::memset(&addr, 0, sizeof addr);
79
+ addr.sin_family = AF_INET;
80
+ addr.sin_addr.s_addr = htonl(targetAddress);
81
+ addr.sin_port = htons(targetPort);
82
+
83
+ pimpl->socket.setHandle(socketCheck(::socket(AF_INET, SOCK_STREAM, 0)));
84
+ pimpl->socket.setBlocking(true);
85
+ socketCheck(::connect(pimpl->socket.handle(), reinterpret_cast<sockaddr*>(&addr),
86
+ sizeof addr));
87
+ pimpl->socket.setBlocking(false);
88
+ }
89
+
90
+ Gosu::CommSocket::CommSocket(CommMode mode, Socket& socket)
91
+ : pimpl(new Impl)
92
+ {
93
+ pimpl->mode = mode;
94
+
95
+ pimpl->socket.swap(socket);
96
+ pimpl->socket.setBlocking(false);
97
+ }
98
+
99
+ Gosu::CommSocket::~CommSocket()
100
+ {
101
+ }
102
+
103
+ Gosu::SocketAddress Gosu::CommSocket::address() const
104
+ {
105
+ return pimpl->socket.address();
106
+ }
107
+
108
+ Gosu::SocketPort Gosu::CommSocket::port() const
109
+ {
110
+ return pimpl->socket.port();
111
+ }
112
+
113
+ Gosu::SocketAddress Gosu::CommSocket::remoteAddress() const
114
+ {
115
+ assert(connected());
116
+
117
+ sockaddr_in addr;
118
+ int size = sizeof addr;
119
+ socketCheck(::getpeername(pimpl->socket.handle(),
120
+ reinterpret_cast<sockaddr*>(&addr),
121
+ reinterpret_cast<socklen_t*>(&size)));
122
+
123
+ return ntohl(addr.sin_addr.s_addr);
124
+ }
125
+
126
+ Gosu::SocketPort Gosu::CommSocket::remotePort() const
127
+ {
128
+ assert(connected());
129
+
130
+ sockaddr_in addr;
131
+ int size = sizeof addr;
132
+ socketCheck(::getpeername(pimpl->socket.handle(),
133
+ reinterpret_cast<sockaddr*>(&addr),
134
+ reinterpret_cast<socklen_t*>(&size)));
135
+ return ntohs(addr.sin_port);
136
+ }
137
+
138
+ Gosu::CommMode Gosu::CommSocket::mode() const
139
+ {
140
+ return pimpl->mode;
141
+ }
142
+
143
+ bool Gosu::CommSocket::connected() const
144
+ {
145
+ return pimpl->socket.handle() != INVALID_SOCKET;
146
+ }
147
+
148
+ void Gosu::CommSocket::disconnect()
149
+ {
150
+ pimpl->socket.setHandle(INVALID_SOCKET);
151
+ // IMPR: Mmmmh. A full-blown sockets library should probably try to send
152
+ // the remaining contents of the outbox. This is annoying to implement,
153
+ // though...
154
+ pimpl->outbox.clear();
155
+ if (onDisconnection)
156
+ onDisconnection();
157
+ }
158
+
159
+ bool Gosu::CommSocket::keepAlive() const
160
+ {
161
+ int buf;
162
+ int size = sizeof buf;
163
+ socketCheck(::getsockopt(pimpl->socket.handle(), SOL_SOCKET, SO_KEEPALIVE,
164
+ reinterpret_cast<char*>(&buf),
165
+ reinterpret_cast<socklen_t*>(&size)));
166
+ return buf != 0;
167
+ }
168
+
169
+ void Gosu::CommSocket::setKeepAlive(bool value)
170
+ {
171
+ int buf = value;
172
+ socketCheck(::setsockopt(pimpl->socket.handle(), SOL_SOCKET, SO_KEEPALIVE,
173
+ reinterpret_cast<char*>(&buf), sizeof buf));
174
+ }
175
+
176
+ void Gosu::CommSocket::update()
177
+ {
178
+ sendPendingData();
179
+
180
+ if (!connected())
181
+ return;
182
+
183
+ for (;;)
184
+ {
185
+ char buffer[1024];
186
+ int received = ::recv(pimpl->socket.handle(), buffer, sizeof buffer, 0);
187
+
188
+ if (received > 0 && received <= static_cast<int>(sizeof buffer))
189
+ {
190
+ // Data arrived and fit into the buffer.
191
+ pimpl->appendBuffer(buffer, received, onReceive);
192
+ }
193
+ else if (received == 0)
194
+ {
195
+ // The other side has gracefully closed the connection.
196
+ disconnect();
197
+ return;
198
+ }
199
+ else if (received == SOCKET_ERROR)
200
+ {
201
+ switch (lastSocketError())
202
+ {
203
+ // Arriving data didn't fit into the buffer.
204
+ case GOSU_SOCK_ERR(EMSGSIZE):
205
+ pimpl->appendBuffer(buffer, sizeof buffer, onReceive);
206
+ break;
207
+
208
+ // There simply was no data.
209
+ case GOSU_SOCK_ERR(EWOULDBLOCK):
210
+ return;
211
+
212
+ // Connection was reset or is invalid.
213
+ case GOSU_SOCK_ERR(ENETDOWN):
214
+ case GOSU_SOCK_ERR(ENOTCONN):
215
+ case GOSU_SOCK_ERR(ENETRESET):
216
+ case GOSU_SOCK_ERR(ECONNABORTED):
217
+ case GOSU_SOCK_ERR(ETIMEDOUT):
218
+ case GOSU_SOCK_ERR(ECONNRESET):
219
+ #ifndef GOSU_IS_WIN
220
+ // UNIX specific, rare error
221
+ case GOSU_SOCK_ERR(EPIPE):
222
+ #endif
223
+ disconnect();
224
+ return;
225
+
226
+ // Everything else is unexpected.
227
+ default:
228
+ throwLastSocketError();
229
+ }
230
+ }
231
+ else assert(false);
232
+ }
233
+ }
234
+
235
+ void Gosu::CommSocket::send(const void* buffer, std::size_t size)
236
+ {
237
+ if (!connected())
238
+ return;
239
+
240
+ // In managed mode, also send the length of the buffer.
241
+ if (mode() == cmManaged)
242
+ {
243
+ u_long netSize = htonl(size);
244
+ const char* charBuf = reinterpret_cast<const char*>(&netSize);
245
+ pimpl->outbox.insert(pimpl->outbox.end(), charBuf,
246
+ charBuf + sizeof netSize);
247
+ }
248
+
249
+ const char* charBuf = reinterpret_cast<const char*>(buffer);
250
+ pimpl->outbox.insert(pimpl->outbox.end(), charBuf, charBuf + size);
251
+ }
252
+
253
+ void Gosu::CommSocket::sendPendingData()
254
+ {
255
+ if (pendingBytes() == 0 || !connected())
256
+ return;
257
+
258
+ int sent = ::send(pimpl->socket.handle(), &pimpl->outbox.front(),
259
+ pendingBytes(), 0);
260
+
261
+ if (sent >= 0)
262
+ {
263
+ // Remove sent data from the outbox.
264
+ if (sent >= static_cast<int>(pendingBytes()))
265
+ pimpl->outbox.clear();
266
+ else
267
+ pimpl->outbox.erase(pimpl->outbox.begin(),
268
+ pimpl->outbox.begin() + sent);
269
+ }
270
+ else
271
+ {
272
+ switch (lastSocketError())
273
+ {
274
+ // These error codes basically mean "try again later".
275
+ case GOSU_SOCK_ERR(ENOBUFS):
276
+ case GOSU_SOCK_ERR(EWOULDBLOCK):
277
+ case GOSU_SOCK_ERR(EHOSTUNREACH):
278
+ break;
279
+
280
+ // And these tell us we're disconnected.
281
+ case GOSU_SOCK_ERR(ENETDOWN):
282
+ case GOSU_SOCK_ERR(ENETRESET):
283
+ case GOSU_SOCK_ERR(ENOTCONN):
284
+ case GOSU_SOCK_ERR(ECONNABORTED):
285
+ case GOSU_SOCK_ERR(ECONNRESET):
286
+ case GOSU_SOCK_ERR(ETIMEDOUT):
287
+ #ifndef GOSU_IS_WIN
288
+ // UNIX-specific, rare error
289
+ case GOSU_SOCK_ERR(EPIPE):
290
+ #endif
291
+ disconnect();
292
+ break;
293
+
294
+ // Everything else is unexpected.
295
+ default:
296
+ throwLastSocketError();
297
+ }
298
+ }
299
+ }
300
+
301
+ std::size_t Gosu::CommSocket::pendingBytes() const
302
+ {
303
+ return pimpl->outbox.size();
304
+ }
@@ -0,0 +1,60 @@
1
+ #include <Gosu/Sockets.hpp>
2
+ #include <GosuImpl/Sockets/Sockets.hpp>
3
+ #include <cassert>
4
+ #include <cstring>
5
+
6
+ struct Gosu::ListenerSocket::Impl
7
+ {
8
+ Socket socket;
9
+ };
10
+
11
+ Gosu::ListenerSocket::ListenerSocket(SocketPort port)
12
+ : pimpl(new Impl)
13
+ {
14
+ pimpl->socket.setHandle(socketCheck(::socket(AF_INET, SOCK_STREAM, 0)));
15
+ pimpl->socket.setBlocking(false);
16
+
17
+ int enable = 1;
18
+ socketCheck(::setsockopt(pimpl->socket.handle(), SOL_SOCKET, SO_REUSEADDR,
19
+ reinterpret_cast<char*>(&enable), sizeof enable));
20
+
21
+ sockaddr_in addr;
22
+ std::memset(&addr, 0, sizeof addr);
23
+ addr.sin_family = AF_INET;
24
+ // addr.sin_addr.S_un.S_addr = ::htonl(INADDR_ANY);
25
+ addr.sin_addr.s_addr = htonl(INADDR_ANY);
26
+ addr.sin_port = htons(port);
27
+ socketCheck(::bind(pimpl->socket.handle(),
28
+ reinterpret_cast<sockaddr*>(&addr), sizeof addr));
29
+ socketCheck(::listen(pimpl->socket.handle(), 10));
30
+ }
31
+
32
+ Gosu::ListenerSocket::~ListenerSocket()
33
+ {
34
+ }
35
+
36
+ Gosu::SocketAddress Gosu::ListenerSocket::address() const
37
+ {
38
+ return pimpl->socket.address();
39
+ }
40
+
41
+ Gosu::SocketPort Gosu::ListenerSocket::port() const
42
+ {
43
+ return pimpl->socket.port();
44
+ }
45
+
46
+ void Gosu::ListenerSocket::update()
47
+ {
48
+ while (onConnection)
49
+ {
50
+ SocketHandle newHandle =
51
+ socketCheck(::accept(pimpl->socket.handle(), 0, 0));
52
+
53
+ if (newHandle == INVALID_SOCKET)
54
+ break;
55
+
56
+ Socket newSocket;
57
+ newSocket.setHandle(newHandle);
58
+ onConnection(newSocket);
59
+ }
60
+ }
@@ -0,0 +1,136 @@
1
+ #include <Gosu/Sockets.hpp>
2
+ #include <GosuImpl/Sockets/Sockets.hpp>
3
+ #include <cassert>
4
+ #include <cstring>
5
+ #include <vector>
6
+
7
+ struct Gosu::MessageSocket::Impl
8
+ {
9
+ Socket socket;
10
+ std::size_t maxMessageSize;
11
+ };
12
+
13
+ Gosu::MessageSocket::MessageSocket(SocketPort port)
14
+ : pimpl(new Impl)
15
+ {
16
+ pimpl->socket.setHandle(socketCheck(::socket(AF_INET, SOCK_DGRAM, 0)));
17
+ pimpl->socket.setBlocking(false);
18
+ sockaddr_in addr;
19
+ std::memset(&addr, 0, sizeof addr);
20
+ addr.sin_family = AF_INET;
21
+ // addr.sin_addr.S_un.S_addr = ::htonl(INADDR_ANY);
22
+ addr.sin_addr.s_addr = htonl(INADDR_ANY);
23
+ addr.sin_port = htons(port);
24
+ socketCheck(::bind(pimpl->socket.handle(),
25
+ reinterpret_cast<sockaddr*>(&addr), sizeof addr));
26
+
27
+ //FIXME: Unix: implement this. SO_MAX_MSG_SIZE not defined.
28
+ #ifdef WIN32
29
+ unsigned maxMessageSize;
30
+ int size = sizeof maxMessageSize;
31
+ socketCheck(::getsockopt(pimpl->socket.handle(), SOL_SOCKET, SO_MAX_MSG_SIZE,
32
+ reinterpret_cast<char*>(&maxMessageSize), &size));
33
+ pimpl->maxMessageSize = maxMessageSize;
34
+ #endif
35
+ }
36
+
37
+ Gosu::MessageSocket::~MessageSocket()
38
+ {
39
+ }
40
+
41
+ Gosu::SocketAddress Gosu::MessageSocket::address() const
42
+ {
43
+ return pimpl->socket.address();
44
+ }
45
+
46
+ Gosu::SocketPort Gosu::MessageSocket::port() const
47
+ {
48
+ return pimpl->socket.port();
49
+ }
50
+
51
+ std::size_t Gosu::MessageSocket::maxMessageSize() const
52
+ {
53
+ return pimpl->maxMessageSize;
54
+ }
55
+
56
+ void Gosu::MessageSocket::update()
57
+ {
58
+ std::vector<char> buffer(maxMessageSize());
59
+
60
+ sockaddr_in addr;
61
+ int size = sizeof addr;
62
+
63
+ for (;;)
64
+ {
65
+ int received = ::recvfrom(pimpl->socket.handle(), &buffer.front(),
66
+ buffer.size(), 0, reinterpret_cast<sockaddr*>(&addr),
67
+ reinterpret_cast<socklen_t*>(&size));
68
+
69
+ if (received != SOCKET_ERROR && onReceive)
70
+ {
71
+ // onReceive(::ntohl(addr.sin_addr.S_un.S_addr),
72
+ onReceive(ntohl(addr.sin_addr.s_addr),
73
+ ntohs(addr.sin_port), &buffer.front(), received);
74
+ }
75
+ else switch (lastSocketError())
76
+ {
77
+ // Ignore some of the errors.
78
+ case GOSU_SOCK_ERR(EWOULDBLOCK):
79
+ case GOSU_SOCK_ERR(ENETDOWN):
80
+ case GOSU_SOCK_ERR(ENETRESET):
81
+ case GOSU_SOCK_ERR(ETIMEDOUT):
82
+ // IMPR: "The application should close the socket..."??
83
+ case GOSU_SOCK_ERR(ECONNRESET):
84
+ return;
85
+
86
+ // Everything else is unexpected.
87
+ default:
88
+ throwLastSocketError();
89
+ }
90
+ }
91
+ }
92
+
93
+ void Gosu::MessageSocket::send(SocketAddress address, SocketPort port,
94
+ const void* buffer, std::size_t size)
95
+ {
96
+ sockaddr_in addr;
97
+ std::memset(&addr, 0, sizeof addr);
98
+ addr.sin_family = AF_INET;
99
+ // addr.sin_addr.S_un.S_addr = ::htonl(address);
100
+ addr.sin_addr.s_addr = htonl(address);
101
+ addr.sin_port = htons(port);
102
+
103
+ int sent = ::sendto(pimpl->socket.handle(),
104
+ reinterpret_cast<const char*>(buffer), size, 0,
105
+ reinterpret_cast<sockaddr*>(&addr), sizeof addr);
106
+
107
+ if (sent == static_cast<int>(size))
108
+ return; // Yay, did it!
109
+
110
+ assert(sent == SOCKET_ERROR); // Don't expect partial sends.
111
+
112
+ switch (lastSocketError())
113
+ {
114
+ // Just ignore a lot of errors... this is UDP, right?
115
+ case GOSU_SOCK_ERR(ENETDOWN):
116
+ case GOSU_SOCK_ERR(ENETRESET):
117
+ case GOSU_SOCK_ERR(ENOBUFS):
118
+ case GOSU_SOCK_ERR(EWOULDBLOCK):
119
+ case GOSU_SOCK_ERR(EHOSTUNREACH):
120
+ case GOSU_SOCK_ERR(ECONNABORTED):
121
+ case GOSU_SOCK_ERR(ECONNRESET):
122
+ case GOSU_SOCK_ERR(ENETUNREACH):
123
+ case GOSU_SOCK_ERR(ETIMEDOUT):
124
+ break;
125
+
126
+ // Everything else means more than just another lost packet, though.
127
+ default:
128
+ throwLastSocketError();
129
+ }
130
+ }
131
+
132
+ /*void Gosu::MessageSocket::broadcast(SocketPort port,
133
+ const void* buffer, std::size_t size)
134
+ {
135
+ assert(false);
136
+ }*/