diff options
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/gles3/rasterizer_scene_gles3.cpp | 28 | ||||
-rw-r--r-- | drivers/gles3/rasterizer_scene_gles3.h | 28 | ||||
-rw-r--r-- | drivers/gles3/rasterizer_storage_gles3.cpp | 117 | ||||
-rw-r--r-- | drivers/gles3/rasterizer_storage_gles3.h | 32 | ||||
-rw-r--r-- | drivers/gles3/shader_compiler_gles3.cpp | 1 | ||||
-rw-r--r-- | drivers/gles3/shaders/scene.glsl | 88 | ||||
-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 |
14 files changed, 1365 insertions, 37 deletions
diff --git a/drivers/gles3/rasterizer_scene_gles3.cpp b/drivers/gles3/rasterizer_scene_gles3.cpp index d38ec2a1f9..13fbb5c293 100644 --- a/drivers/gles3/rasterizer_scene_gles3.cpp +++ b/drivers/gles3/rasterizer_scene_gles3.cpp @@ -1849,6 +1849,20 @@ void RasterizerSceneGLES3::_setup_light(RenderList::Element *e, const Transform state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE2_ENABLED, false); } + } else if (!e->instance->lightmap_capture_data.empty()) { + + glUniform4fv(state.scene_shader.get_uniform_location(SceneShaderGLES3::LIGHTMAP_CAPTURES), 12, (const GLfloat *)e->instance->lightmap_capture_data.ptr()); + state.scene_shader.set_uniform(SceneShaderGLES3::LIGHTMAP_CAPTURE_SKY, false); + + } else if (e->instance->lightmap.is_valid()) { + RasterizerStorageGLES3::Texture *lightmap = storage->texture_owner.getornull(e->instance->lightmap); + RasterizerStorageGLES3::LightmapCapture *capture = storage->lightmap_capture_data_owner.getornull(e->instance->lightmap_capture->base); + + if (lightmap && capture) { + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 9); + glBindTexture(GL_TEXTURE_2D, lightmap->tex_id); + state.scene_shader.set_uniform(SceneShaderGLES3::LIGHTMAP_ENERGY, capture->energy); + } } } @@ -1971,6 +1985,8 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements, int p_ state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_5, false); state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_13, false); state.scene_shader.set_conditional(SceneShaderGLES3::USE_GI_PROBES, false); + state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP_CAPTURE, false); + state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP, false); state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP, false); state.scene_shader.set_conditional(SceneShaderGLES3::USE_CONTACT_SHADOWS, false); @@ -1978,6 +1994,8 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements, int p_ } else { state.scene_shader.set_conditional(SceneShaderGLES3::USE_GI_PROBES, e->instance->gi_probe_instances.size() > 0); + state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP, e->instance->lightmap.is_valid() && e->instance->gi_probe_instances.size() == 0); + state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP_CAPTURE, !e->instance->lightmap_capture_data.empty() && !e->instance->lightmap.is_valid() && e->instance->gi_probe_instances.size() == 0); state.scene_shader.set_conditional(SceneShaderGLES3::SHADELESS, false); @@ -2148,6 +2166,8 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements, int p_ state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_5, false); state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_13, false); state.scene_shader.set_conditional(SceneShaderGLES3::USE_GI_PROBES, false); + state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP, false); + state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP_CAPTURE, false); state.scene_shader.set_conditional(SceneShaderGLES3::USE_CONTACT_SHADOWS, false); state.scene_shader.set_conditional(SceneShaderGLES3::USE_VERTEX_LIGHTING, false); state.scene_shader.set_conditional(SceneShaderGLES3::USE_OPAQUE_PREPASS, false); @@ -2274,6 +2294,14 @@ void RasterizerSceneGLES3::_add_geometry_with_material(RasterizerStorageGLES3::G e->sort_key |= SORT_KEY_GI_PROBES_FLAG; } + if (e->instance->lightmap.is_valid()) { + e->sort_key |= SORT_KEY_LIGHTMAP_FLAG; + } + + if (!e->instance->lightmap_capture_data.empty()) { + e->sort_key |= SORT_KEY_LIGHTMAP_CAPTURE_FLAG; + } + e->sort_key |= uint64_t(p_material->render_priority + 128) << RenderList::SORT_KEY_PRIORITY_SHIFT; } else { e->sort_key |= uint64_t(e->instance->depth_layer) << RenderList::SORT_KEY_OPAQUE_DEPTH_LAYER_SHIFT; diff --git a/drivers/gles3/rasterizer_scene_gles3.h b/drivers/gles3/rasterizer_scene_gles3.h index ffbe10fb60..6df223c961 100644 --- a/drivers/gles3/rasterizer_scene_gles3.h +++ b/drivers/gles3/rasterizer_scene_gles3.h @@ -662,19 +662,21 @@ public: SORT_KEY_OPAQUE_DEPTH_LAYER_SHIFT = 52, SORT_KEY_OPAQUE_DEPTH_LAYER_MASK = 0xF, //64 bits unsupported in MSVC -#define SORT_KEY_UNSHADED_FLAG (uint64_t(1) << 51) -#define SORT_KEY_NO_DIRECTIONAL_FLAG (uint64_t(1) << 50) -#define SORT_KEY_GI_PROBES_FLAG (uint64_t(1) << 49) -#define SORT_KEY_VERTEX_LIT_FLAG (uint64_t(1) << 48) - SORT_KEY_SHADING_SHIFT = 48, - SORT_KEY_SHADING_MASK = 15, - //48-32 material index - SORT_KEY_MATERIAL_INDEX_SHIFT = 32, - //32-12 geometry index - SORT_KEY_GEOMETRY_INDEX_SHIFT = 12, - //bits 12-8 geometry type - SORT_KEY_GEOMETRY_TYPE_SHIFT = 8, - //bits 0-7 for flags +#define SORT_KEY_UNSHADED_FLAG (uint64_t(1) << 49) +#define SORT_KEY_NO_DIRECTIONAL_FLAG (uint64_t(1) << 48) +#define SORT_KEY_LIGHTMAP_CAPTURE_FLAG (uint64_t(1) << 47) +#define SORT_KEY_LIGHTMAP_FLAG (uint64_t(1) << 46) +#define SORT_KEY_GI_PROBES_FLAG (uint64_t(1) << 45) +#define SORT_KEY_VERTEX_LIT_FLAG (uint64_t(1) << 44) + SORT_KEY_SHADING_SHIFT = 44, + SORT_KEY_SHADING_MASK = 63, + //44-28 material index + SORT_KEY_MATERIAL_INDEX_SHIFT = 28, + //28-8 geometry index + SORT_KEY_GEOMETRY_INDEX_SHIFT = 8, + //bits 5-7 geometry type + SORT_KEY_GEOMETRY_TYPE_SHIFT = 5, + //bits 0-5 for flags SORT_KEY_OPAQUE_PRE_PASS = 8, SORT_KEY_CULL_DISABLED_FLAG = 4, SORT_KEY_SKELETON_FLAG = 2, diff --git a/drivers/gles3/rasterizer_storage_gles3.cpp b/drivers/gles3/rasterizer_storage_gles3.cpp index cba9f08537..ee6c738a05 100644 --- a/drivers/gles3/rasterizer_storage_gles3.cpp +++ b/drivers/gles3/rasterizer_storage_gles3.cpp @@ -5216,6 +5216,104 @@ void RasterizerStorageGLES3::gi_probe_dynamic_data_update(RID p_gi_probe_data, i //glTexImage3D(GL_TEXTURE_3D,p_mipmap,GL_RGBA8,gipd->width>>p_mipmap,gipd->height>>p_mipmap,gipd->depth>>p_mipmap,0,GL_RGBA,GL_UNSIGNED_BYTE,p_data); //glTexImage3D(GL_TEXTURE_3D,p_mipmap,GL_RGBA8,gipd->width>>p_mipmap,gipd->height>>p_mipmap,gipd->depth>>p_mipmap,0,GL_RGBA,GL_UNSIGNED_BYTE,data.ptr()); } +///////////////////////////// + +RID RasterizerStorageGLES3::lightmap_capture_create() { + + LightmapCapture *capture = memnew(LightmapCapture); + return lightmap_capture_data_owner.make_rid(capture); +} + +void RasterizerStorageGLES3::lightmap_capture_set_bounds(RID p_capture, const AABB &p_bounds) { + + LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND(!capture); + capture->bounds = p_bounds; + capture->instance_change_notify(); +} +AABB RasterizerStorageGLES3::lightmap_capture_get_bounds(RID p_capture) const { + + const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND_V(!capture, AABB()); + return capture->bounds; +} +void RasterizerStorageGLES3::lightmap_capture_set_octree(RID p_capture, const PoolVector<uint8_t> &p_octree) { + + LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND(!capture); + + ERR_FAIL_COND(p_octree.size() == 0 || (p_octree.size() % sizeof(LightmapCaptureOctree)) != 0); + + capture->octree.resize(p_octree.size() / sizeof(LightmapCaptureOctree)); + if (p_octree.size()) { + PoolVector<LightmapCaptureOctree>::Write w = capture->octree.write(); + PoolVector<uint8_t>::Read r = p_octree.read(); + copymem(w.ptr(), r.ptr(), p_octree.size()); + } + capture->instance_change_notify(); +} +PoolVector<uint8_t> RasterizerStorageGLES3::lightmap_capture_get_octree(RID p_capture) const { + + const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND_V(!capture, PoolVector<uint8_t>()); + + if (capture->octree.size() == 0) + return PoolVector<uint8_t>(); + + PoolVector<uint8_t> ret; + ret.resize(capture->octree.size() * sizeof(LightmapCaptureOctree)); + { + PoolVector<LightmapCaptureOctree>::Read r = capture->octree.read(); + PoolVector<uint8_t>::Write w = ret.write(); + copymem(w.ptr(), r.ptr(), ret.size()); + } + + return ret; +} + +void RasterizerStorageGLES3::lightmap_capture_set_octree_cell_transform(RID p_capture, const Transform &p_xform) { + LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND(!capture); + capture->cell_xform = p_xform; +} + +Transform RasterizerStorageGLES3::lightmap_capture_get_octree_cell_transform(RID p_capture) const { + const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND_V(!capture, Transform()); + return capture->cell_xform; +} + +void RasterizerStorageGLES3::lightmap_capture_set_octree_cell_subdiv(RID p_capture, int p_subdiv) { + LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND(!capture); + capture->cell_subdiv = p_subdiv; +} + +int RasterizerStorageGLES3::lightmap_capture_get_octree_cell_subdiv(RID p_capture) const { + const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND_V(!capture, 0); + return capture->cell_subdiv; +} + +void RasterizerStorageGLES3::lightmap_capture_set_energy(RID p_capture, float p_energy) { + + LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND(!capture); + capture->energy = p_energy; +} + +float RasterizerStorageGLES3::lightmap_capture_get_energy(RID p_capture) const { + + const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND_V(!capture, 0); + return capture->energy; +} + +const PoolVector<RasterizerStorage::LightmapCaptureOctree> *RasterizerStorageGLES3::lightmap_capture_get_octree_ptr(RID p_capture) const { + const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND_V(!capture, NULL); + return &capture->octree; +} /////// @@ -5817,6 +5915,10 @@ void RasterizerStorageGLES3::instance_add_dependency(RID p_base, RasterizerScene inst = gi_probe_owner.getornull(p_base); ERR_FAIL_COND(!inst); } break; + case VS::INSTANCE_LIGHTMAP_CAPTURE: { + inst = lightmap_capture_data_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; default: { if (!inst) { ERR_FAIL(); @@ -5860,6 +5962,10 @@ void RasterizerStorageGLES3::instance_remove_dependency(RID p_base, RasterizerSc inst = gi_probe_owner.getornull(p_base); ERR_FAIL_COND(!inst); } break; + case VS::INSTANCE_LIGHTMAP_CAPTURE: { + inst = lightmap_capture_data_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; default: { if (!inst) { @@ -6609,6 +6715,10 @@ VS::InstanceType RasterizerStorageGLES3::get_base_type(RID p_rid) const { return VS::INSTANCE_GI_PROBE; } + if (lightmap_capture_data_owner.owns(p_rid)) { + return VS::INSTANCE_LIGHTMAP_CAPTURE; + } + return VS::INSTANCE_NONE; } @@ -6795,6 +6905,13 @@ bool RasterizerStorageGLES3::free(RID p_rid) { glDeleteTextures(1, &gi_probe_data->tex_id); gi_probe_owner.free(p_rid); memdelete(gi_probe_data); + } else if (lightmap_capture_data_owner.owns(p_rid)) { + + // delete the texture + LightmapCapture *lightmap_capture = lightmap_capture_data_owner.get(p_rid); + + gi_probe_owner.free(p_rid); + memdelete(lightmap_capture); } else if (canvas_occluder_owner.owns(p_rid)) { diff --git a/drivers/gles3/rasterizer_storage_gles3.h b/drivers/gles3/rasterizer_storage_gles3.h index 25327af0a5..6647372688 100644 --- a/drivers/gles3/rasterizer_storage_gles3.h +++ b/drivers/gles3/rasterizer_storage_gles3.h @@ -1069,6 +1069,38 @@ public: virtual RID gi_probe_dynamic_data_create(int p_width, int p_height, int p_depth, GIProbeCompression p_compression); virtual void gi_probe_dynamic_data_update(RID p_gi_probe_data, int p_depth_slice, int p_slice_count, int p_mipmap, const void *p_data); + /* LIGHTMAP CAPTURE */ + + virtual RID lightmap_capture_create(); + virtual void lightmap_capture_set_bounds(RID p_capture, const AABB &p_bounds); + virtual AABB lightmap_capture_get_bounds(RID p_capture) const; + virtual void lightmap_capture_set_octree(RID p_capture, const PoolVector<uint8_t> &p_octree); + virtual PoolVector<uint8_t> lightmap_capture_get_octree(RID p_capture) const; + virtual void lightmap_capture_set_octree_cell_transform(RID p_capture, const Transform &p_xform); + virtual Transform lightmap_capture_get_octree_cell_transform(RID p_capture) const; + virtual void lightmap_capture_set_octree_cell_subdiv(RID p_capture, int p_subdiv); + virtual int lightmap_capture_get_octree_cell_subdiv(RID p_capture) const; + + virtual void lightmap_capture_set_energy(RID p_capture, float p_energy); + virtual float lightmap_capture_get_energy(RID p_capture) const; + + virtual const PoolVector<LightmapCaptureOctree> *lightmap_capture_get_octree_ptr(RID p_capture) const; + + struct LightmapCapture : public Instantiable { + + PoolVector<LightmapCaptureOctree> octree; + AABB bounds; + Transform cell_xform; + int cell_subdiv; + float energy; + LightmapCapture() { + energy = 1.0; + cell_subdiv = 1; + } + }; + + mutable RID_Owner<LightmapCapture> lightmap_capture_data_owner; + /* PARTICLES */ struct Particles : public GeometryOwner { 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/shaders/scene.glsl b/drivers/gles3/shaders/scene.glsl index 9b817c7a4e..9bc2bc079d 100644 --- a/drivers/gles3/shaders/scene.glsl +++ b/drivers/gles3/shaders/scene.glsl @@ -35,14 +35,14 @@ layout(location=3) in vec4 color_attrib; layout(location=4) in vec2 uv_attrib; #endif -#if defined(ENABLE_UV2_INTERP) +#if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP) layout(location=5) in vec2 uv2_attrib; #endif uniform float normal_mult; #ifdef USE_SKELETON -layout(location=6) in ivec4 bone_indices; // attrib:6 +layout(location=6) in uvec4 bone_indices; // attrib:6 layout(location=7) in vec4 bone_weights; // attrib:7 #endif @@ -223,7 +223,7 @@ out vec4 color_interp; out vec2 uv_interp; #endif -#if defined(ENABLE_UV2_INTERP) +#if defined(ENABLE_UV2_INTERP) || defined (USE_LIGHTMAP) out vec2 uv2_interp; #endif @@ -234,9 +234,6 @@ out vec3 binormal_interp; #endif - - - #if defined(USE_MATERIAL) layout(std140) uniform UniformData { //ubo:1 @@ -302,14 +299,16 @@ void main() { #ifdef USE_SKELETON { //skeleton transform - ivec2 tex_ofs = ivec2( bone_indices.x%256, (bone_indices.x/256)*3 ); + ivec4 bone_indicesi = ivec4(bone_indices); // cast to signed int + + ivec2 tex_ofs = ivec2( bone_indicesi.x%256, (bone_indicesi.x/256)*3 ); highp mat3x4 m = mat3x4( texelFetch(skeleton_texture,tex_ofs,0), texelFetch(skeleton_texture,tex_ofs+ivec2(0,1),0), texelFetch(skeleton_texture,tex_ofs+ivec2(0,2),0) ) * bone_weights.x; - tex_ofs = ivec2( bone_indices.y%256, (bone_indices.y/256)*3 ); + tex_ofs = ivec2( bone_indicesi.y%256, (bone_indicesi.y/256)*3 ); m+= mat3x4( texelFetch(skeleton_texture,tex_ofs,0), @@ -317,7 +316,7 @@ void main() { texelFetch(skeleton_texture,tex_ofs+ivec2(0,2),0) ) * bone_weights.y; - tex_ofs = ivec2( bone_indices.z%256, (bone_indices.z/256)*3 ); + tex_ofs = ivec2( bone_indicesi.z%256, (bone_indicesi.z/256)*3 ); m+= mat3x4( texelFetch(skeleton_texture,tex_ofs,0), @@ -326,7 +325,7 @@ void main() { ) * bone_weights.z; - tex_ofs = ivec2( bone_indices.w%256, (bone_indices.w/256)*3 ); + tex_ofs = ivec2( bone_indicesi.w%256, (bone_indicesi.w/256)*3 ); m+= mat3x4( texelFetch(skeleton_texture,tex_ofs,0), @@ -354,7 +353,7 @@ void main() { uv_interp = uv_attrib; #endif -#if defined(ENABLE_UV2_INTERP) +#if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP) uv2_interp = uv2_attrib; #endif @@ -547,7 +546,7 @@ in vec4 color_interp; in vec2 uv_interp; #endif -#if defined(ENABLE_UV2_INTERP) +#if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP) in vec2 uv2_interp; #endif @@ -1355,7 +1354,7 @@ void reflection_process(int idx, vec3 vertex, vec3 normal,vec3 binormal, vec3 ta reflection_accum+=reflection; } - +#ifndef USE_LIGHTMAP if (reflections[idx].ambient.a>0.0) { //compute ambient using skybox @@ -1401,8 +1400,20 @@ void reflection_process(int idx, vec3 vertex, vec3 normal,vec3 binormal, vec3 ta ambient_accum+=ambient_out; } +#endif } +#ifdef USE_LIGHTMAP +uniform mediump sampler2D lightmap; //texunit:-9 +uniform mediump float lightmap_energy; +#endif + +#ifdef USE_LIGHTMAP_CAPTURE +uniform mediump vec4[12] lightmap_captures; +uniform bool lightmap_capture_sky; + +#endif + #ifdef USE_GI_PROBES uniform mediump sampler3D gi_probe1; //texunit:-9 @@ -1630,7 +1641,7 @@ void main() { vec2 uv = uv_interp; #endif -#if defined(ENABLE_UV2_INTERP) +#if defined(ENABLE_UV2_INTERP) || defined (USE_LIGHTMAP) vec2 uv2 = uv2_interp; #endif @@ -1743,7 +1754,7 @@ FRAGMENT_SHADER_CODE //vec3 radiance = textureLod(radiance_cube, r, lod).xyz * ( brdf.x + brdf.y); } - +#ifndef USE_LIGHTMAP { vec3 ambient_dir=normalize((radiance_inverse_xform * vec4(normal,0.0)).xyz); @@ -1752,6 +1763,7 @@ FRAGMENT_SHADER_CODE ambient_light=mix(ambient_light_color.rgb,env_ambient,radiance_ambient_contribution); //ambient_light=vec3(0.0,0.0,0.0); } +#endif } #else @@ -1936,6 +1948,48 @@ FRAGMENT_SHADER_CODE #endif +#ifdef USE_LIGHTMAP + ambient_light = texture(lightmap,uv2).rgb * lightmap_energy; +#endif + +#ifdef USE_LIGHTMAP_CAPTURE + { + vec3 cone_dirs[12] = vec3[] ( + vec3(0, 0, 1), + vec3(0.866025, 0, 0.5), + vec3(0.267617, 0.823639, 0.5), + vec3(-0.700629, 0.509037, 0.5), + vec3(-0.700629, -0.509037, 0.5), + vec3(0.267617, -0.823639, 0.5), + vec3(0, 0, -1), + vec3(0.866025, 0, -0.5), + vec3(0.267617, 0.823639, -0.5), + vec3(-0.700629, 0.509037, -0.5), + vec3(-0.700629, -0.509037, -0.5), + vec3(0.267617, -0.823639, -0.5) + ); + + + vec3 local_normal = normalize(camera_matrix * vec4(normal,0.0)).xyz; + vec4 captured = vec4(0.0); + float sum = 0.0; + for(int i=0;i<12;i++) { + float amount = max(0.0,dot(local_normal,cone_dirs[i])); //not correct, but creates a nice wrap around effect + captured += lightmap_captures[i]*amount; + sum+=amount; + } + + captured/=sum; + + if (lightmap_capture_sky) { + ambient_light = mix( ambient_light, captured.rgb, captured.a); + } else { + ambient_light = captured.rgb; + } + + } +#endif + #ifdef USE_FORWARD_LIGHTING @@ -1950,11 +2004,11 @@ FRAGMENT_SHADER_CODE } else { specular_light+=env_reflection_light; } - +#ifndef USE_LIGHTMAP if (ambient_accum.a>0.0) { ambient_light+=ambient_accum.rgb/ambient_accum.a; } - +#endif #ifdef USE_VERTEX_LIGHTING 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 |