Пример #1
0
int32_t ShortConnection::handle_exception(BASE_HANDLER handle)
{
	CORE_DEBUG("handle_exception");
	if(state_ != SHORT_CONN_IDLE)
	{
		//通知服务器节点管理模块

		//通知上层连接断开
		if(msg_proc_ != NULL)
			msg_proc_->on_short_disconnected(this);

		sock_stream_.close();

		reset();
		//将连接对象返回给连接池
		SHORTCONN_POOL.push_obj(this);
		CORE_DEBUG("push short conn = " << this);
	}
	else
	{
		sock_stream_.close();
	}

	return 0;
}
Пример #2
0
int32_t CConnection::handle_close(BASE_HANDLER handle, ReactorMask close_mask)
{
	CORE_DEBUG("handle_close");
	if(state_ != CONN_IDLE)
	{
		//通知服务器节点管理模块
		CONN_MANAGER()->on_del_connection(this);

		//通知上层连接断开
		MSG_PROCESSOR()->on_disconnect(server_id_, this);
		
		sock_stream_.close();

		reset();
		//将连接对象返回给连接池
		CONNECTION_POOL.push_obj(this);
		CORE_DEBUG("push conn = " << this);
	}
	else
	{
		sock_stream_.close();
	}

	return 0;
}
Пример #3
0
int32_t ShortConnection::connect(const Inet_Addr& src_addr, const Inet_Addr& dst_addr)
{
	CORE_DEBUG("src_addr = " << src_addr << ", TCP connect " << dst_addr );

	//打开一个SOCKET
	if(sock_stream_.open(src_addr, true, false, true) != 0)
	{
		CORE_FATAL("open tcp failed! conn = " << this);
		return -1;
	}

	CORE_DEBUG("sort conection connector ....");

	CSockConnector connector;
	if(connector.connect(sock_stream_, dst_addr) != 0)
	{
		sock_stream_.close();
		CORE_FATAL("connect failed, remote addr = " << dst_addr << ", conn = " << this);
		return -1;
	}

	state_ = SHORT_CONN_CONNECTING;
	CORE_DEBUG("short conn, state = SHORT_CONN_CONNECTING, conn = " << this);

	REACTOR_INSTANCE()->register_handler(this, MASK_WRITE);

	//设置一个连接定时器
	CORE_DEBUG("short conn set connect timer");
	timer_id_ = set_timer(eShort_Connect_Timer, 23 * 1000);

	return 0;
}
Пример #4
0
int32_t CConnection::connect(const Inet_Addr& src_addr, const Inet_Addr& dst_addr)
{
	CORE_DEBUG("TCP connect " << dst_addr);

	//打开一个SOCKET
	if(sock_stream_.open(src_addr, true, false, true) != 0)
	{
		CORE_FATAL("open tcp failed! conn = " << this);
		return -1;
	}
	
	//主动方
	conn_name_ = "connect connection";

	CSockConnector connector;
	if(connector.connect(sock_stream_, dst_addr) != 0)
	{
		sock_stream_.close();
		CORE_FATAL("connect failed, remote addr = " << dst_addr << ", conn = " << this);
		return -1;
	}

	state_ = CONN_CONNECTING;
	CORE_DEBUG("CConnection, state = CONN_CONNECTING, conn = " << this);

	//CORE_DEBUG("CConnection, register MASK_WRITE");
	REACTOR_INSTANCE()->register_handler(this, MASK_WRITE);

	//设置一个连接定时器
	CORE_DEBUG("set connect timer");
	timer_id_ = set_timer(eConnect_Timer, 10 * 1000);

	return 0;
}
void BaseGameManager::init()
{
    CORE_DEBUG("Providing texture locator...");
    TextureLocator::provide(TextureLocator::Ptr(new TextureHolder()));

    CORE_DEBUG("Providing world locator...");
    WorldLocator::provide(m_pWorld);
}
void BaseGameManager::initPhysics(const sf::Vector2f& gravity, const sf::Vector2f& ppp)
{
    CORE_DEBUG("Providing physics locator...");
    PhysicsLocator::provide(gravity, ppp);

    m_pPhysicsWorld = PhysicsLocator::getObject();

    CORE_DEBUG("Creating physics system...");
    m_pPhysicsSystem = static_cast<PhysicsSystem*>(
            getSystemManager()->setSystem(new PhysicsSystem())
    );
}
void BaseGameManager::destroy()
{
    CORE_ASSERT(m_pWorld != nullptr);

    CORE_DEBUG("Removing all entities...");
    m_pWorld->getEntityManager()->removeAllEntities();

    CORE_DEBUG("Destroying entity world...");
    WorldLocator::remove();
    m_pWorld = nullptr;

    CORE_DEBUG("Destroying textures...");
    TextureLocator::remove();
}
Пример #8
0
void GameManager::destroy()
{
    CORE_DEBUG("Destroying event listener...");
    m_pJumpListener = nullptr;

    // Remove input locator
    CORE_DEBUG("Destroying input locator...");
    InputLocator::remove();

    CORE_DEBUG("Destroying input system...");
    m_pPlayerInputSystem = nullptr;

    // Super
    BaseGameManager::destroy();
}
bool BaseGameManager::initLua()
{
    CORE_DEBUG("Initializing lua state...");
    if(!LuaStateManager::create())
    {
        return false;
    }

    CORE_DEBUG("Creating script system...");
    m_pScriptSystem = static_cast<ScriptSystem*>(
        getSystemManager()->setSystem(new ScriptSystem())
    );

    return true;
}
Пример #10
0
void CConnection::reset()
{
	//清空到初始化状态
	sock_stream_.close();

	reactor_index_ = 0;

	state_ = CONN_IDLE;
	CORE_DEBUG("CConnection, state = CONN_IDLE, this = " << this);

	//取消定时器
	cancel_timer();

	timer_id_ = 0;

	sbuffer_.reset();
	rbuffer_.reset();

	index_ = 0;
	server_id_ = 0;
	server_type_ = 0;

	timer_count_ = 0;

	conn_ptr_ = NULL;

	send_flag_ = false;

	//释放太多的内存占用
	istrm_.rewind(true);
	istrm_.reduce();

	remote_addr_.set_ip(INADDR_ANY);
	remote_addr_.set_port(0);
}
Пример #11
0
void ShortConnection::reset()
{
	//清空到初始化状态
	sock_stream_.close();

	reactor_index_ = 0;

	state_ = SHORT_CONN_IDLE;
	CORE_DEBUG("short conn, state = SHORT_CONN_IDLE, this = " << this);

	//取消定时器
	cancel_timer();

	timer_id_ = 0;

	sbuffer_.reset();
	rbuffer_.reset();

	timer_count_ = 0;

	//释放太多的内存占用
	istrm_.rewind(true);
	istrm_.reduce();

	remote_addr_.set_ip(INADDR_ANY);
	remote_addr_.set_port(0);

	msg_proc_ = NULL;
	conn_id_ = 0;
}
Пример #12
0
void BaseGameManager::destroyPhysics()
{
    CORE_DEBUG("Destroying physics world...");
    PhysicsLocator::remove();
    m_pPhysicsWorld = nullptr;

    m_pPhysicsSystem = nullptr;
}
Пример #13
0
void ShortConnection::close()
{
	//删除监听事件
	REACTOR_INSTANCE()->delete_handler(this);
	//通知连接断开
	CORE_DEBUG("short conn close, conn = " << this);
	handle_close(get_handle(), MASK_TIMEOUT);
}
Пример #14
0
BASE_NAMESPACE_BEGIN_DECL

CoreUDPHandler::CoreUDPHandler()
{
    bin_strm_.resize(MAX_UDP_PACKET);

    CORE_DEBUG("Create core UDP");
}
Пример #15
0
void BaseGameManager::destroyLua()
{
    CORE_DEBUG("Destroying lua state...");
    LuaStateManager::destroy();

    // TODO: CHeck if this causes memory leaks.
    m_pScriptSystem = nullptr;
}
Пример #16
0
void GameManager::init()
{
    // Call super
    BaseGameManager::init();

    // Set up input system
    CORE_DEBUG("Creating input system...");
    m_pPlayerInputSystem =  static_cast<PlayerInputSystem*>(
                                getSystemManager()->setSystem(new PlayerInputSystem())
                            );

    CORE_DEBUG("Registering input locator...");
    InputLocator::provide(loose_ptr(m_pPlayerInputSystem));

    CORE_DEBUG("Creating jump listener...");
    m_pJumpListener = UJumpListenerPtr(new JumpListener());
}
Пример #17
0
void BaseGameManager::start()
{
    CORE_DEBUG("Initializing all systems...");
    getSystemManager()->initializeAll();

    if(m_pPhysicsSystem)
    {
        Camera2D& camera = m_pCameraFollowingSystem->getCamera();

        CORE_DEBUG("Resizing camera to work with physics system...");
        camera.resize(sf::Vector2u(
                    camera.getSize().x / PhysicsLocator::PixelsPerMeter.x,
                    camera.getSize().y / PhysicsLocator::PixelsPerMeter.y
        ));
    }

    CORE_DEBUG("Initialization complete");
}
Пример #18
0
void BaseGameManager::destroyPhysicsRenderer()
{
    if(m_pPhysicsWorld)
    {
        CORE_DEBUG("Destroying physics renderer...");
        // Remove debug draw
        m_pPhysicsWorld->SetDebugDraw(NULL);
        m_pBox2DRenderer = nullptr;
    }
}
Пример #19
0
void CConnection::close()
{
	//删除监听事件
	REACTOR_INSTANCE()->delete_handler(this);
	//关闭SOCKET
	//sock_stream_.close();
	//通知连接断开
	CORE_DEBUG("connection close, conn = " << this);
	handle_close(get_handle(), MASK_TIMEOUT);
}
Пример #20
0
void ShortConnection::extern_close()
{
	//删除监听事件
	REACTOR_INSTANCE()->delete_handler(this);

	reset();
	//将连接对象返回给连接池
	SHORTCONN_POOL.push_obj(this);
	CORE_DEBUG("push short conn = " << this);
}
Пример #21
0
CoreUDPHandler::~CoreUDPHandler()
{
    recivers_.clear();
    if(is_open())
    {
        this->close();
    }

    CORE_DEBUG("Destory UDP");
}
Пример #22
0
void BaseGameManager::initRenderer(SpriteBatch& spriteBatch, Camera2D& camera)
{
    CORE_ASSERT(m_pWorld != nullptr);

    artemis::SystemManager* systemManager = getSystemManager();

    CORE_DEBUG("Creating render system...");
    m_pRenderSystem = static_cast<RenderSystem*>(
        systemManager->setSystem(new RenderSystem(spriteBatch))
    );

	CORE_DEBUG("Creating animation system...");
	m_pAnimationSystem = static_cast<AnimationSystem*>(
		systemManager->setSystem(new AnimationSystem(spriteBatch))	
	);

    CORE_DEBUG("Creating camera following system...");
    m_pCameraFollowingSystem = static_cast<CameraFollowingSystem*>(
            systemManager->setSystem(new CameraFollowingSystem(camera))
    );
}
void Connection_Manager::connecting_by_id(const string& data, uint32_t sid)
{
	Server_Node_Map::iterator it = nodes_.find(sid);
	if(it != nodes_.end())
	{
		CConnection* conn = NULL;
		if(it->second.conn == NULL) //建立一条新的TCP服务器连接 
		{
			conn = CONNECTION_POOL.pop_obj();

			conn->set_index(0);
			conn->set_server_type(it->second.server_type);
			conn->set_server_id(it->second.server_id);			
			
			//选取连接地址,目标地址为电信用电信网卡进行连接,依次类推
			Inet_Addr src_addr, dst_addr;
			get_address_pair(it->second, src_addr, dst_addr);

			src_addr.set_port(0);
			//随机一个端口作为客户机端口
			if(src_addr.get_ip() != INADDR_ANY)
			{
				src_addr.set_port(15000 + rand() % 15000);
			}

			CORE_INFO("connecting " << dst_addr << ", by local addr = " << src_addr << ", dst sid = " << sid);

			if(conn->connect(src_addr, dst_addr) != 0)
			{
				conn->close();
				CONNECTION_POOL.push_obj(conn);
				CORE_DEBUG("push conn = " << this);
				CORE_ERROR("connect " << dst_addr << "failed!!");
				return ;
			}

			it->second.conn = conn;
		}
		else
		{
			conn = it->second.conn;
		}

		if(conn->get_state() != CConnection::CONN_CONNECTED) //处于连接状态,缓冲到LIST中
			it->second.strms.push_back(data);
		else
			conn->send(data);
	}
	else
	{
		CORE_ERROR("sid = " << sid << " error!!");
	}
}
Пример #24
0
int32_t CCoreTCPListener::handle_input(BASE_HANDLER handle)
{
	while(true) //EPOLL ET模式是需要将事件全部处理干净,直到状态EAGAIN
	{
		Inet_Addr remote_addr;
		CConnection *conn = CONNECTION_POOL.pop_obj();
		conn->reset();

		if(acceptor_.accept(conn->get_sock_stream(), remote_addr, true) == 0)
		{
			CORE_INFO("accept tcp connection, remote addr = " << remote_addr);
			conn->set_remote_addr(remote_addr);

			if(REACTOR_INSTANCE()->register_handler(conn, MASK_WRITE | MASK_READ) == 0)
			{	
				conn->set_state(CConnection::CONN_CONNECTING);

				CORE_INFO("pop conn = " << conn);
			}
			else 
			{
				conn->reset();
				CONNECTION_POOL.push_obj(conn);
			}
#ifdef WIN32
			return 0;
#endif
		}
		else
		{
			conn->reset();
			CONNECTION_POOL.push_obj(conn);
#ifndef WIN32
			if (errno != EAGAIN && errno != ECONNABORTED && errno != EPROTO && errno != EINTR)
			{
				CORE_DEBUG("push conn = " << this);
				CORE_FATAL("tcp listener accept failed!");
				return -2;
			}
			else 
			{
				return 0;
			}
#else 
			return -2;
#endif
		}
	}

	return 0;
}
Пример #25
0
void BaseGameManager::initPhysicsRenderer(sf::RenderTargetPtr pRenderTarget)
{
    CORE_ASSERT(m_pWorld != nullptr);

    if(!m_pPhysicsWorld)
    {
        CORE_ERROR("Attempting to initialize physics renderer without initializing physics");
        return;
    }

    m_pBox2DRenderer = std::unique_ptr<Box2DRenderer>(new Box2DRenderer(pRenderTarget));

    CORE_DEBUG("Initializing physics renderer...");
    m_pPhysicsWorld->SetDebugDraw(m_pBox2DRenderer.get());
    m_pBox2DRenderer->SetFlags(Box2DRenderer::e_shapeBit);
}
Пример #26
0
int32_t DCTCPListener::handle_input(BASE_HANDLER handle)
{
	while(true)
	{
		Inet_Addr remote_addr;
		ShortConnection *conn = SHORTCONN_POOL.pop_obj();
		conn->reset();

		if(acceptor_.accept(conn->get_sock_stream(), remote_addr, true) == 0)
		{
			CORE_INFO("accept tcp short connection, remote addr = " << remote_addr);
			conn->set_remote_addr(remote_addr);
			conn->set_proc(proc_); //管理消息处理句柄

			REACTOR_INSTANCE()->register_handler(conn, MASK_WRITE);	
			REACTOR_INSTANCE()->register_handler(conn, MASK_READ);

			conn->set_state(ShortConnection::SHORT_CONN_CONNECTING);

			CORE_INFO("pop short conn = " << conn);

#ifdef WIN32
			return 0;
#endif
		}
		else
		{
			SHORTCONN_POOL.push_obj(conn);
#ifndef WIN32
			if (errno != EAGAIN && errno != ECONNABORTED && errno != EPROTO && errno != EINTR)
			{
				CORE_DEBUG("push short conn = " << this);
				CORE_FATAL("short tcp listener accept failed!");
				return -2;
			}
			else 
			{
				return 0;
			}
#else 
			return -2;
#endif
		}
	}

	return 0;
}
Пример #27
0
int32_t CConnection::handle_timeout(const void *act, uint32_t timer_id)
{
	if(timer_id_ !=  timer_id)
		return -1;

	timer_id_ = 0;

	STCPTimerParam* param = (STCPTimerParam *)act;
	switch(param->timer_type_)
	{
	case eConnect_Timer:
		{
			delete param;

			if(state_ == CONN_CONNECTING)
			{				
				CONN_MANAGER()->on_del_connection(this);

				//通知上层连接断开
				MSG_PROCESSOR()->on_disconnect(server_id_, this);

				state_ = CONN_IDLE;
				CORE_DEBUG("CConnection, state = CONN_IDLE");
				this->close();
			}
		}
		break;

	case eTCP_Hearbeat: //心跳,PING保持
		{
			if(heartbeat() == 0)
			{
				timer_id_ = REACTOR_INSTANCE()->set_timer(this, param, 60000);
			}
			else
			{
				delete param;
			}
		}
		break;
	}

	return 0;
}
Пример #28
0
int32_t CoreUDPHandler::open(const Inet_Addr& local_addr)
{
    CORE_INFO("open core udp socket, bind local addr = " << local_addr);

    int32_t ret = sock_dgram_.open(local_addr, true);
    if(ret == 0)
    {
        //设置缓冲区大小
        int32_t buf_size = 1024 * 1024; //1M
        sock_dgram_.set_option(SOL_SOCKET, SO_RCVBUF, (void *)&buf_size, sizeof(int32_t));
        sock_dgram_.set_option(SOL_SOCKET, SO_SNDBUF, (void *)&buf_size, sizeof(int32_t));

        CORE_DEBUG("CoreUDPHandler, register MASK_READ");
        return REACTOR_INSTANCE()->register_handler(this, MASK_READ);
    }
    else
    {
        CORE_FATAL("open UDP failed");
        return -1;
    }
}
Пример #29
0
int createEntity(const char* entityResource, LuaPlus::LuaObject luaPosition)
{
    if(!luaPosition.IsTable())
    {
        CORE_LOG("LUA", "Invalid position object passed to create_entity function. Must be a table");
        return INVALID_ENTITY_ID;
    }

	sf::Vector2f position = tableToVec2<sf::Vector2f>(luaPosition);

    artemis::Entity& entity = WorldLocator::getObject()->createEntity();

    // Create entity
    if(!EntityFactory::get().loadFromFile(entityResource, entity))
    {
        CORE_ERROR("Failed to load entity from file: " + std::string(entityResource));
        return INVALID_ENTITY_ID;
    }

    Transform* transformComp = safeGetComponent<Transform>(&entity);

    if(!transformComp)
    {
        // Create new transform component if it doesn't exist
        entity.addComponent(new Transform(position.x, position.y));
    }
	else
	{
		CORE_DEBUG("Overriding transform with lua object");

		transformComp->position = position;
	}

    // Commit entity changes
    entity.refresh();

    CORE_LOG("LUA", "Created entity from: " + std::string(entityResource));

    return entity.getId();
}
Пример #30
0
int32_t ShortConnection::handle_timeout(const void *act, uint32_t timer_id)
{
	if(timer_id_ !=  timer_id)
		return -1;

	timer_id_ = 0;

	SShortTCPTimerParam* param = (SShortTCPTimerParam *)act;
	switch(param->timer_type_)
	{
	case eShort_Connect_Timer:
		{
			delete param;

			if(state_ == SHORT_CONN_CONNECTING)
			{
				state_ = SHORT_CONN_IDLE;
				CORE_DEBUG("short conn, state = CONN_IDLE");
				this->close();
			}
		}
		break;

	case eShortTCP_Hearbeat: //心跳,PING保持
		{
			if(heartbeat() == 0)
			{
				timer_id_ = REACTOR_INSTANCE()->set_timer(this, param, SHORT_TCP_HEARTBEAT);
			}
			else
			{
				delete param;
			}
		}
		break;
	}

	return 0;
}