diff options
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/gles3/rasterizer_canvas_gles3.cpp | 4 | ||||
-rw-r--r-- | drivers/gles3/rasterizer_gles3.cpp | 37 | ||||
-rw-r--r-- | drivers/gles3/rasterizer_scene_gles3.cpp | 10 | ||||
-rw-r--r-- | drivers/gles3/rasterizer_storage_gles3.cpp | 1 | ||||
-rw-r--r-- | drivers/gles3/shader_compiler_gles3.cpp | 1 | ||||
-rw-r--r-- | drivers/gles3/shader_gles3.cpp | 31 | ||||
-rw-r--r-- | drivers/gles3/shaders/canvas.glsl | 2 | ||||
-rw-r--r-- | drivers/gles3/shaders/scene.glsl | 92 | ||||
-rw-r--r-- | drivers/unix/packet_peer_udp_posix.cpp | 2 | ||||
-rw-r--r-- | drivers/unix/packet_peer_udp_posix.h | 12 | ||||
-rw-r--r-- | drivers/windows/packet_peer_udp_winsock.cpp | 297 | ||||
-rw-r--r-- | drivers/windows/packet_peer_udp_winsock.h | 88 | ||||
-rw-r--r-- | drivers/windows/stream_peer_tcp_winsock.cpp | 371 | ||||
-rw-r--r-- | drivers/windows/stream_peer_tcp_winsock.h | 91 | ||||
-rw-r--r-- | drivers/windows/tcp_server_winsock.cpp | 187 | ||||
-rw-r--r-- | drivers/windows/tcp_server_winsock.h | 60 |
16 files changed, 1154 insertions, 132 deletions
diff --git a/drivers/gles3/rasterizer_canvas_gles3.cpp b/drivers/gles3/rasterizer_canvas_gles3.cpp index 309497c938..a4daa77b50 100644 --- a/drivers/gles3/rasterizer_canvas_gles3.cpp +++ b/drivers/gles3/rasterizer_canvas_gles3.cpp @@ -1545,7 +1545,7 @@ void RasterizerCanvasGLES3::reset_canvas() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); //use for reading from screen - if (storage->frame.current_rt) { + if (storage->frame.current_rt && !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_NO_SAMPLING]) { glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3); glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); } @@ -1784,6 +1784,8 @@ void RasterizerCanvasGLES3::initialize() { state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_RGBA_SHADOWS, storage->config.use_rgba_2d_shadows); state.canvas_shadow_shader.set_conditional(CanvasShadowShaderGLES3::USE_RGBA_SHADOWS, storage->config.use_rgba_2d_shadows); + + state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_PIXEL_SNAP, GLOBAL_DEF("rendering/quality/2d/use_pixel_snap", false)); } void RasterizerCanvasGLES3::finalize() { diff --git a/drivers/gles3/rasterizer_gles3.cpp b/drivers/gles3/rasterizer_gles3.cpp index cd0adbd0d1..6b827002f8 100644 --- a/drivers/gles3/rasterizer_gles3.cpp +++ b/drivers/gles3/rasterizer_gles3.cpp @@ -354,47 +354,10 @@ void RasterizerGLES3::blit_render_target_to_screen(RID p_render_target, const Re void RasterizerGLES3::end_frame(bool p_swap_buffers) { -#if 0 - canvas->canvas_begin(); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D,storage->resources.white_tex); - glDisable(GL_BLEND); - glDisable(GL_DEPTH_TEST); - glDisable(GL_CULL_FACE); - - - float vtx[8]={0,0, - 0,1, - 1,1, - 1,0 - }; - - glBindBuffer(GL_ARRAY_BUFFER,0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0); - - glEnableVertexAttribArray(VS::ARRAY_VERTEX); - glVertexAttribPointer( VS::ARRAY_VERTEX, 2 ,GL_FLOAT, false, 0, vtx ); - - - //glBindBuffer(GL_ARRAY_BUFFER,canvas->data.canvas_quad_vertices); - //glEnableVertexAttribArray(VS::ARRAY_VERTEX); - //glVertexAttribPointer( VS::ARRAY_VERTEX, 2 ,GL_FLOAT, false, 0, 0 ); - - glBindVertexArray(canvas->data.canvas_quad_array); - - canvas->draw_generic_textured_rect(Rect2(0,0,15,15),Rect2(0,0,1,1)); -#endif if (p_swap_buffers) OS::get_singleton()->swap_buffers(); else glFinish(); - - /* print_line("objects: "+itos(storage->info.render_object_count)); - print_line("material chages: "+itos(storage->info.render_material_switch_count)); - print_line("surface changes: "+itos(storage->info.render_surface_switch_count)); - print_line("shader changes: "+itos(storage->info.render_shader_rebind_count)); - print_line("vertices: "+itos(storage->info.render_vertices_count)); -*/ } void RasterizerGLES3::finalize() { diff --git a/drivers/gles3/rasterizer_scene_gles3.cpp b/drivers/gles3/rasterizer_scene_gles3.cpp index 13fbb5c293..b777f9343a 100644 --- a/drivers/gles3/rasterizer_scene_gles3.cpp +++ b/drivers/gles3/rasterizer_scene_gles3.cpp @@ -1557,7 +1557,7 @@ void RasterizerSceneGLES3::_render_geometry(RenderList::Element *e) { glEnableVertexAttribArray(VS::ARRAY_NORMAL); glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector3) * vertices, c.normals.ptr()); - glVertexAttribPointer(VS::ARRAY_NORMAL, 3, GL_FLOAT, false, sizeof(Vector3) * vertices, ((uint8_t *)NULL) + buf_ofs); + glVertexAttribPointer(VS::ARRAY_NORMAL, 3, GL_FLOAT, false, sizeof(Vector3), ((uint8_t *)NULL) + buf_ofs); buf_ofs += sizeof(Vector3) * vertices; } else { @@ -1569,7 +1569,7 @@ void RasterizerSceneGLES3::_render_geometry(RenderList::Element *e) { glEnableVertexAttribArray(VS::ARRAY_TANGENT); glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Plane) * vertices, c.tangents.ptr()); - glVertexAttribPointer(VS::ARRAY_TANGENT, 4, GL_FLOAT, false, sizeof(Plane) * vertices, ((uint8_t *)NULL) + buf_ofs); + glVertexAttribPointer(VS::ARRAY_TANGENT, 4, GL_FLOAT, false, sizeof(Plane), ((uint8_t *)NULL) + buf_ofs); buf_ofs += sizeof(Plane) * vertices; } else { @@ -2786,12 +2786,6 @@ void RasterizerSceneGLES3::_setup_lights(RID *p_light_cull_result, int p_light_c copymem(&state.omni_array_tmp[li->light_index * state.ubo_light_size], &ubo_data, state.ubo_light_size); state.omni_light_count++; -#if 0 - if (li->light_ptr->shadow_enabled) { - li->shadow_projection[0] = Transform(camera_transform_inverse * li->transform).inverse(); - lights_use_shadow=true; - } -#endif } break; case VS::LIGHT_SPOT: { diff --git a/drivers/gles3/rasterizer_storage_gles3.cpp b/drivers/gles3/rasterizer_storage_gles3.cpp index ee6c738a05..8ee9e3fdb8 100644 --- a/drivers/gles3/rasterizer_storage_gles3.cpp +++ b/drivers/gles3/rasterizer_storage_gles3.cpp @@ -6048,6 +6048,7 @@ void RasterizerStorageGLES3::_render_target_clear(RenderTarget *rt) { glDeleteTextures(1, &rt->effects.mip_maps[i].color); rt->effects.mip_maps[i].sizes.clear(); rt->effects.mip_maps[i].levels = 0; + rt->effects.mip_maps[i].color = 0; } } diff --git a/drivers/gles3/shader_compiler_gles3.cpp b/drivers/gles3/shader_compiler_gles3.cpp index 21102e8c25..101978548d 100644 --- a/drivers/gles3/shader_compiler_gles3.cpp +++ b/drivers/gles3/shader_compiler_gles3.cpp @@ -812,6 +812,7 @@ ShaderCompilerGLES3::ShaderCompilerGLES3() { //for light actions[VS::SHADER_SPATIAL].renames["VIEW"] = "view"; actions[VS::SHADER_SPATIAL].renames["LIGHT_COLOR"] = "light_color"; + actions[VS::SHADER_SPATIAL].renames["LIGHT"] = "light"; actions[VS::SHADER_SPATIAL].renames["ATTENUATION"] = "attenuation"; actions[VS::SHADER_SPATIAL].renames["DIFFUSE_LIGHT"] = "diffuse_light"; actions[VS::SHADER_SPATIAL].renames["SPECULAR_LIGHT"] = "specular_light"; diff --git a/drivers/gles3/shader_gles3.cpp b/drivers/gles3/shader_gles3.cpp index 9e234f5005..56b1cfdf0f 100644 --- a/drivers/gles3/shader_gles3.cpp +++ b/drivers/gles3/shader_gles3.cpp @@ -279,21 +279,6 @@ ShaderGLES3::Version *ShaderGLES3::get_current_version() { strings.push_back("precision highp sampler2DArray;\n"); #endif -#if 0 - if (cc) { - - String _code_string = "#define VERTEX_SHADER_CODE "+cc->vertex+"\n"; - String _code_globals = "#define VERTEX_SHADER_GLOBALS "+cc->vertex_globals+"\n"; - - code_string=_code_string.ascii(); - code_globals=_code_globals.ascii(); - DEBUG_PRINT( code_globals.get_data() ); - DEBUG_PRINT( code_string.get_data() ); - strings.push_back(code_globals); - strings.push_back(code_string); - } -#endif - strings.push_back(vertex_code0.get_data()); if (cc) { @@ -382,21 +367,6 @@ ShaderGLES3::Version *ShaderGLES3::get_current_version() { strings.push_back("precision highp sampler2DArray;\n"); #endif -#if 0 - if (cc) { - - String _code_string = "#define FRAGMENT_SHADER_CODE "+cc->fragment+"\n"; - String _code_globals = "#define FRAGMENT_SHADER_GLOBALS "+cc->fragment_globals+"\n"; - - code_string=_code_string.ascii(); - code_globals=_code_globals.ascii(); - DEBUG_PRINT( code_globals.get_data() ); - DEBUG_PRINT( code_string.get_data() ); - strings.push_back(code_globals); - strings.push_back(code_string); - } -#endif - strings.push_back(fragment_code0.get_data()); if (cc) { material_string = cc->uniforms.ascii(); @@ -495,7 +465,6 @@ ShaderGLES3::Version *ShaderGLES3::get_current_version() { if (feedbacks[i].conditional == -1 || (1 << feedbacks[i].conditional) & conditional_version.version) { //conditional for this feedback is enabled - print_line("tf varying: " + itos(feedback.size()) + " " + String(feedbacks[i].name)); feedback.push_back(feedbacks[i].name); } } diff --git a/drivers/gles3/shaders/canvas.glsl b/drivers/gles3/shaders/canvas.glsl index 4bbb18ce42..0b8230234b 100644 --- a/drivers/gles3/shaders/canvas.glsl +++ b/drivers/gles3/shaders/canvas.glsl @@ -171,7 +171,7 @@ VERTEX_SHADER_CODE #ifdef USE_PIXEL_SNAP - outvec.xy=floor(outvec+0.5); + outvec.xy=floor(outvec+0.5).xy; #endif diff --git a/drivers/gles3/shaders/scene.glsl b/drivers/gles3/shaders/scene.glsl index 9bc2bc079d..bb9ff29a8e 100644 --- a/drivers/gles3/shaders/scene.glsl +++ b/drivers/gles3/shaders/scene.glsl @@ -296,6 +296,48 @@ void main() { #endif + +#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) + + vec3 binormal = normalize( cross(normal,tangent) * binormalf ); +#endif + +#if defined(ENABLE_UV_INTERP) + uv_interp = uv_attrib; +#endif + +#if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP) + uv2_interp = uv2_attrib; +#endif + +#if defined(USE_INSTANCING) && defined(ENABLE_INSTANCE_CUSTOM) + vec4 instance_custom = instance_custom_data; +#else + vec4 instance_custom = vec4(0.0); +#endif + + highp mat4 local_projection = projection_matrix; + +//using world coordinates +#if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED) + + vertex = world_matrix * vertex; + normal = normalize((world_matrix * vec4(normal,0.0)).xyz); + +#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) + + tangent = normalize((world_matrix * vec4(tangent,0.0)).xyz); + binormal = normalize((world_matrix * vec4(binormal,0.0)).xyz); +#endif +#endif + + float roughness=0.0; + +//defines that make writing custom shaders easier +#define projection_matrix local_projection +#define world_transform world_matrix + + #ifdef USE_SKELETON { //skeleton transform @@ -333,57 +375,13 @@ void main() { texelFetch(skeleton_texture,tex_ofs+ivec2(0,2),0) ) * bone_weights.w; + mat4 bone_matrix = transpose(mat4(m[0],m[1],m[2],vec4(0.0,0.0,0.0,1.0))); - vertex.xyz = vertex * m; - - normal = vec4(normal,0.0) * m; -#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) - tangent.xyz = vec4(tangent.xyz,0.0) * m; -#endif + world_matrix = bone_matrix * world_matrix; } #endif - -#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) - - vec3 binormal = normalize( cross(normal,tangent) * binormalf ); -#endif - -#if defined(ENABLE_UV_INTERP) - uv_interp = uv_attrib; -#endif - -#if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP) - uv2_interp = uv2_attrib; -#endif - -#if defined(USE_INSTANCING) && defined(ENABLE_INSTANCE_CUSTOM) - vec4 instance_custom = instance_custom_data; -#else - vec4 instance_custom = vec4(0.0); -#endif - - highp mat4 modelview = camera_inverse_matrix * world_matrix; - highp mat4 local_projection = projection_matrix; - -//using world coordinates -#if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED) - - vertex = world_matrix * vertex; - normal = normalize((world_matrix * vec4(normal,0.0)).xyz); - -#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) - - tangent = normalize((world_matrix * vec4(tangent,0.0)).xyz); - binormal = normalize((world_matrix * vec4(binormal,0.0)).xyz); -#endif -#endif - - float roughness=0.0; - -//defines that make writing custom shaders easier -#define projection_matrix local_projection -#define world_transform world_matrix + mat4 modelview = camera_inverse_matrix * world_matrix; { VERTEX_SHADER_CODE diff --git a/drivers/unix/packet_peer_udp_posix.cpp b/drivers/unix/packet_peer_udp_posix.cpp index 61d2737555..f6742d8114 100644 --- a/drivers/unix/packet_peer_udp_posix.cpp +++ b/drivers/unix/packet_peer_udp_posix.cpp @@ -65,7 +65,7 @@ int PacketPeerUDPPosix::get_available_packet_count() const { return queue_count; } -Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer, int &r_buffer_size) const { +Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { Error err = const_cast<PacketPeerUDPPosix *>(this)->_poll(false); if (err != OK) diff --git a/drivers/unix/packet_peer_udp_posix.h b/drivers/unix/packet_peer_udp_posix.h index e580d336b2..ad7be5bbe0 100644 --- a/drivers/unix/packet_peer_udp_posix.h +++ b/drivers/unix/packet_peer_udp_posix.h @@ -41,12 +41,12 @@ class PacketPeerUDPPosix : public PacketPeerUDP { PACKET_BUFFER_SIZE = 65536 }; - mutable RingBuffer<uint8_t> rb; + RingBuffer<uint8_t> rb; uint8_t recv_buffer[PACKET_BUFFER_SIZE]; - mutable uint8_t packet_buffer[PACKET_BUFFER_SIZE]; - mutable IP_Address packet_ip; - mutable int packet_port; - mutable int queue_count; + uint8_t packet_buffer[PACKET_BUFFER_SIZE]; + IP_Address packet_ip; + int packet_port; + int queue_count; int sockfd; bool sock_blocking; IP::Type sock_type; @@ -62,7 +62,7 @@ class PacketPeerUDPPosix : public PacketPeerUDP { public: virtual int get_available_packet_count() const; - virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) const; + virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size); virtual int get_max_packet_size() const; diff --git a/drivers/windows/packet_peer_udp_winsock.cpp b/drivers/windows/packet_peer_udp_winsock.cpp new file mode 100644 index 0000000000..119ee68bd2 --- /dev/null +++ b/drivers/windows/packet_peer_udp_winsock.cpp @@ -0,0 +1,297 @@ +/*************************************************************************/ +/* packet_peer_udp_winsock.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifdef WINDOWS_ENABLED + +#include "packet_peer_udp_winsock.h" + +#include <winsock2.h> +#include <ws2tcpip.h> + +#include "drivers/unix/socket_helpers.h" + +int PacketPeerUDPWinsock::get_available_packet_count() const { + + Error err = const_cast<PacketPeerUDPWinsock *>(this)->_poll(false); + if (err != OK) + return 0; + + return queue_count; +} + +Error PacketPeerUDPWinsock::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { + + Error err = const_cast<PacketPeerUDPWinsock *>(this)->_poll(false); + if (err != OK) + return err; + if (queue_count == 0) + return ERR_UNAVAILABLE; + + uint32_t size; + uint8_t type; + rb.read(&type, 1, true); + if (type == IP::TYPE_IPV4) { + uint8_t ip[4]; + rb.read(ip, 4, true); + packet_ip.set_ipv4(ip); + } else { + uint8_t ip[16]; + rb.read(ip, 16, true); + packet_ip.set_ipv6(ip); + }; + rb.read((uint8_t *)&packet_port, 4, true); + rb.read((uint8_t *)&size, 4, true); + rb.read(packet_buffer, size, true); + --queue_count; + *r_buffer = packet_buffer; + r_buffer_size = size; + return OK; +} +Error PacketPeerUDPWinsock::put_packet(const uint8_t *p_buffer, int p_buffer_size) { + + ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED); + + if (sock_type == IP::TYPE_NONE) + sock_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; + + int sock = _get_socket(); + ERR_FAIL_COND_V(sock == -1, FAILED); + struct sockaddr_storage addr; + size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, sock_type); + + _set_sock_blocking(blocking); + + errno = 0; + int err; + while ((err = sendto(sock, (const char *)p_buffer, p_buffer_size, 0, (struct sockaddr *)&addr, addr_size)) != p_buffer_size) { + + if (WSAGetLastError() != WSAEWOULDBLOCK) { + return FAILED; + } else if (!blocking) { + return ERR_UNAVAILABLE; + } + } + + return OK; +} + +int PacketPeerUDPWinsock::get_max_packet_size() const { + + return 512; // uhm maybe not +} + +void PacketPeerUDPWinsock::_set_sock_blocking(bool p_blocking) { + + if (sock_blocking == p_blocking) + return; + + sock_blocking = p_blocking; + unsigned long par = sock_blocking ? 0 : 1; + if (ioctlsocket(sockfd, FIONBIO, &par)) { + perror("setting non-block mode"); + //close(); + //return -1; + }; +} + +Error PacketPeerUDPWinsock::listen(int p_port, const IP_Address &p_bind_address, int p_recv_buffer_size) { + + ERR_FAIL_COND_V(sockfd != -1, ERR_ALREADY_IN_USE); + ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); + + sock_type = IP::TYPE_ANY; + + if (p_bind_address.is_valid()) + sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; + + int sock = _get_socket(); + if (sock == -1) + return ERR_CANT_CREATE; + + struct sockaddr_storage addr = { 0 }; + size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, IP_Address()); + + if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) { + close(); + return ERR_UNAVAILABLE; + } + + printf("UDP Connection listening on port %i\n", p_port); + rb.resize(nearest_shift(p_recv_buffer_size)); + return OK; +} + +void PacketPeerUDPWinsock::close() { + + if (sockfd != -1) + ::closesocket(sockfd); + sockfd = -1; + sock_type = IP::TYPE_NONE; + rb.resize(16); + queue_count = 0; +} + +Error PacketPeerUDPWinsock::wait() { + + return _poll(true); +} +Error PacketPeerUDPWinsock::_poll(bool p_wait) { + + if (sockfd == -1) { + return FAILED; + } + + _set_sock_blocking(p_wait); + + struct sockaddr_storage from = { 0 }; + int len = sizeof(struct sockaddr_storage); + int ret; + while ((ret = recvfrom(sockfd, (char *)recv_buffer, MIN((int)sizeof(recv_buffer), MAX(rb.space_left() - 24, 0)), 0, (struct sockaddr *)&from, &len)) > 0) { + + uint32_t port = 0; + + if (from.ss_family == AF_INET) { + uint8_t type = (uint8_t)IP::TYPE_IPV4; + rb.write(&type, 1); + struct sockaddr_in *sin_from = (struct sockaddr_in *)&from; + rb.write((uint8_t *)&sin_from->sin_addr, 4); + port = ntohs(sin_from->sin_port); + + } else if (from.ss_family == AF_INET6) { + + uint8_t type = (uint8_t)IP::TYPE_IPV6; + rb.write(&type, 1); + + struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from; + rb.write((uint8_t *)&s6_from->sin6_addr, 16); + + port = ntohs(s6_from->sin6_port); + + } else { + // WARN_PRINT("Ignoring packet with unknown address family"); + uint8_t type = (uint8_t)IP::TYPE_NONE; + rb.write(&type, 1); + }; + + rb.write((uint8_t *)&port, 4); + rb.write((uint8_t *)&ret, 4); + rb.write(recv_buffer, ret); + + len = sizeof(struct sockaddr_storage); + ++queue_count; + if (p_wait) + break; + }; + + if (ret == SOCKET_ERROR) { + int error = WSAGetLastError(); + + if (error == WSAEWOULDBLOCK) { + // Expected when doing non-blocking sockets, retry later. + } else if (error == WSAECONNRESET) { + // If the remote target does not accept messages, this error may occur, but is harmless. + // Once the remote target gets available, this message will disappear for new messages. + } else { + close(); + return FAILED; + } + } + + if (ret == 0) { + close(); + return FAILED; + }; + + return OK; +} + +bool PacketPeerUDPWinsock::is_listening() const { + + return sockfd != -1; +} + +IP_Address PacketPeerUDPWinsock::get_packet_address() const { + + return packet_ip; +} + +int PacketPeerUDPWinsock::get_packet_port() const { + + return packet_port; +} + +int PacketPeerUDPWinsock::_get_socket() { + + ERR_FAIL_COND_V(sock_type == IP::TYPE_NONE, -1); + + if (sockfd != -1) + return sockfd; + + sockfd = _socket_create(sock_type, SOCK_DGRAM, IPPROTO_UDP); + + if (sockfd != -1) + _set_sock_blocking(false); + + return sockfd; +} + +void PacketPeerUDPWinsock::set_dest_address(const IP_Address &p_address, int p_port) { + + peer_addr = p_address; + peer_port = p_port; +} + +void PacketPeerUDPWinsock::make_default() { + + PacketPeerUDP::_create = PacketPeerUDPWinsock::_create; +}; + +PacketPeerUDP *PacketPeerUDPWinsock::_create() { + + return memnew(PacketPeerUDPWinsock); +}; + +PacketPeerUDPWinsock::PacketPeerUDPWinsock() { + + blocking = true; + sock_blocking = true; + sockfd = -1; + packet_port = 0; + queue_count = 0; + peer_port = 0; + sock_type = IP::TYPE_NONE; + rb.resize(16); +} + +PacketPeerUDPWinsock::~PacketPeerUDPWinsock() { + + close(); +} + +#endif diff --git a/drivers/windows/packet_peer_udp_winsock.h b/drivers/windows/packet_peer_udp_winsock.h new file mode 100644 index 0000000000..8ce2cff741 --- /dev/null +++ b/drivers/windows/packet_peer_udp_winsock.h @@ -0,0 +1,88 @@ +/*************************************************************************/ +/* packet_peer_udp_winsock.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifdef WINDOWS_ENABLED + +#ifndef PACKET_PEER_UDP_WINSOCK_H +#define PACKET_PEER_UDP_WINSOCK_H + +#include "io/packet_peer_udp.h" +#include "ring_buffer.h" + +class PacketPeerUDPWinsock : public PacketPeerUDP { + + enum { + PACKET_BUFFER_SIZE = 65536 + }; + + RingBuffer<uint8_t> rb; + uint8_t recv_buffer[PACKET_BUFFER_SIZE]; + uint8_t packet_buffer[PACKET_BUFFER_SIZE]; + IP_Address packet_ip; + int packet_port; + int queue_count; + int sockfd; + bool sock_blocking; + IP::Type sock_type; + + IP_Address peer_addr; + int peer_port; + + _FORCE_INLINE_ int _get_socket(); + + static PacketPeerUDP *_create(); + + void _set_sock_blocking(bool p_blocking); + + Error _poll(bool p_wait); + +public: + virtual int get_available_packet_count() const; + virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); + virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size); + + virtual int get_max_packet_size() const; + + virtual Error listen(int p_port, const IP_Address &p_bind_address = IP_Address("*"), int p_recv_buffer_size = 65536); + virtual void close(); + virtual Error wait(); + virtual bool is_listening() const; + + virtual IP_Address get_packet_address() const; + virtual int get_packet_port() const; + + virtual void set_dest_address(const IP_Address &p_address, int p_port); + + static void make_default(); + PacketPeerUDPWinsock(); + ~PacketPeerUDPWinsock(); +}; +#endif // PACKET_PEER_UDP_WINSOCK_H + +#endif diff --git a/drivers/windows/stream_peer_tcp_winsock.cpp b/drivers/windows/stream_peer_tcp_winsock.cpp new file mode 100644 index 0000000000..f4cd38079d --- /dev/null +++ b/drivers/windows/stream_peer_tcp_winsock.cpp @@ -0,0 +1,371 @@ +/*************************************************************************/ +/* stream_peer_tcp_winsock.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifdef WINDOWS_ENABLED + +#include "stream_peer_tcp_winsock.h" + +#include <winsock2.h> +#include <ws2tcpip.h> + +#include "drivers/unix/socket_helpers.h" + +int winsock_refcount = 0; + +StreamPeerTCP *StreamPeerTCPWinsock::_create() { + + return memnew(StreamPeerTCPWinsock); +}; + +void StreamPeerTCPWinsock::make_default() { + + StreamPeerTCP::_create = StreamPeerTCPWinsock::_create; + + if (winsock_refcount == 0) { + WSADATA data; + WSAStartup(MAKEWORD(2, 2), &data); + }; + ++winsock_refcount; +}; + +void StreamPeerTCPWinsock::cleanup() { + + --winsock_refcount; + if (winsock_refcount == 0) { + + WSACleanup(); + }; +}; + +Error StreamPeerTCPWinsock::_block(int p_sockfd, bool p_read, bool p_write) const { + + fd_set read, write; + FD_ZERO(&read); + FD_ZERO(&write); + + if (p_read) + FD_SET(p_sockfd, &read); + if (p_write) + FD_SET(p_sockfd, &write); + + int ret = select(p_sockfd + 1, &read, &write, NULL, NULL); // block forever + return ret < 0 ? FAILED : OK; +}; + +Error StreamPeerTCPWinsock::_poll_connection() const { + + ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == INVALID_SOCKET, FAILED); + + struct sockaddr_storage their_addr; + size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, sock_type); + + if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == SOCKET_ERROR) { + + int err = WSAGetLastError(); + if (err == WSAEISCONN) { + status = STATUS_CONNECTED; + return OK; + }; + + if (err == WSAEINPROGRESS || err == WSAEALREADY) { + return OK; + } + + status = STATUS_ERROR; + return ERR_CONNECTION_ERROR; + } else { + + status = STATUS_CONNECTED; + return OK; + }; + + return OK; +}; + +Error StreamPeerTCPWinsock::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block) { + + if (status == STATUS_NONE || status == STATUS_ERROR) { + + return FAILED; + }; + + if (status != STATUS_CONNECTED) { + + if (_poll_connection() != OK) { + + return FAILED; + }; + + if (status != STATUS_CONNECTED) { + r_sent = 0; + return OK; + }; + }; + + int data_to_send = p_bytes; + const uint8_t *offset = p_data; + if (sockfd == -1) return FAILED; + int total_sent = 0; + + while (data_to_send) { + + int sent_amount = send(sockfd, (const char *)offset, data_to_send, 0); + + if (sent_amount == -1) { + + if (WSAGetLastError() != WSAEWOULDBLOCK) { + + perror("Nothing sent"); + disconnect_from_host(); + ERR_PRINT("Server disconnected!\n"); + return FAILED; + }; + + if (!p_block) { + r_sent = total_sent; + return OK; + }; + + _block(sockfd, false, true); + } else { + + data_to_send -= sent_amount; + offset += sent_amount; + total_sent += sent_amount; + }; + } + + r_sent = total_sent; + + return OK; +}; + +Error StreamPeerTCPWinsock::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block) { + + if (!is_connected_to_host()) { + + return FAILED; + }; + + if (status != STATUS_CONNECTED) { + + if (_poll_connection() != OK) { + + return FAILED; + }; + + if (status != STATUS_CONNECTED) { + r_received = 0; + return OK; + }; + }; + + int to_read = p_bytes; + int total_read = 0; + + while (to_read) { + + int read = recv(sockfd, (char *)p_buffer + total_read, to_read, 0); + + if (read == -1) { + + if (WSAGetLastError() != WSAEWOULDBLOCK) { + + perror("Nothing read"); + disconnect_from_host(); + ERR_PRINT("Server disconnected!\n"); + return FAILED; + }; + + if (!p_block) { + + r_received = total_read; + return OK; + }; + _block(sockfd, true, false); + } else if (read == 0) { + disconnect_from_host(); + return ERR_FILE_EOF; + } else { + + to_read -= read; + total_read += read; + }; + }; + + r_received = total_read; + + return OK; +}; + +Error StreamPeerTCPWinsock::put_data(const uint8_t *p_data, int p_bytes) { + + int total; + return write(p_data, p_bytes, total, true); +}; + +Error StreamPeerTCPWinsock::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) { + + return write(p_data, p_bytes, r_sent, false); +}; + +Error StreamPeerTCPWinsock::get_data(uint8_t *p_buffer, int p_bytes) { + + int total; + return read(p_buffer, p_bytes, total, true); +}; + +Error StreamPeerTCPWinsock::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) { + + return read(p_buffer, p_bytes, r_received, false); +}; + +StreamPeerTCP::Status StreamPeerTCPWinsock::get_status() const { + + if (status == STATUS_CONNECTING) { + _poll_connection(); + }; + + return status; +}; + +bool StreamPeerTCPWinsock::is_connected_to_host() const { + + if (status == STATUS_NONE || status == STATUS_ERROR) { + + return false; + }; + if (status != STATUS_CONNECTED) { + return true; + }; + + return (sockfd != INVALID_SOCKET); +}; + +void StreamPeerTCPWinsock::disconnect_from_host() { + + if (sockfd != INVALID_SOCKET) + closesocket(sockfd); + sockfd = INVALID_SOCKET; + sock_type = IP::TYPE_NONE; + + status = STATUS_NONE; + + peer_host = IP_Address(); + peer_port = 0; +}; + +void StreamPeerTCPWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type) { + + sockfd = p_sockfd; + sock_type = p_sock_type; + status = STATUS_CONNECTING; + peer_host = p_host; + peer_port = p_port; +}; + +Error StreamPeerTCPWinsock::connect_to_host(const IP_Address &p_host, uint16_t p_port) { + + ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER); + + sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; + sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP); + if (sockfd == INVALID_SOCKET) { + ERR_PRINT("Socket creation failed!"); + disconnect_from_host(); + //perror("socket"); + return FAILED; + }; + + unsigned long par = 1; + if (ioctlsocket(sockfd, FIONBIO, &par)) { + perror("setting non-block mode"); + disconnect_from_host(); + return FAILED; + }; + + struct sockaddr_storage their_addr; + size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port, sock_type); + + if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == SOCKET_ERROR) { + + if (WSAGetLastError() != WSAEWOULDBLOCK) { + ERR_PRINT("Connection to remote host failed!"); + disconnect_from_host(); + return FAILED; + }; + status = STATUS_CONNECTING; + } else { + status = STATUS_CONNECTED; + }; + + peer_host = p_host; + peer_port = p_port; + + return OK; +}; + +void StreamPeerTCPWinsock::set_nodelay(bool p_enabled) { + ERR_FAIL_COND(!is_connected_to_host()); + int flag = p_enabled ? 1 : 0; + setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)); +} + +int StreamPeerTCPWinsock::get_available_bytes() const { + + unsigned long len; + int ret = ioctlsocket(sockfd, FIONREAD, &len); + ERR_FAIL_COND_V(ret == -1, 0) + return len; +} + +IP_Address StreamPeerTCPWinsock::get_connected_host() const { + + return peer_host; +}; + +uint16_t StreamPeerTCPWinsock::get_connected_port() const { + + return peer_port; +}; + +StreamPeerTCPWinsock::StreamPeerTCPWinsock() { + + sock_type = IP::TYPE_NONE; + sockfd = INVALID_SOCKET; + status = STATUS_NONE; + peer_port = 0; +}; + +StreamPeerTCPWinsock::~StreamPeerTCPWinsock() { + + disconnect_from_host(); +}; + +#endif diff --git a/drivers/windows/stream_peer_tcp_winsock.h b/drivers/windows/stream_peer_tcp_winsock.h new file mode 100644 index 0000000000..fef457c43f --- /dev/null +++ b/drivers/windows/stream_peer_tcp_winsock.h @@ -0,0 +1,91 @@ +/*************************************************************************/ +/* stream_peer_winsock.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifdef WINDOWS_ENABLED + +#ifndef STREAM_PEER_TCP_WINSOCK_H +#define STREAM_PEER_TCP_WINSOCK_H + +#include "error_list.h" + +#include "core/io/ip_address.h" +#include "core/io/stream_peer_tcp.h" + +class StreamPeerTCPWinsock : public StreamPeerTCP { + +protected: + mutable Status status; + IP::Type sock_type; + + int sockfd; + + Error _block(int p_sockfd, bool p_read, bool p_write) const; + + Error _poll_connection() const; + + IP_Address peer_host; + int peer_port; + + Error write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block); + Error read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block); + + static StreamPeerTCP *_create(); + +public: + virtual Error connect_to_host(const IP_Address &p_host, uint16_t p_port); + + virtual Error put_data(const uint8_t *p_data, int p_bytes); + virtual Error put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent); + + virtual Error get_data(uint8_t *p_buffer, int p_bytes); + virtual Error get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received); + + virtual int get_available_bytes() const; + + void set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type); + + virtual IP_Address get_connected_host() const; + virtual uint16_t get_connected_port() const; + + virtual bool is_connected_to_host() const; + virtual Status get_status() const; + virtual void disconnect_from_host(); + + static void make_default(); + static void cleanup(); + + virtual void set_nodelay(bool p_enabled); + + StreamPeerTCPWinsock(); + ~StreamPeerTCPWinsock(); +}; + +#endif // STREAM_PEER_TCP_WINSOCK_H + +#endif diff --git a/drivers/windows/tcp_server_winsock.cpp b/drivers/windows/tcp_server_winsock.cpp new file mode 100644 index 0000000000..49de279793 --- /dev/null +++ b/drivers/windows/tcp_server_winsock.cpp @@ -0,0 +1,187 @@ +/*************************************************************************/ +/* tcp_server_winsock.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifdef WINDOWS_ENABLED + +#include "tcp_server_winsock.h" + +#include "stream_peer_tcp_winsock.h" + +#include <winsock2.h> +#include <ws2tcpip.h> + +#include "drivers/unix/socket_helpers.h" + +extern int winsock_refcount; + +TCP_Server *TCPServerWinsock::_create() { + + return memnew(TCPServerWinsock); +}; + +void TCPServerWinsock::make_default() { + + TCP_Server::_create = TCPServerWinsock::_create; + + if (winsock_refcount == 0) { + WSADATA data; + WSAStartup(MAKEWORD(2, 2), &data); + }; + ++winsock_refcount; +}; + +void TCPServerWinsock::cleanup() { + + --winsock_refcount; + if (winsock_refcount == 0) { + + WSACleanup(); + }; +}; + +Error TCPServerWinsock::listen(uint16_t p_port, const IP_Address &p_bind_address) { + + ERR_FAIL_COND_V(listen_sockfd != -1, ERR_ALREADY_IN_USE); + ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); + + int sockfd; + sock_type = IP::TYPE_ANY; + + // If the bind address is valid use its type as the socket type + if (p_bind_address.is_valid()) + sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; + + sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP); + ERR_FAIL_COND_V(sockfd == INVALID_SOCKET, FAILED); + + unsigned long par = 1; + if (ioctlsocket(sockfd, FIONBIO, &par)) { + perror("setting non-block mode"); + stop(); + return FAILED; + }; + + struct sockaddr_storage my_addr; + size_t addr_size = _set_listen_sockaddr(&my_addr, p_port, sock_type, p_bind_address); + + int reuse = 1; + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) { + + printf("REUSEADDR failed!"); + } + + if (bind(sockfd, (struct sockaddr *)&my_addr, addr_size) != SOCKET_ERROR) { + + if (::listen(sockfd, SOMAXCONN) == SOCKET_ERROR) { + + closesocket(sockfd); + ERR_FAIL_V(FAILED); + }; + } else { + return ERR_ALREADY_IN_USE; + }; + + if (listen_sockfd != INVALID_SOCKET) { + + stop(); + }; + + listen_sockfd = sockfd; + + return OK; +}; + +bool TCPServerWinsock::is_connection_available() const { + + if (listen_sockfd == -1) { + return false; + }; + + timeval timeout; + timeout.tv_sec = 0; + timeout.tv_usec = 0; + + fd_set pfd; + FD_ZERO(&pfd); + FD_SET(listen_sockfd, &pfd); + + int ret = select(listen_sockfd + 1, &pfd, NULL, NULL, &timeout); + ERR_FAIL_COND_V(ret < 0, 0); + + if (ret && (FD_ISSET(listen_sockfd, &pfd))) { + + return true; + }; + + return false; +}; + +Ref<StreamPeerTCP> TCPServerWinsock::take_connection() { + + if (!is_connection_available()) { + return NULL; + }; + + struct sockaddr_storage their_addr; + int sin_size = sizeof(their_addr); + int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &sin_size); + ERR_FAIL_COND_V(fd == INVALID_SOCKET, NULL); + + Ref<StreamPeerTCPWinsock> conn = memnew(StreamPeerTCPWinsock); + IP_Address ip; + int port; + _set_ip_addr_port(ip, port, &their_addr); + + conn->set_socket(fd, ip, port, sock_type); + + return conn; +}; + +void TCPServerWinsock::stop() { + + if (listen_sockfd != INVALID_SOCKET) { + closesocket(listen_sockfd); + }; + + listen_sockfd = -1; + sock_type = IP::TYPE_NONE; +}; + +TCPServerWinsock::TCPServerWinsock() { + + listen_sockfd = INVALID_SOCKET; + sock_type = IP::TYPE_NONE; +}; + +TCPServerWinsock::~TCPServerWinsock() { + + stop(); +}; + +#endif diff --git a/drivers/windows/tcp_server_winsock.h b/drivers/windows/tcp_server_winsock.h new file mode 100644 index 0000000000..fd16480167 --- /dev/null +++ b/drivers/windows/tcp_server_winsock.h @@ -0,0 +1,60 @@ +/*************************************************************************/ +/* tcp_server_winsock.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifdef WINDOWS_ENABLED + +#ifndef TCP_SERVER_WINSOCK_H +#define TCP_SERVER_WINSOCK_H + +#include "core/io/tcp_server.h" + +class TCPServerWinsock : public TCP_Server { + + int listen_sockfd; + IP::Type sock_type; + + static TCP_Server *_create(); + +public: + virtual Error listen(uint16_t p_port, const IP_Address &p_bind_address = IP_Address("*")); + virtual bool is_connection_available() const; + virtual Ref<StreamPeerTCP> take_connection(); + + virtual void stop(); //stop listening + + static void make_default(); + static void cleanup(); + + TCPServerWinsock(); + ~TCPServerWinsock(); +}; + +#endif + +#endif |