void IOServiceLibEvent::send(const ChannelId& id,
		const std::list<mxcore::SharedPtr<mxcore::ByteBuffer> > messageList,
		int perMsgTimeout)
{
	ChannelContext* ctx = findChannel(id);

	MX_ASSERT(NULL != ctx);
	MX_ASSERT(ctx->isConnector());

	for (std::list<mxcore::SharedPtr<mxcore::ByteBuffer> >::const_iterator it =
			messageList.begin(); it != messageList.end(); it++)
	{
		MX_ASSERT((*it)->remaining() > 0);
	}

	ConnectorChannelContext* connectorCtx = (ConnectorChannelContext*) ctx;

	bool haveMessage = connectorCtx->haveMessageToWrite();

	connectorCtx->putWriteMessage(messageList, perMsgTimeout);

	if (!haveMessage)
	{
		connectorCtx->registWrite();
		connectorCtx->registWriteTimer(perMsgTimeout);
	}
}
bool HttpVirtualServer::addServlet(const std::string& path,
		mxcore::SharedPtr<HttpServlet> servlet)
{
	MX_ASSERT(!path.empty());
	MX_ASSERT(!servlet.isNull());

	ServletMap::const_iterator it = servlets_.find(path);

	if (it != servlets_.end())
	{
		return false;
	}

	pcre* re = mxhttp::PcreUtil::open(path);

	if (NULL == re)
	{
		return false;
	}

	servlets_.insert(std::make_pair(path, servlet));
	pcres_.insert(std::make_pair(re, servlet));

	return true;
}
Exemple #3
0
MessageBuffer& MessageBuffer::put(const void* data, uint32_t len)
{
	MX_ASSERT(NULL != data && len > 0);
	MX_ASSERT(len <= writable());

	return putImpl(data, len);
}
void mx_LevelGenerator::AddConnectionCube( const Connection* connection )
{
	const unsigned int c_side_count= 4;

	ReserveTrianglesAndVertices( c_side_count * 2, c_side_count * 4 );
	MX_ASSERT( out_level_data_.vertex_count + c_side_count * 4 <= out_level_data_.vertices_capacity );
	MX_ASSERT( out_level_data_.triangle_count + c_side_count * 2 < out_level_data_.triangles_capacity );

	float min_max[2][3]; // 0 - min, 1 - max
	for( unsigned int i= 0; i < 3; i++ )
	{
		float b= float(connection->coord_begin[i]);
		float e= float(connection->coord_end[i]);
		if( b > e )
			min_max[0][i]= e, min_max[1][i]= b;
		else
			min_max[0][i]= b,  min_max[1][i]= e;
		min_max[1][i]++;
	}

	unsigned int skip_side;
	if( connection->coord_begin[0] != connection->coord_end[0] )
		skip_side= 0;
	else if( connection->coord_begin[1] != connection->coord_end[1] )
		skip_side= 1;
	else
	{
		skip_side= 2;
		MX_ASSERT( connection->coord_begin[2] != connection->coord_end[2] );
	}

	mx_LevelVertex* v= out_level_data_.vertices + out_level_data_.vertex_count;
	for( unsigned int i= 0; i < 4 * 6; i++ )
	{
		if( i / 8u == skip_side ) continue;
		const unsigned char *c= c_cube_vertices + 3 * i;
	
		v->xyz[0]= min_max[ c[0] ][0];
		v->xyz[1]= min_max[ c[1] ][1];
		v->xyz[2]= min_max[ c[2] ][2];
		v++;
	}

	mx_LevelTriangle* triangle= out_level_data_.triangles + out_level_data_.triangle_count;
	for(
		unsigned int i= out_level_data_.vertex_count;
		i < out_level_data_.vertex_count + c_side_count * 4; i+=4, triangle+=2 )
	{
		triangle[0].vertex_index[0]= (i    );
		triangle[0].vertex_index[1]= (i + 1);
		triangle[0].vertex_index[2]= (i + 2);

		triangle[1].vertex_index[0]= (i    );
		triangle[1].vertex_index[1]= (i + 2);
		triangle[1].vertex_index[2]= (i + 3);
	}

	out_level_data_.vertex_count+= c_side_count * 4;
	out_level_data_.triangle_count+= c_side_count * 2;
}
Exemple #5
0
uint32_t MessageBuffer::fullForGet(mxos::IOVEC* vec, uint32_t count) const
{
	MX_ASSERT(NULL != vec && count > 0);
	int rable = readable();
	MX_ASSERT(rable > 0);

	int len = capacity_ - readPos_;

	if (len > rable)
	{
		len = rable;
	}

	vec[0].iov_base = buffer_ + readPos_;
	vec[0].iov_len = len;

	rable -= len;

	if (rable > 0 && count > 1)
	{
		vec[1].iov_base = buffer_;
		vec[1].iov_len = rable;
		return 2;
	}

	return 1;
}
Exemple #6
0
uint32_t MessageBuffer::fullForPut(mxos::IOVEC* vec, uint32_t count) const
{
	MX_ASSERT(NULL != vec && count > 0);

	int wable = writable();

	MX_ASSERT(wable > 0);

	int len = capacity_ - writePos_;

	if (len > wable)
	{
		len = wable;
	}

	vec[0].iov_base = buffer_ + writePos_;
	vec[0].iov_len = len;

	wable -= len;

	if (wable > 0 && count > 1)
	{
		vec[1].iov_base = buffer_;
		vec[1].iov_len = wable;
		return 2;
	}

	return 1;
}
void mx_LevelGenerator::PlaceRooms()
{
	Room* room= rooms_;

	{ // Place central room
		for( unsigned int j= 0; j < 3; j++ )
		{
			room->coord_min[j]= MX_MAX_LEVEL_SIZE_CELLS / 2 - MX_CENTRAL_ROOM_HALF_SIZE;
			room->coord_max[j]= MX_MAX_LEVEL_SIZE_CELLS / 2 + MX_CENTRAL_ROOM_HALF_SIZE;
		}
		for( int z= room->coord_min[2]; z < room->coord_max[2]; z++ )
		for( int y= room->coord_min[1]; y < room->coord_max[1]; y++ )
		for( int x= room->coord_min[0]; x < room->coord_max[0]; x++ )
			ElementMap( x, y, z )= room;

		room->connection_count= 0;
		room++;
		room_count_++;
	}

	for( unsigned int i= 1; i < MX_MAX_ROOMS * 2; i++ )
	{
		// Create room
		for( unsigned int j= 0; j < 3; j++ )
		{
			room->coord_min[j]= rand_.RandI( 1, MX_MAX_LEVEL_SIZE_CELLS - MX_MAX_ROOM_SIZE - 1 );
			room->coord_max[j]= room->coord_min[j] + rand_.RandI( MX_MIN_ROOM_SIZE, MX_MAX_ROOM_SIZE + 1 );

			MX_ASSERT( room->coord_min[j] >= 1 && room->coord_min[j] < MX_MAX_LEVEL_SIZE_CELLS - 1 );
			MX_ASSERT( room->coord_max[j] >= 1 && room->coord_max[j] < MX_MAX_LEVEL_SIZE_CELLS - 1 );
		}

		// Check free space
		for( int z= std::max( 0, room->coord_min[2] - MX_MIN_ROOM_DISTANCE );
			z < std::min( MX_MAX_LEVEL_SIZE_CELLS, room->coord_max[2] + MX_MIN_ROOM_DISTANCE );
			z++ )
		for( int y= std::max( 0, room->coord_min[1] - MX_MIN_ROOM_DISTANCE );
			y < std::min( MX_MAX_LEVEL_SIZE_CELLS, room->coord_max[1] + MX_MIN_ROOM_DISTANCE );
			y++ )
		for( int x= std::max( 0, room->coord_min[0] - MX_MIN_ROOM_DISTANCE );
			x < std::min( MX_MAX_LEVEL_SIZE_CELLS, room->coord_max[0] + MX_MIN_ROOM_DISTANCE );
			x++ )
			if( ElementMap( x, y, z ) != NULL )
				goto failed;

		// Mark space for this room
		for( int z= room->coord_min[2]; z < room->coord_max[2]; z++ )
		for( int y= room->coord_min[1]; y < room->coord_max[1]; y++ )
		for( int x= room->coord_min[0]; x < room->coord_max[0]; x++ )
			ElementMap( x, y, z )= room;

		room->connection_count= 0;

		room++;
		room_count_++;
		if( room_count_ == MX_MAX_ROOMS ) break;
	failed:;
	}
}
Exemple #8
0
void mxface_init( MxFace *f, MxVertexID v0, MxVertexID v1, MxVertexID v2 )
{
	MX_ASSERT( v0 != v1 );
	MX_ASSERT( v1 != v2 );
	MX_ASSERT( v2 != v0 );
	f->v[0] = v0;
	f->v[1] = v1;
	f->v[2] = v2;
}
Exemple #9
0
static void swapb_big( Block *b, int i, int j, void *place1, void *place2 )
{
	void *temp = MX_ALLOC ( b->size_each );

	MX_ASSERT( i >= 0 && i < b->used );
	MX_ASSERT( j >= 0 && j < b->used );

	MX_COPY (place1, b->size_each, temp);
	MX_COPY (place2, b->size_each, place1);
	MX_COPY (temp,   b->size_each, place2);

	MX_FREE( temp, b->size_each );
}
void mx_LevelGenerator::Generate()
{
	MX_ASSERT( room_count_ == 0 );
	MX_ASSERT( connection_count_ == 0 );

	PlaceRooms();
	PlaceConnections();
	CalculateLinkage();

	GenerateMeshes();
	CalculateNormals();
	CalculateTextureCoordinates();
	SetupTextures();
}
Exemple #11
0
static bool getAddrInfo(const char* hostname, int af, void* addr, int* addr_len)
{
	bool ret = false;
	struct addrinfo hints;
	struct addrinfo* res = NULL;

	MX_ASSERT(NULL != hostname);

	::memset(&hints, 0, sizeof(hints));
	hints.ai_family = af;

	if (0 == ::getaddrinfo(hostname, NULL, &hints, &res) && (res->ai_addrlen
			<= sizeof(struct sockaddr_in6)))
	{
		ret = true;
		*addr_len = res->ai_addrlen;
		::memcpy(addr, res->ai_addr, res->ai_addrlen);
	}

	if (NULL != res)
	{
		::freeaddrinfo(res);
	}

	return ret;
}
Exemple #12
0
MessageBuffer& MessageBuffer::put(uint64_t value)
{
	MX_ASSERT(writable() >= sizeof(value));

	uint64_t orderValue = ByteOrder::NATIVE_ORDER.toOrder(value, byteOrder_);
	return putImpl(&orderValue, sizeof(orderValue));
}
Exemple #13
0
void SocketAddress::set(const char* hostname, int family, uint16_t port)
		throw (UnknownHostException)
{
	int addrLen = 0;

	MX_ASSERT(NULL != hostname);

	if (AF_UNSPEC == family)
	{
		if (!getAddrInfo(hostname, AF_INET6, &data_, &addrLen) && !getAddrInfo(
				hostname, AF_INET, &data_, &addrLen))
		{
			THROW2(UnknownHostException, std::string("Unknown hostname: [") + hostname + "]");
		}
	}
	else
	{
		if (!getAddrInfo(hostname, family, &data_, &addrLen))
		{
			THROW2(UnknownHostException, std::string("Unknown hostname: [") + hostname + "]");
		}
	}

	setPort(port);
}
Exemple #14
0
bool ServerSocket::accept(Socket& client) throw (IOException&)
{
	MX_ASSERT(!isClosed());

	int sock = mxos::accept(handle_, NULL, NULL);
	if (-1 == sock)
	{
		int err = mxos::getLastSocketError();
#if defined(WIN32)
		if (WSAEWOULDBLOCK == err || WSAEINTR == err)
		{
			return false;
		}
#else
		if (EAGAIN == err || EINTR == err)
		{
			return false;
		}
#endif
		THROW3 (IOException, "Can't accept socket", err);
	}

	client.attach(sock, true, true);
	return true;
}
Exemple #15
0
static void downheaph( MxHeap *h, int i )
{
    int l = left(i);
    int r = right(i);
	MxHeapable *moving, *hl, *hr, *largest;
	int oldpos, newpos;
    MX_ASSERT( i >= 0 && i < lengthb( &(h->data) ) );

    /* we are careful here to handle the case where l is in range, but r is not */
    while( l < lengthb( &(h->data) ) )
    {
	    moving = (MxHeapable *) getpb ( &(h->data), i );
		hl     = (MxHeapable *) getpb ( &(h->data), l );
		hr     = (MxHeapable *) getpb ( &(h->data), r );
		if( 
		    ( r < lengthb( &(h->data) ) ) && 
			( get_heap_key( hl ) < get_heap_key( hr ) )
		)
		    largest = hr;
		else 
		    largest = hl;

		if( get_heap_key( moving ) < get_heap_key( largest ) )
		{
			oldpos = get_heap_pos( moving );
			newpos = get_heap_pos( largest );
		    swaph( h, oldpos, newpos );
			i = newpos;
			l = left( i );
			r = right( i );
		}
		else
			break;
    }
}
Exemple #16
0
/* remove a particular element of the heap */
void removeh( MxHeap *h, MxHeapable *t )
{
	MxHeapable *temp;
	int i, end;

    if( !is_in_heap( t ) ) return;
    i = get_heap_pos( t );
    end = lengthb( &(h->data) ) - 1;
	MX_ASSERT( i >= 0 && i <= end );
	not_in_heap( t );
    if( i!= end )
    {
        swaph( h, i, end );
        chopb( &(h->data) );

        temp = (MxHeapable *) getpb ( &(h->data), i );
        if( get_heap_key( temp ) < get_heap_key( t ) )
     		downheaph( h, i );
        else
	        upheaph( h, i );
    }
    else
        chopb( &(h->data) ); /* remove last element */
        

}
Exemple #17
0
int16_t MessageBuffer::getShort(void)
{
	int16_t ret;
	MX_ASSERT(readable() >= sizeof(int16_t));

	getImpl(&ret, sizeof(int16_t));
	return byteOrder_.toOrder(ret, ByteOrder::NATIVE_ORDER);
}
Exemple #18
0
Logger::Logger(int level, const std::string& name, uint16_t bufferSize,
		mxcore::SharedPtr<LoggerImpl> impl) :
	level_(level), name_(name), buffer_(NULL), bufferSize_(bufferSize),
			impl_(impl)
{
	MX_ASSERT(bufferSize_ > 0);
	buffer_ = new char[bufferSize_ + 1];
}
Exemple #19
0
uint64_t MessageBuffer::getULong(void)
{
	uint64_t ret;
	MX_ASSERT(readable() >= sizeof(ret));

	getImpl(&ret, sizeof(uint64_t));
	return byteOrder_.toOrder(ret, ByteOrder::NATIVE_ORDER);
}
Exemple #20
0
void swapb( Block *b, int i, int j)
{
	char tempspace[12];
	void *temp = tempspace;
	void *place1 = (char *) b->data + (i * b->size_each);
	void *place2 = (char *) b->data + (j * b->size_each);

	if( b->size_each > 12 ) swapb_big( b, i, j, place1, place2 );

	MX_ASSERT( i >= 0 && i < b->used );
	MX_ASSERT( j >= 0 && j < b->used );

	MX_COPY (place1, b->size_each, temp);
	MX_COPY (place2, b->size_each, place1);
	MX_COPY (temp,   b->size_each, place2);

}
Exemple #21
0
/* access in place without modifying heap.  
 * unused in the LOD module */
void *itemh( MxHeap *h, int i )
{
	MxHeapable *temp;

    MX_ASSERT( i >= 0 && i < lengthb( &(h->data) ) );
    temp = (MxHeapable *) getpb( &(h->data), i ); 
	return temp->payload; 
}
Exemple #22
0
void ServerSocket::attach(int handle, bool isBlocking)
{
	MX_ASSERT(-1 != handle);

	close();

	handle_ = handle;
	isBlocking_ = isBlocking;
}
Exemple #23
0
void mxquadric3_init( MxQuadric3 *q, double a, double b, double c, double d, double area)
{
    q->a2 = a*a;  q->ab = a*b;  q->ac = a*c;  q->ad = a*d;
                  q->b2 = b*b;  q->bc = b*c;  q->bd = b*d;
						        q->c2 = c*c;  q->cd = c*d;
							                  q->d2 = d*d;

    q->r = area;

    MX_ASSERT(!( quad_evaluate( q, 0, 0, 0 ) < 0 ));
    MX_ASSERT(!( quad_evaluate( q, 0, 0, 0 ) < 0 ));
/*
	MX_ASSERT( ((*((int *)(&q->a2)) >> 23) & 0xFF) != 0xFF );
	MX_ASSERT( ((*((int *)(&q->a2)) >> 23) & 0xFF) != 0xFF );
	MX_ASSERT( !isNaN( q->a2 ) );
*/

} /* end function mxquadric3_init */
Exemple #24
0
MxBool face_is_inorder( const MxFace *f, MxVertexID v0, MxVertexID v1 )
{
	if( f->v[0]==v0 ) return f->v[1]==v1;
	else if( f->v[1]==v0 ) return f->v[2]==v1;
	else { 
		MX_ASSERT(f->v[2]==v0); 
		return f->v[0]==v1; 
	}
}
Exemple #25
0
int face_find_vertex( const MxFace *f, MxVertexID i )
{
	if( f->v[0]==i ) return 0;
	else if( f->v[1]==i ) return 1;
	else { 
		MX_ASSERT(f->v[2]==i); 
		return 2; 
	}
}
Exemple #26
0
/* HELPER
 * calculates the maximum from 3 doubles
 */
static double max3f( double f1, double f2, double f3 )
{
    if( f1 >= f2 )
        if( f1 >= f3 )
            return f1;
        else {
            MX_ASSERT( f3 >= f2 );
            return f3;
        }
    else
        if( f2 >= f3 )
            return f2;
        else {
            MX_ASSERT( f3 >= f1 );
            return f3;
        }  
       
} /* end function max3f */
Exemple #27
0
bool SyncEchoClient::sendAndRecv(const std::string& message)
		throw (mxcore::IOException&)
{
	MX_ASSERT(!message.empty());
	std::string recvMsg;
	socket_.writeN(message.c_str(), message.size());
	bool ret = readN(socket_, recvMsg, (int) message.size());
	return ret && (message == recvMsg);
}
Exemple #28
0
MxVertexID face_opposite_vertex( const MxFace *f, MxVertexID v0, MxVertexID v1 )
{
	if( f->v[0]!=v0 && f->v[0]!=v1 ) return f->v[0];
	else if( f->v[1]!=v0 && f->v[1]!=v1 ) return f->v[1];
	else { 
		MX_ASSERT( f->v[2]!=v0 && f->v[2]!=v1 ); 
		return f->v[2]; 
	}
}
Exemple #29
0
void mxv_cross(double *r, const double *u, const double *v, int dim)
{
	if( dim == 3 )
	{
	    r[0] =  (u[1] * v[2]) - (v[1] * u[2]); 
		r[1] = -(u[0] * v[2]) + (v[0] * u[2]);
		r[2] =  (u[0] * v[1]) - (v[0] * u[1]);  
	}
	else MX_ASSERT(0);
}
void IOServiceLibEvent::send(const ChannelId& id,
		mxcore::SharedPtr<mxcore::ByteBuffer> message, int timeout)
{
	ChannelContext* ctx = findChannel(id);

	MX_ASSERT(NULL != ctx);
	MX_ASSERT(ctx->isConnector());
	MX_ASSERT(message->remaining() > 0);

	ConnectorChannelContext* connectorCtx = (ConnectorChannelContext*) ctx;

	bool haveMessage = connectorCtx->haveMessageToWrite();

	connectorCtx->putWriteMessage(ChannelMessage(message, timeout, false));

	if (!haveMessage)
	{
		connectorCtx->registWrite();
		connectorCtx->registWriteTimer(timeout);
	}
}