Skip to content

Commit 8299d4f

Browse files
committed
Add Network module and make related changes
1 parent 6b8861e commit 8299d4f

File tree

7 files changed

+3331
-9
lines changed

7 files changed

+3331
-9
lines changed

generate/generate.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ def rename_type(name, var=''):
5959
'size_t': 'SizeT',
6060
'sfBool': 'CSFML::Bool',
6161
'unsigned int': 'Int32',
62+
'unsigned short': 'UInt16',
6263
'float': 'Float32',
64+
'double': 'Float64',
6365
'sfVector2u': 'sfVector2i',
6466
}.get(name, name)
6567
if ptr and 'sf' in name:
@@ -117,6 +119,11 @@ def get_doc(indent=0):
117119
'SoundStatus': 'SoundSource',
118120
'PrimitiveType': '',
119121
'WindowStyle': '',
122+
'FtpTransferMode': 'Ftp',
123+
'FtpStatus': 'FtpResponse',
124+
'HttpMethod': 'HttpRequest',
125+
'HttpStatus': 'HttpResponse',
126+
'SocketStatus': 'Socket',
120127
}
121128
def handle_enum(name, items):
122129
if name is None:
@@ -290,9 +297,16 @@ def handle_function(main, params):
290297
const = []
291298
sgn = main_sgn
292299

293-
for ptype, pname in params:
300+
anon_index = 0
301+
for i, (ptype, pname) in enumerate(params, 1):
302+
if 'wchar_t' in ptype:
303+
return
294304
rtype = rename_type(ptype, pname)
295-
rname = rename_identifier(pname) or 'p{}'.format(i)
305+
if pname:
306+
rname = rename_identifier(pname)
307+
else:
308+
anon_index += 1
309+
rname = 'p{}'.format(anon_index)
296310
if rtype=='UInt32*':
297311
rtype = 'Char*'
298312
elif rtype=='UInt32':
@@ -353,6 +367,9 @@ def handle_function(main, params):
353367
elif t == 'Int64':
354368
t = 'Int'
355369
conv.append('{0} = {0}.to_i64'.format(n))
370+
elif t == 'UInt16':
371+
t = 'Int'
372+
conv.append('{0} = {0}.to_u16'.format(n))
356373
elif t in ['Vector2f', 'Vector2i']:
357374
conv.append('{0} = SF.{2}({0}) unless {0}.is_a? {1}'.format(n, t, t.lower()))
358375
t = None
@@ -384,7 +401,7 @@ def handle_function(main, params):
384401
obj(cls, ' '+line)
385402
if aparams:
386403
lparams = ', '.join(([] if not cut else ['@this' if cls in classes else ('pointerof(cself)' if p1.endswith('*') else 'self')]) + [n for n, t in oparams])
387-
if cls not in classes and p1.endswith('*'):
404+
if cls in structs and p1.endswith('*'):
388405
obj(cls, ' cself = self')
389406
else:
390407
lparams = ''
@@ -406,6 +423,8 @@ def handle_function(main, params):
406423
obj(cls, ' result += ptr[i]; i += 1')
407424
obj(cls, ' end')
408425
obj(cls, ' result')
426+
elif nftype == 'UInt8*' and not nfname.endswith('_ptr'):
427+
obj(cls, ' String.new({})'.format(call))
409428
elif ftype == 'sfBool':
410429
obj(cls, ' {} != 0'.format(call))
411430
elif 'Vector2' in ftype:
@@ -585,7 +604,7 @@ def obj(cls, *args):
585604
ast = parse_file('headers_gen.h')
586605
Visitor().visit(ast)
587606

588-
deps = {'system': [], 'window': ['system'], 'graphics': ['system', 'window'], 'audio': ['system']}
607+
deps = {'system': [], 'window': ['system'], 'graphics': ['system', 'window'], 'audio': ['system'], 'network': ['system']}
589608
for mod, lines in libs.items():
590609
with open('{}_lib.cr'.format(mod), 'w') as f:
591610
f.write('\n'.join(lines[0]))

generate/headers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929

3030

3131
src = ['''
32-
typedef unsigned long sfWindowHandle;
32+
typedef int sfWindowHandle;
3333
typedef int size_t;
34+
typedef int wchar_t;
3435
''']
3536
doc = []
3637

@@ -87,7 +88,7 @@ def visit_header(file_path):
8788
src.append('\n')
8889

8990

90-
for m in ['system', 'window', 'graphics', 'audio']:
91+
for m in ['system', 'window', 'graphics', 'audio', 'network']:
9192
visit_header('SFML/{}.h'.format(m.capitalize()))
9293

9394

src/audio_obj.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ module SF
11901190
#
11911191
# *Returns*: The name of the default audio capture device (null terminated)
11921192
def self.get_default_device()
1193-
CSFML.sound_recorder_get_default_device()
1193+
String.new(CSFML.sound_recorder_get_default_device())
11941194
end
11951195

11961196
# Set the audio capture device
@@ -1218,7 +1218,7 @@ module SF
12181218
#
12191219
# *Returns*: The name of the current audio capture device
12201220
def device
1221-
CSFML.sound_recorder_get_device(@this)
1221+
String.new(CSFML.sound_recorder_get_device(@this))
12221222
end
12231223

12241224
end

src/common_obj.cr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ module SF
4040
# Put the pointer into the wrapper object.
4141
# The pointer will **not** be freed on GC.
4242
def self.wrap_ptr(ptr)
43-
ptr ? new(ptr, false) : nil
43+
#ptr ? new(ptr, false) : nil
44+
new(ptr, false)
4445
end
4546

4647
# Transfer ownership of the pointer to the wrapper object.

src/network.cr

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright (C) 2015 Oleh Prypin <[email protected]>
2+
#
3+
# This file is part of CrSFML.
4+
#
5+
# This software is provided 'as-is', without any express or implied
6+
# warranty. In no event will the authors be held liable for any damages
7+
# arising from the use of this software.
8+
#
9+
# Permission is granted to anyone to use this software for any purpose,
10+
# including commercial applications, and to alter it and redistribute it
11+
# freely, subject to the following restrictions:
12+
#
13+
# 1. The origin of this software must not be misrepresented; you must not
14+
# claim that you wrote the original software. If you use this software
15+
# in a product, an acknowledgement in the product documentation would be
16+
# appreciated but is not required.
17+
# 2. Altered source versions must be plainly marked as such, and must not be
18+
# misrepresented as being the original software.
19+
# 3. This notice may not be removed or altered from any source distribution.
20+
21+
require "./network_lib"
22+
require "./system"
23+
24+
module SF
25+
extend self
26+
27+
class Http
28+
def initialize(host: String, port=0)
29+
initialize()
30+
set_host(host, port)
31+
end
32+
def set_host(host: String)
33+
set_host(host, 0)
34+
end
35+
36+
def send_request(request: SF::HttpRequest)
37+
send_request(request, SF.milliseconds(0))
38+
end
39+
end
40+
41+
class HttpRequest
42+
def initialize(uri: String, method=HttpRequest::Get, body="")
43+
initialize()
44+
self.uri = uri
45+
self.method = method
46+
self.body = body
47+
end
48+
end
49+
50+
class SocketSelector
51+
def wait()
52+
wait(SF::Time::Zero)
53+
end
54+
end
55+
56+
class TcpSocket
57+
def connect(host: IpAddress, port: Int)
58+
connect(host, port, SF::Time::Zero)
59+
end
60+
61+
def send_partial(data: Void*, size: Int): {SocketStatus, Int}
62+
r = CSFML.tcp_socket_send_partial(@this, data, CSFML::SizeT.cast(size), out sent)
63+
{r, sent}
64+
end
65+
66+
def receive(data: Void*, max_size: Int): {SocketStatus, Int}
67+
r = CSFML.tcp_socket_receive(@this, data, CSFML::SizeT.cast(max_size), out size_received)
68+
{r, size_received}
69+
end
70+
end
71+
72+
class TcpListener
73+
def accept(): {SocketStatus, TcpSocket}
74+
r = CSFML.tcp_listener_accept(@this, out connected)
75+
{r, connected}
76+
end
77+
end
78+
79+
class UdpSocket
80+
def receive(data: Void*, max_size: Int, size_received: SizeT*, address: IpAddress*, port: UInt16*): {SocketStatus, Int, IpAddress, UInt16}
81+
r = CSFML.udp_socket_receive(@this, data, CSFML::SizeT.cast(max_size), out size_received, out address, out port)
82+
{r, size_received, address, port}
83+
end
84+
end
85+
end
86+
87+
require "./network_obj"

0 commit comments

Comments
 (0)