TcpConnection::~TcpConnection()
{
    LOG_INFO("TcpConnection::~TcpConnection(),[%0x] [%d][%0x][%0x]", this, socket_->fd(), socket_, channel_);
    ZL_ASSERT(state_ == kDisconnected)(state_);
    Safe_Delete(socket_);
    Safe_Delete(channel_);
}
	GLuint CreateShaderOfType(GLenum type, GLsizei count, const char*const* shader_src)
	{
		GLuint shader;
		GLint compiled;
		shader = glCreateShader(type); //Create the shader object
		if (shader == 0) return 0;

		#if defined(ZILLALOG)
		//Format the source code for debugging purposes
		ZL_String src, indent;
		for (GLsizei i = 0, justnl = 1, bracket_level = 0; i < count; i++, justnl = 1)
			for (const char *p = shader_src[i]; *p; p++)
				if (justnl && *p <= ' ') { } //skip whitespaces after newline
				else if (*p == '{') { src << '\n' << indent << '{' << '\n' << indent << '\t'; indent += '\t'; justnl = 1; }
				else if (*p == '}') { if (!indent.empty()) { indent.resize(indent.size()-1); if (justnl && src.size()) src.resize(src.size()-1); }src << '}' << '\n' << indent; justnl = 1; }
				else if (*p == '\n') { src << '\n' << indent; justnl = 1;  }
				else if (*p == ';' && !bracket_level) { src << ';' << '\n' << indent; justnl = 1; }
				else { if (*p == '(') bracket_level++; else if (*p == ')') bracket_level--; src << *p; justnl = 0; }
		const char* psrc = src.c_str();
		shader_src = &psrc;
		count = 1;
		#endif

		//Load and compile the shader source
		glShaderSource(shader, count, (const GLchar**)shader_src, NULL);
		glCompileShader(shader);

		//Check the compile status
		glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
		if (compiled == 0)
		{
			#if defined(ZILLALOG)
			GLint info_len = 0;
			glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_len);
			if (info_len > 1)
			{
				char* info_log = reinterpret_cast<char*>(malloc(sizeof(char) * info_len));
				glGetShaderInfoLog(shader, info_len, NULL, info_log);
				ZL_LOG1("ZLGLSL", "Error compiling shader: %s", info_log);
				ZL_LOG0("ZLGLSL", "Shader Source:");
				int i = 1; for (const char *lb = src.c_str(), *le; (le = strchr(lb, '\n')); lb = le+1) { ZL_LOG3("ZLGLSL", "%3d: %.*s", i++, le-lb, lb); }
				free(info_log);
				ZL_ASSERT(false);
			}
			#endif
			glDeleteShader(shader);
			return 0;
		}

		#if defined(ZILLALOG) && (0||ZL_GLSL_OUTPUT_ALL_SHADER_SOURCE)
		ZL_LOG0("ZLGLSL", "---------------------------------------------");
		ZL_LOG0("ZLGLSL", "Shader Source:");
		int i = 1; for (const char *lb = src.c_str(), *le; (le = strchr(lb, '\n')); lb = le+1) { ZL_LOG3("ZLGLSL", "%3d: %.*s", i++, le-lb, lb); }
		ZL_LOG0("ZLGLSL", "---------------------------------------------");
		#endif

		return shader;
	}
void TcpConnection::connectEstablished()
{
    loop_->assertInLoopThread();
    ZL_ASSERT(state_ == kConnecting)(state_);
    setState(kConnected);
    channel_->enableReading();

    TcpConnectionPtr sp_this(shared_from_this());
    connectionCallback_(sp_this);
}
Beispiel #4
0
void TcpConnection::handleClose()
{
    loop_->assertInLoopThread();
    ZL_ASSERT(state_ == kConnected || state_ == kDisconnecting)(state_)(socket_->fd());
    setState(kDisconnected);
    channel_->disableAll();

    connectionCallback_(shared_from_this());

    closeCallback_(shared_from_this());
}
Beispiel #5
0
void TcpConnection::connectEstablished()
{
    LOG_INFO("TcpConnection::connectEstablished fd = %d, state = %s", socket_->fd(), getState(state_));
    loop_->assertInLoopThread();
    ZL_ASSERT(state_ == kConnecting)(state_);
    setState(kConnected);
    channel_->enableReading();

    TcpConnectionPtr sp_this(shared_from_this());
    connectionCallback_(sp_this);
}
	GLuint CreateProgramFromVertexAndFragmentShaders(GLsizei vertex_shader_srcs_count, const char*const* vertex_shader_srcs, GLsizei fragment_shader_srcs_count, const char*const* fragment_shader_srcs, GLsizei bind_attribs_count, const char*const* bind_attribs)
	{
		GLuint vertex_shader;
		GLuint fragment_shader;
		GLuint program_object;
		GLint linked;

		// Load the vertex/fragment shaders
		if (!(vertex_shader = CreateShaderOfType(GL_VERTEX_SHADER, vertex_shader_srcs_count, vertex_shader_srcs))) return 0;
		if (!(fragment_shader = CreateShaderOfType(GL_FRAGMENT_SHADER, fragment_shader_srcs_count, fragment_shader_srcs))) { glDeleteShader(vertex_shader); return 0; }

		// Create the program object and attach the shaders.
		if (!(program_object = glCreateProgram())) { glDeleteShader(vertex_shader); glDeleteShader(fragment_shader); return 0; }
		glAttachShader(program_object, vertex_shader);
		glAttachShader(program_object, fragment_shader);

		//attribute bound to index 0 must always be an enabled array attribute - so force position to it
		for (GLsizei i = 0; i < bind_attribs_count; i++) glBindAttribLocation(program_object, i, bind_attribs[i]);

		// Link the program
		glLinkProgram(program_object);

		// Check the link status
		glGetProgramiv(program_object, GL_LINK_STATUS, &linked);
		if (linked == 0)
		{
			#if defined(ZILLALOG)
			GLint info_len = 0;
			glGetProgramiv(program_object, GL_INFO_LOG_LENGTH, &info_len);
			if (info_len > 1)
			{
				char* info_log = reinterpret_cast<char*>(malloc(info_len));
				glGetProgramInfoLog(program_object, info_len, NULL, info_log);
				ZL_LOG1("ZLGLSL", "Error linking program: %s", info_log);
				for (GLsizei vi = 0; vi < vertex_shader_srcs_count; vi++) { ZL_LOG2("ZLGLSL", "    VERTEX SHADER #%d: %s", vi, vertex_shader_srcs[vi]); }
				for (GLsizei fi = 0; fi < fragment_shader_srcs_count; fi++) { ZL_LOG2("ZLGLSL", "    FRAGMENT SHADER #%d: %s", fi, fragment_shader_srcs[fi]); }
				free(info_log);
				ZL_ASSERT(false);
			}
			#endif
			glDeleteProgram(program_object);
			return 0;
		}

		#ifndef ZILLALOG
		// Detach and delete these here because they are linked into the program object (leave them in debug builds for debugging purposes)
		glDetachShader(program_object, vertex_shader); glDeleteShader(vertex_shader);
		glDetachShader(program_object, fragment_shader); glDeleteShader(fragment_shader);
		#endif

		return program_object;
	}
Beispiel #7
0
void TcpConnection::sendInLoop(const void* data, size_t len)
{
    loop_->assertInLoopThread();
    if (state_ == kDisconnected)
    {
        LOG_WARN("TcpConnection::sendInLoop [%d]disconnected, give up writing", socket_->fd());
        return;
    }
    
    size_t nwrote = 0;
    size_t remaining = len;
    bool faultError = false;
    // 如果当前连接尚没有注册可写事件(比如直接调用send接口),并且发送缓冲区为空
    // 就直接发送数据,发送成功则回调写完成事件;
    if (!channel_->isWriting() && outputBuffer_.readableBytes() == 0)
    {
        nwrote = socket_->send((const char*)data, len);
        if (nwrote >= 0)
        {
            remaining = len - nwrote;
            if (remaining == 0 && writeCompleteCallback_)
            {
                loop_->queueInLoop(std::bind(writeCompleteCallback_, shared_from_this()));
            }
        }
        else // nwrote < 0
        {
            nwrote = 0;
            if (errno != EWOULDBLOCK)
            {
                LOG_ERROR("TcpConnection::sendInLoop error, fd[%d], error[%d]", socket_->fd(), errno);
                if (errno == EPIPE || errno == ECONNRESET)
                {
                    faultError = true;
                }
            }
        }
    }
    ZL_ASSERT(remaining <= len)(remaining)(len)(socket_->fd());

    //如果发送成功且数据尚未发送完毕,则将剩余数据存入缓冲区,并注册该channel上的可写事件
    if (!faultError && remaining > 0)
    {
        outputBuffer_.write(static_cast<const char*>(data) + nwrote, remaining);
        if (!channel_->isWriting())
        {
            channel_->enableWriting();
        }
    }
}