gosu 0.7.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING.txt +29 -0
- data/Gosu/Async.hpp +48 -0
- data/Gosu/Audio.hpp +145 -0
- data/Gosu/AutoLink.hpp +16 -0
- data/Gosu/Bitmap.hpp +85 -0
- data/Gosu/ButtonsMac.hpp +114 -0
- data/Gosu/ButtonsWin.hpp +111 -0
- data/Gosu/ButtonsX.hpp +115 -0
- data/Gosu/Color.hpp +172 -0
- data/Gosu/Directories.hpp +36 -0
- data/Gosu/Font.hpp +59 -0
- data/Gosu/Fwd.hpp +31 -0
- data/Gosu/Gosu.hpp +26 -0
- data/Gosu/Graphics.hpp +86 -0
- data/Gosu/GraphicsBase.hpp +45 -0
- data/Gosu/IO.hpp +255 -0
- data/Gosu/Image.hpp +148 -0
- data/Gosu/ImageData.hpp +45 -0
- data/Gosu/Input.hpp +116 -0
- data/Gosu/Math.hpp +95 -0
- data/Gosu/Platform.hpp +61 -0
- data/Gosu/RotFlip.hpp +116 -0
- data/Gosu/Sockets.hpp +129 -0
- data/Gosu/Text.hpp +47 -0
- data/Gosu/TextInput.hpp +57 -0
- data/Gosu/Timing.hpp +16 -0
- data/Gosu/Utility.hpp +24 -0
- data/Gosu/WinUtility.hpp +76 -0
- data/Gosu/Window.hpp +84 -0
- data/GosuImpl/Async.cpp +37 -0
- data/GosuImpl/AudioFmod.cpp +417 -0
- data/GosuImpl/AudioSDL.cpp +255 -0
- data/GosuImpl/DirectoriesMac.mm +38 -0
- data/GosuImpl/DirectoriesUnix.cpp +48 -0
- data/GosuImpl/DirectoriesWin.cpp +42 -0
- data/GosuImpl/FileUnix.cpp +100 -0
- data/GosuImpl/FileWin.cpp +83 -0
- data/GosuImpl/Graphics/Bitmap.cpp +116 -0
- data/GosuImpl/Graphics/BitmapBMP.cpp +232 -0
- data/GosuImpl/Graphics/BitmapColorKey.cpp +39 -0
- data/GosuImpl/Graphics/BitmapPNG.cpp +276 -0
- data/GosuImpl/Graphics/BitmapUtils.cpp +67 -0
- data/GosuImpl/Graphics/BlockAllocator.cpp +127 -0
- data/GosuImpl/Graphics/BlockAllocator.hpp +34 -0
- data/GosuImpl/Graphics/Color.cpp +126 -0
- data/GosuImpl/Graphics/Common.hpp +21 -0
- data/GosuImpl/Graphics/DrawOp.hpp +154 -0
- data/GosuImpl/Graphics/Font.cpp +110 -0
- data/GosuImpl/Graphics/Graphics.cpp +295 -0
- data/GosuImpl/Graphics/Image.cpp +159 -0
- data/GosuImpl/Graphics/LargeImageData.cpp +115 -0
- data/GosuImpl/Graphics/LargeImageData.hpp +37 -0
- data/GosuImpl/Graphics/RotFlip.cpp +184 -0
- data/GosuImpl/Graphics/TexChunk.cpp +77 -0
- data/GosuImpl/Graphics/TexChunk.hpp +40 -0
- data/GosuImpl/Graphics/Text.cpp +223 -0
- data/GosuImpl/Graphics/TextMac.cpp +242 -0
- data/GosuImpl/Graphics/TextPangoFT.cpp +186 -0
- data/GosuImpl/Graphics/TextWin.cpp +172 -0
- data/GosuImpl/Graphics/Texture.cpp +104 -0
- data/GosuImpl/Graphics/Texture.hpp +34 -0
- data/GosuImpl/IO.cpp +48 -0
- data/GosuImpl/InputMac.mm +677 -0
- data/GosuImpl/InputWin.cpp +444 -0
- data/GosuImpl/InputX.cpp +158 -0
- data/GosuImpl/MacUtility.hpp +48 -0
- data/GosuImpl/Math.cpp +49 -0
- data/GosuImpl/RubyGosu.swg +474 -0
- data/GosuImpl/RubyGosuStub.mm +17 -0
- data/GosuImpl/RubyGosu_DllMain.cxx +30 -0
- data/GosuImpl/RubyGosu_wrap.cxx +8521 -0
- data/GosuImpl/RubyGosu_wrap.h +31 -0
- data/GosuImpl/Sockets/CommSocket.cpp +304 -0
- data/GosuImpl/Sockets/ListenerSocket.cpp +60 -0
- data/GosuImpl/Sockets/MessageSocket.cpp +136 -0
- data/GosuImpl/Sockets/Socket.cpp +145 -0
- data/GosuImpl/Sockets/Sockets.hpp +66 -0
- data/GosuImpl/TextInputMac.mm +207 -0
- data/GosuImpl/TextInputWin.cpp +197 -0
- data/GosuImpl/TextInputX.cpp +201 -0
- data/GosuImpl/TextTTFWin.cpp +247 -0
- data/GosuImpl/TimingUnix.cpp +17 -0
- data/GosuImpl/TimingWin.cpp +28 -0
- data/GosuImpl/Utility.cpp +140 -0
- data/GosuImpl/WinMain.cpp +69 -0
- data/GosuImpl/WinUtility.cpp +137 -0
- data/GosuImpl/WindowMac.mm +466 -0
- data/GosuImpl/WindowWin.cpp +447 -0
- data/GosuImpl/WindowX.cpp +392 -0
- data/GosuImpl/X11vroot.h +118 -0
- data/README.txt +13 -0
- data/Rakefile +178 -0
- data/examples/ChipmunkIntegration.rb +275 -0
- data/examples/CptnRuby.rb +231 -0
- data/examples/MoreChipmunkAndRMagick.rb +155 -0
- data/examples/OpenGLIntegration.rb +232 -0
- data/examples/RMagickIntegration.rb +449 -0
- data/examples/TextInput.cpp +170 -0
- data/examples/TextInput.rb +139 -0
- data/examples/Tutorial.cpp +215 -0
- data/examples/Tutorial.rb +137 -0
- data/examples/media/Beep.wav +0 -0
- data/examples/media/CptnRuby Gem.png +0 -0
- data/examples/media/CptnRuby Map.txt +25 -0
- data/examples/media/CptnRuby Tileset.png +0 -0
- data/examples/media/CptnRuby.png +0 -0
- data/examples/media/Cursor.png +0 -0
- data/examples/media/Earth.png +0 -0
- data/examples/media/Explosion.wav +0 -0
- data/examples/media/LargeStar.png +0 -0
- data/examples/media/Sky.jpg +0 -0
- data/examples/media/Smoke.png +0 -0
- data/examples/media/Soldier.png +0 -0
- data/examples/media/Space.png +0 -0
- data/examples/media/Star.png +0 -0
- data/examples/media/Starfighter.bmp +0 -0
- data/linux/Makefile.in +98 -0
- data/linux/configure +5658 -0
- data/linux/configure.ac +126 -0
- data/linux/extconf.rb +11 -0
- data/mac/English.lproj/InfoPlist.strings +0 -0
- data/mac/Gosu-Info.plist +26 -0
- data/mac/Gosu.xcodeproj/project.pbxproj +1194 -0
- data/mac/RubyGosu Template-Info.plist +26 -0
- data/mac/libboost_thread_1_34_1_universal.a +0 -0
- data/mac/libboost_thread_d_1_34_1_universal.a +0 -0
- data/mac/libfmod_universal.a +0 -0
- data/mac/libpng_universal.a +0 -0
- data/mac/libz_universal.a +0 -0
- data/reference/Async_8hpp-source.html +70 -0
- data/reference/Audio_8hpp-source.html +114 -0
- data/reference/Audio_8hpp.html +50 -0
- data/reference/AutoLink_8hpp-source.html +38 -0
- data/reference/AutoLink_8hpp.html +34 -0
- data/reference/Bitmap_8hpp-source.html +85 -0
- data/reference/Bitmap_8hpp.html +58 -0
- data/reference/ButtonsMac_8hpp-source.html +133 -0
- data/reference/ButtonsWin_8hpp-source.html +133 -0
- data/reference/ButtonsX_8hpp-source.html +134 -0
- data/reference/Color_8hpp-source.html +169 -0
- data/reference/Color_8hpp.html +85 -0
- data/reference/Directories_8hpp-source.html +42 -0
- data/reference/Directories_8hpp.html +46 -0
- data/reference/Font_8hpp-source.html +65 -0
- data/reference/Font_8hpp.html +41 -0
- data/reference/Fwd_8hpp-source.html +52 -0
- data/reference/Fwd_8hpp.html +37 -0
- data/reference/Gosu_8hpp-source.html +48 -0
- data/reference/Gosu_8hpp.html +34 -0
- data/reference/GraphicsBase_8hpp-source.html +57 -0
- data/reference/GraphicsBase_8hpp.html +56 -0
- data/reference/Graphics_8hpp-source.html +96 -0
- data/reference/Graphics_8hpp.html +53 -0
- data/reference/IO_8hpp-source.html +255 -0
- data/reference/IO_8hpp.html +74 -0
- data/reference/ImageData_8hpp-source.html +62 -0
- data/reference/ImageData_8hpp.html +43 -0
- data/reference/Image_8hpp-source.html +126 -0
- data/reference/Image_8hpp.html +48 -0
- data/reference/Input_8hpp-source.html +118 -0
- data/reference/Input_8hpp.html +50 -0
- data/reference/Math_8hpp-source.html +92 -0
- data/reference/Math_8hpp.html +74 -0
- data/reference/Platform_8hpp-source.html +83 -0
- data/reference/Platform_8hpp.html +73 -0
- data/reference/RotFlip_8hpp-source.html +138 -0
- data/reference/RotFlip_8hpp.html +77 -0
- data/reference/Sockets_8hpp-source.html +130 -0
- data/reference/Sockets_8hpp.html +66 -0
- data/reference/TextInput_8hpp-source.html +64 -0
- data/reference/TextInput_8hpp.html +41 -0
- data/reference/Text_8hpp-source.html +51 -0
- data/reference/Text_8hpp.html +46 -0
- data/reference/Timing_8hpp-source.html +36 -0
- data/reference/Timing_8hpp.html +42 -0
- data/reference/Utility_8hpp-source.html +44 -0
- data/reference/Utility_8hpp.html +48 -0
- data/reference/WinUtility_8hpp-source.html +79 -0
- data/reference/WinUtility_8hpp.html +64 -0
- data/reference/Window_8hpp-source.html +91 -0
- data/reference/Window_8hpp.html +41 -0
- data/reference/annotated.html +51 -0
- data/reference/classGosu_1_1Audio-members.html +34 -0
- data/reference/classGosu_1_1Audio.html +46 -0
- data/reference/classGosu_1_1Bitmap-members.html +44 -0
- data/reference/classGosu_1_1Bitmap.html +263 -0
- data/reference/classGosu_1_1Buffer-members.html +44 -0
- data/reference/classGosu_1_1Buffer.html +78 -0
- data/reference/classGosu_1_1Buffer.png +0 -0
- data/reference/classGosu_1_1Button-members.html +36 -0
- data/reference/classGosu_1_1Button.html +143 -0
- data/reference/classGosu_1_1Color-members.html +56 -0
- data/reference/classGosu_1_1Color.html +387 -0
- data/reference/classGosu_1_1File-members.html +41 -0
- data/reference/classGosu_1_1File.html +69 -0
- data/reference/classGosu_1_1File.png +0 -0
- data/reference/classGosu_1_1Font-members.html +39 -0
- data/reference/classGosu_1_1Font.html +309 -0
- data/reference/classGosu_1_1Graphics-members.html +50 -0
- data/reference/classGosu_1_1Graphics.html +234 -0
- data/reference/classGosu_1_1Image-members.html +45 -0
- data/reference/classGosu_1_1Image.html +518 -0
- data/reference/classGosu_1_1ImageData-members.html +37 -0
- data/reference/classGosu_1_1ImageData.html +60 -0
- data/reference/classGosu_1_1Input-members.html +44 -0
- data/reference/classGosu_1_1Input.html +223 -0
- data/reference/classGosu_1_1MessageSocket-members.html +40 -0
- data/reference/classGosu_1_1MessageSocket.html +233 -0
- data/reference/classGosu_1_1Resource-members.html +39 -0
- data/reference/classGosu_1_1Resource.html +116 -0
- data/reference/classGosu_1_1Resource.png +0 -0
- data/reference/classGosu_1_1Sample-members.html +37 -0
- data/reference/classGosu_1_1Sample.html +200 -0
- data/reference/classGosu_1_1SampleInstance-members.html +38 -0
- data/reference/classGosu_1_1SampleInstance.html +169 -0
- data/reference/classGosu_1_1Song-members.html +43 -0
- data/reference/classGosu_1_1Song.html +260 -0
- data/reference/classGosu_1_1TextInput-members.html +38 -0
- data/reference/classGosu_1_1TextInput.html +121 -0
- data/reference/classGosu_1_1Window-members.html +50 -0
- data/reference/classGosu_1_1Window.html +271 -0
- data/reference/doxyfile +233 -0
- data/reference/doxygen.css +433 -0
- data/reference/doxygen.png +0 -0
- data/reference/files.html +54 -0
- data/reference/functions.html +236 -0
- data/reference/functions_enum.html +45 -0
- data/reference/functions_func.html +227 -0
- data/reference/functions_vars.html +47 -0
- data/reference/hierarchy.html +53 -0
- data/reference/index.html +26 -0
- data/reference/namespaceGosu.html +2890 -0
- data/reference/namespaceGosu_1_1Colors.html +70 -0
- data/reference/namespaceGosu_1_1Win.html +275 -0
- data/reference/namespacemembers.html +216 -0
- data/reference/namespacemembers_enum.html +52 -0
- data/reference/namespacemembers_eval.html +54 -0
- data/reference/namespacemembers_func.html +185 -0
- data/reference/namespacemembers_type.html +46 -0
- data/reference/namespacemembers_vars.html +46 -0
- data/reference/namespaces.html +35 -0
- data/reference/tab_b.gif +0 -0
- data/reference/tab_l.gif +0 -0
- data/reference/tab_r.gif +0 -0
- data/reference/tabs.css +102 -0
- data/windows/Gosu.sln +29 -0
- data/windows/Gosu.vcproj +553 -0
- data/windows/RubyGosu.vcproj +138 -0
- 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
|
+
}*/
|