void source_accept_cb(uv_stream_t *server, int status) { struct source_context *source = new_source(); struct target_context *target = new_target(); source->target = target; target->source = source; uv_tcp_init(server->loop, &source->handle.tcp); uv_tcp_init(server->loop, &target->handle.tcp); uv_tcp_nodelay(&source->handle.tcp, 0); uv_tcp_nodelay(&target->handle.tcp, 0); uv_tcp_keepalive(&source->handle.tcp, 1, 60); uv_tcp_keepalive(&target->handle.tcp, 1, 60); int rc = uv_accept(server, &source->handle.stream); if (rc == 0) { connect_to_target(target); } else { logger_log(LOG_ERR, "accept error: %s", uv_strerror(rc)); close_source(source); close_target(target); } }
static void source_recv_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { struct source_context *source = stream->data; struct target_context *target = source->target; if (nread > 0) { if (mode == TUNNEL_MODE_CLIENT) { int clen = nread + PRIMITIVE_BYTES; uint8_t *c = source->packet.buf + HEADER_BYTES; int rc = crypto_encrypt(c, (uint8_t*)buf->base, nread); if (rc) { goto error; } forward_to_target(target, c, clen); } else { struct packet *packet = &source->packet; int rc = packet_filter(packet, buf->base, nread); if (rc == PACKET_COMPLETED) { uint8_t *m = packet->buf; int mlen = packet->size - PRIMITIVE_BYTES; int err = crypto_decrypt(m, packet->buf, packet->size); if (err) { goto error; } forward_to_target(target, m, mlen); } else if (rc == PACKET_INVALID) { goto error; } } } else if (nread < 0){ close_source(source); close_target(target); } return; error: logger_log(LOG_ERR, "invalid packet"); close_source(source); close_target(target); }
// render target static RID create_rt(struct render *R, RID texid) { struct target *tar = (struct target *)array_alloc(&R->target); if (tar == NULL) return 0; tar->tex = texid; struct texture * tex = (struct texture *)array_ref(&R->texture, texid); if (tex == NULL) return 0; glGenFramebuffers(1, &tar->glid); glBindFramebuffer(GL_FRAMEBUFFER, tar->glid); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex->glid, 0); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { close_target(tar, R); return 0; } CHECK_GL_ERROR return array_id(&R->target, tar); }
void render_release(struct render *R, enum RENDER_OBJ what, RID id) { switch (what) { case VERTEXBUFFER: case INDEXBUFFER: { struct buffer * buf = (struct buffer *)array_ref(&R->buffer, id); if (buf) { close_buffer(buf, R); array_free(&R->buffer, buf); } break; } case SHADER: { struct shader * shader = (struct shader *)array_ref(&R->shader, id); if (shader) { close_shader(shader, R); array_free(&R->shader, shader); } break; } case TEXTURE : { struct texture * tex = (struct texture *) array_ref(&R->texture, id); if (tex) { close_texture(tex, R); array_free(&R->texture, tex); } break; } case TARGET : { struct target * tar = (struct target *)array_ref(&R->target, id); if (tar) { close_target(tar, R); array_free(&R->target, tar); } break; } default: assert(0); break; } }