1 /** 2 Abstraction and base class for stream-based protocols. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.streampeer; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.traits; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.globalenums; 22 import godot.object; 23 import godot.reference; 24 /** 25 Abstraction and base class for stream-based protocols. 26 27 StreamPeer is an abstraction and base class for stream-based protocols (such as TCP). It provides an API for sending and receiving data through streams as raw data or strings. 28 */ 29 @GodotBaseClass struct StreamPeer 30 { 31 package(godot) enum string _GODOT_internal_name = "StreamPeer"; 32 public: 33 @nogc nothrow: 34 union { /** */ godot_object _godot_object; /** */ Reference _GODOT_base; } 35 alias _GODOT_base this; 36 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 37 package(godot) __gshared bool _classBindingInitialized = false; 38 package(godot) static struct GDNativeClassBinding 39 { 40 __gshared: 41 @GodotName("get_16") GodotMethod!(long) get16; 42 @GodotName("get_32") GodotMethod!(long) get32; 43 @GodotName("get_64") GodotMethod!(long) get64; 44 @GodotName("get_8") GodotMethod!(long) get8; 45 @GodotName("get_available_bytes") GodotMethod!(long) getAvailableBytes; 46 @GodotName("get_data") GodotMethod!(Array, long) getData; 47 @GodotName("get_double") GodotMethod!(double) getDouble; 48 @GodotName("get_float") GodotMethod!(double) getFloat; 49 @GodotName("get_partial_data") GodotMethod!(Array, long) getPartialData; 50 @GodotName("get_string") GodotMethod!(String, long) getString; 51 @GodotName("get_u16") GodotMethod!(long) getU16; 52 @GodotName("get_u32") GodotMethod!(long) getU32; 53 @GodotName("get_u64") GodotMethod!(long) getU64; 54 @GodotName("get_u8") GodotMethod!(long) getU8; 55 @GodotName("get_utf8_string") GodotMethod!(String, long) getUtf8String; 56 @GodotName("get_var") GodotMethod!(Variant, bool) getVar; 57 @GodotName("is_big_endian_enabled") GodotMethod!(bool) isBigEndianEnabled; 58 @GodotName("put_16") GodotMethod!(void, long) put16; 59 @GodotName("put_32") GodotMethod!(void, long) put32; 60 @GodotName("put_64") GodotMethod!(void, long) put64; 61 @GodotName("put_8") GodotMethod!(void, long) put8; 62 @GodotName("put_data") GodotMethod!(GodotError, PoolByteArray) putData; 63 @GodotName("put_double") GodotMethod!(void, double) putDouble; 64 @GodotName("put_float") GodotMethod!(void, double) putFloat; 65 @GodotName("put_partial_data") GodotMethod!(Array, PoolByteArray) putPartialData; 66 @GodotName("put_string") GodotMethod!(void, String) putString; 67 @GodotName("put_u16") GodotMethod!(void, long) putU16; 68 @GodotName("put_u32") GodotMethod!(void, long) putU32; 69 @GodotName("put_u64") GodotMethod!(void, long) putU64; 70 @GodotName("put_u8") GodotMethod!(void, long) putU8; 71 @GodotName("put_utf8_string") GodotMethod!(void, String) putUtf8String; 72 @GodotName("put_var") GodotMethod!(void, Variant, bool) putVar; 73 @GodotName("set_big_endian") GodotMethod!(void, bool) setBigEndian; 74 } 75 /// 76 pragma(inline, true) bool opEquals(in StreamPeer other) const 77 { return _godot_object.ptr is other._godot_object.ptr; } 78 /// 79 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 80 { _godot_object.ptr = n; return null; } 81 /// 82 pragma(inline, true) bool opEquals(typeof(null) n) const 83 { return _godot_object.ptr is n; } 84 /// 85 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 86 mixin baseCasts; 87 /// Construct a new instance of StreamPeer. 88 /// Note: use `memnew!StreamPeer` instead. 89 static StreamPeer _new() 90 { 91 static godot_class_constructor constructor; 92 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("StreamPeer"); 93 if(constructor is null) return typeof(this).init; 94 return cast(StreamPeer)(constructor()); 95 } 96 @disable new(size_t s); 97 /** 98 Gets a signed 16-bit value from the stream. 99 */ 100 long get16() 101 { 102 checkClassBinding!(typeof(this))(); 103 return ptrcall!(long)(GDNativeClassBinding.get16, _godot_object); 104 } 105 /** 106 Gets a signed 32-bit value from the stream. 107 */ 108 long get32() 109 { 110 checkClassBinding!(typeof(this))(); 111 return ptrcall!(long)(GDNativeClassBinding.get32, _godot_object); 112 } 113 /** 114 Gets a signed 64-bit value from the stream. 115 */ 116 long get64() 117 { 118 checkClassBinding!(typeof(this))(); 119 return ptrcall!(long)(GDNativeClassBinding.get64, _godot_object); 120 } 121 /** 122 Gets a signed byte from the stream. 123 */ 124 long get8() 125 { 126 checkClassBinding!(typeof(this))(); 127 return ptrcall!(long)(GDNativeClassBinding.get8, _godot_object); 128 } 129 /** 130 Returns the amount of bytes this $(D StreamPeer) has available. 131 */ 132 long getAvailableBytes() const 133 { 134 checkClassBinding!(typeof(this))(); 135 return ptrcall!(long)(GDNativeClassBinding.getAvailableBytes, _godot_object); 136 } 137 /** 138 Returns a chunk data with the received bytes. The amount of bytes to be received can be requested in the `bytes` argument. If not enough bytes are available, the function will block until the desired amount is received. This function returns two values, an $(D @GlobalScope.error) code and a data array. 139 */ 140 Array getData(in long bytes) 141 { 142 checkClassBinding!(typeof(this))(); 143 return ptrcall!(Array)(GDNativeClassBinding.getData, _godot_object, bytes); 144 } 145 /** 146 Gets a double-precision float from the stream. 147 */ 148 double getDouble() 149 { 150 checkClassBinding!(typeof(this))(); 151 return ptrcall!(double)(GDNativeClassBinding.getDouble, _godot_object); 152 } 153 /** 154 Gets a single-precision float from the stream. 155 */ 156 double getFloat() 157 { 158 checkClassBinding!(typeof(this))(); 159 return ptrcall!(double)(GDNativeClassBinding.getFloat, _godot_object); 160 } 161 /** 162 Returns a chunk data with the received bytes. The amount of bytes to be received can be requested in the "bytes" argument. If not enough bytes are available, the function will return how many were actually received. This function returns two values, an $(D @GlobalScope.error) code, and a data array. 163 */ 164 Array getPartialData(in long bytes) 165 { 166 checkClassBinding!(typeof(this))(); 167 return ptrcall!(Array)(GDNativeClassBinding.getPartialData, _godot_object, bytes); 168 } 169 /** 170 Gets a string with byte-length `bytes` from the stream. If `bytes` is negative (default) the length will be read from the stream using the reverse process of $(D putString). 171 */ 172 String getString(in long bytes = -1) 173 { 174 checkClassBinding!(typeof(this))(); 175 return ptrcall!(String)(GDNativeClassBinding.getString, _godot_object, bytes); 176 } 177 /** 178 Gets an unsigned 16-bit value from the stream. 179 */ 180 long getU16() 181 { 182 checkClassBinding!(typeof(this))(); 183 return ptrcall!(long)(GDNativeClassBinding.getU16, _godot_object); 184 } 185 /** 186 Gets an unsigned 32-bit value from the stream. 187 */ 188 long getU32() 189 { 190 checkClassBinding!(typeof(this))(); 191 return ptrcall!(long)(GDNativeClassBinding.getU32, _godot_object); 192 } 193 /** 194 Gets an unsigned 64-bit value from the stream. 195 */ 196 long getU64() 197 { 198 checkClassBinding!(typeof(this))(); 199 return ptrcall!(long)(GDNativeClassBinding.getU64, _godot_object); 200 } 201 /** 202 Gets an unsigned byte from the stream. 203 */ 204 long getU8() 205 { 206 checkClassBinding!(typeof(this))(); 207 return ptrcall!(long)(GDNativeClassBinding.getU8, _godot_object); 208 } 209 /** 210 Gets an UTF-8 string with byte-length `bytes` from the stream (this decodes the string sent as UTF-8). If `bytes` is negative (default) the length will be read from the stream using the reverse process of $(D putUtf8String). 211 */ 212 String getUtf8String(in long bytes = -1) 213 { 214 checkClassBinding!(typeof(this))(); 215 return ptrcall!(String)(GDNativeClassBinding.getUtf8String, _godot_object, bytes); 216 } 217 /** 218 Gets a Variant from the stream. If `allow_objects` is `true`, decoding objects is allowed. 219 $(B Warning:) Deserialized objects can contain code which gets executed. Do not use this option if the serialized object comes from untrusted sources to avoid potential security threats such as remote code execution. 220 */ 221 Variant getVar(in bool allow_objects = false) 222 { 223 checkClassBinding!(typeof(this))(); 224 return ptrcall!(Variant)(GDNativeClassBinding.getVar, _godot_object, allow_objects); 225 } 226 /** 227 228 */ 229 bool isBigEndianEnabled() const 230 { 231 checkClassBinding!(typeof(this))(); 232 return ptrcall!(bool)(GDNativeClassBinding.isBigEndianEnabled, _godot_object); 233 } 234 /** 235 Puts a signed 16-bit value into the stream. 236 */ 237 void put16(in long value) 238 { 239 checkClassBinding!(typeof(this))(); 240 ptrcall!(void)(GDNativeClassBinding.put16, _godot_object, value); 241 } 242 /** 243 Puts a signed 32-bit value into the stream. 244 */ 245 void put32(in long value) 246 { 247 checkClassBinding!(typeof(this))(); 248 ptrcall!(void)(GDNativeClassBinding.put32, _godot_object, value); 249 } 250 /** 251 Puts a signed 64-bit value into the stream. 252 */ 253 void put64(in long value) 254 { 255 checkClassBinding!(typeof(this))(); 256 ptrcall!(void)(GDNativeClassBinding.put64, _godot_object, value); 257 } 258 /** 259 Puts a signed byte into the stream. 260 */ 261 void put8(in long value) 262 { 263 checkClassBinding!(typeof(this))(); 264 ptrcall!(void)(GDNativeClassBinding.put8, _godot_object, value); 265 } 266 /** 267 Sends a chunk of data through the connection, blocking if necessary until the data is done sending. This function returns an $(D @GlobalScope.error) code. 268 */ 269 GodotError putData(in PoolByteArray data) 270 { 271 checkClassBinding!(typeof(this))(); 272 return ptrcall!(GodotError)(GDNativeClassBinding.putData, _godot_object, data); 273 } 274 /** 275 Puts a double-precision float into the stream. 276 */ 277 void putDouble(in double value) 278 { 279 checkClassBinding!(typeof(this))(); 280 ptrcall!(void)(GDNativeClassBinding.putDouble, _godot_object, value); 281 } 282 /** 283 Puts a single-precision float into the stream. 284 */ 285 void putFloat(in double value) 286 { 287 checkClassBinding!(typeof(this))(); 288 ptrcall!(void)(GDNativeClassBinding.putFloat, _godot_object, value); 289 } 290 /** 291 Sends a chunk of data through the connection. If all the data could not be sent at once, only part of it will. This function returns two values, an $(D @GlobalScope.error) code and an integer, describing how much data was actually sent. 292 */ 293 Array putPartialData(in PoolByteArray data) 294 { 295 checkClassBinding!(typeof(this))(); 296 return ptrcall!(Array)(GDNativeClassBinding.putPartialData, _godot_object, data); 297 } 298 /** 299 Puts a zero-terminated ASCII string into the stream prepended by a 32-bit unsigned integer representing its size. 300 Note: To put an ASCII string without prepending its size, you can use $(D putData): 301 302 303 put_data("Hello world".to_ascii()) 304 305 306 */ 307 void putString(in String value) 308 { 309 checkClassBinding!(typeof(this))(); 310 ptrcall!(void)(GDNativeClassBinding.putString, _godot_object, value); 311 } 312 /** 313 Puts an unsigned 16-bit value into the stream. 314 */ 315 void putU16(in long value) 316 { 317 checkClassBinding!(typeof(this))(); 318 ptrcall!(void)(GDNativeClassBinding.putU16, _godot_object, value); 319 } 320 /** 321 Puts an unsigned 32-bit value into the stream. 322 */ 323 void putU32(in long value) 324 { 325 checkClassBinding!(typeof(this))(); 326 ptrcall!(void)(GDNativeClassBinding.putU32, _godot_object, value); 327 } 328 /** 329 Puts an unsigned 64-bit value into the stream. 330 */ 331 void putU64(in long value) 332 { 333 checkClassBinding!(typeof(this))(); 334 ptrcall!(void)(GDNativeClassBinding.putU64, _godot_object, value); 335 } 336 /** 337 Puts an unsigned byte into the stream. 338 */ 339 void putU8(in long value) 340 { 341 checkClassBinding!(typeof(this))(); 342 ptrcall!(void)(GDNativeClassBinding.putU8, _godot_object, value); 343 } 344 /** 345 Puts a zero-terminated UTF-8 string into the stream prepended by a 32 bits unsigned integer representing its size. 346 Note: To put an UTF-8 string without prepending its size, you can use $(D putData): 347 348 349 put_data("Hello world".to_utf8()) 350 351 352 */ 353 void putUtf8String(in String value) 354 { 355 checkClassBinding!(typeof(this))(); 356 ptrcall!(void)(GDNativeClassBinding.putUtf8String, _godot_object, value); 357 } 358 /** 359 Puts a Variant into the stream. If `full_objects` is `true` encoding objects is allowed (and can potentially include code). 360 */ 361 void putVar(VariantArg0)(in VariantArg0 value, in bool full_objects = false) 362 { 363 checkClassBinding!(typeof(this))(); 364 ptrcall!(void)(GDNativeClassBinding.putVar, _godot_object, value, full_objects); 365 } 366 /** 367 368 */ 369 void setBigEndian(in bool enable) 370 { 371 checkClassBinding!(typeof(this))(); 372 ptrcall!(void)(GDNativeClassBinding.setBigEndian, _godot_object, enable); 373 } 374 /** 375 If `true`, this $(D StreamPeer) will using big-endian format for encoding and decoding. 376 */ 377 @property bool bigEndian() 378 { 379 return isBigEndianEnabled(); 380 } 381 /// ditto 382 @property void bigEndian(bool v) 383 { 384 setBigEndian(v); 385 } 386 }