SOCKET_RESULT_CODE Connect(struct sockaddr* server, int socketLen, int msTimeout) {
        SOCKET_RESULT_CODE result = SOCKET_RESULT_FAIL;
        
        if (INVALID_SOCKET != m_socket )
        {
			// 获取当前block状态
			bool block = IsBlock();

			SetBlock(true);

            m_connStatus = CONNECTION_STATUS_CONNECTING;
			if( msTimeout > 0 ) {
                timeval timeout;
                timeout.tv_sec = msTimeout / 1000;
                timeout.tv_usec = msTimeout % 1000;
                socklen_t len = sizeof(timeout);
                setsockopt(m_socket, SOL_SOCKET, SO_SNDTIMEO, &timeout, len);
            }

            if (connect(m_socket, (struct sockaddr*)server, socketLen) == 0) {
                result = SOCKET_RESULT_SUCCESS;
            }

            if (SOCKET_RESULT_SUCCESS == result) {
                m_connStatus = CONNECTION_STATUS_CONNECTED;
            }
            else {
                m_connStatus = CONNECTION_STATUS_DISCONNECT;
            }
            
            // 回复block状态
			SetBlock(block);
        }
        return result;
    }
Beispiel #2
0
	void KBlockBox::Switch( int posA, int posB )
	{
		IBlock* ba = GetBlock(posA);
		IBlock* bb = GetBlock(posB);
		if (!ba && !bb) return;
		SetBlock(posB, ba);
		SetBlock(posA, bb);
	}
	// 连接(msTimeout:超时时间(毫秒),不大于0表示使用默认超时)
	virtual SOCKET_RESULT_CODE Connect(const string& ip, unsigned int port, int msTimeout) {
		SOCKET_RESULT_CODE result = SOCKET_RESULT_FAIL;

		// 定义socketaddr
		sockaddr_in server;
		server.sin_family = AF_INET;
		server.sin_addr.s_addr = inet_addr(ip.c_str());
		server.sin_port = htons(port);

		// 连接
		m_connStatus = CONNECTION_STATUS_CONNECTING;
		if (msTimeout > 0) {
			SetBlock(false);
			if (connect(m_socket, (SOCKADDR*)&server, sizeof(server)) == SOCKET_ERROR) {
				if (WSAGetLastError() == WSAEWOULDBLOCK) {
					timeval timeout;  
					timeout.tv_sec = msTimeout / 1000;
					timeout.tv_usec = msTimeout % 1000;
					fd_set writeset, exceptset;
					FD_ZERO(&writeset);
					FD_SET(m_socket, &writeset);
					FD_ZERO(&exceptset);
					FD_SET(m_socket, &exceptset);
		  
					int ret = select(FD_SETSIZE, NULL, &writeset, &exceptset, &timeout);
					if (ret == 0) {
						result = SOCKET_RESULT_TIMEOUT;
					}
					else if (ret > 0 && 0 != FD_ISSET(m_socket, &exceptset)) {
						result  = SOCKET_RESULT_SUCCESS;
					}
				}
				else if (WSAGetLastError() == 0) {
					result = SOCKET_RESULT_SUCCESS;
				}
			}
			else {
				result = SOCKET_RESULT_SUCCESS;
			}
			SetBlock(true);
		}
		else {
			SetBlock(true);
			if (connect(m_socket, (SOCKADDR*)&server, sizeof(server)) == 0) {
				result = SOCKET_RESULT_SUCCESS;
			}
		}

        if (SOCKET_RESULT_SUCCESS == result) {
            m_connStatus = CONNECTION_STATUS_CONNECTED;
        }
        else {
            m_connStatus = CONNECTION_STATUS_DISCONNECT;
        }
		return result;
	}
Beispiel #4
0
void CMonster::SetPosXY(float x, float y)
{
	// 将之前位置的阻挡去除
	if(GetFather())
		SetBlock(GetTileX(), GetTileY(), CRegion::BLOCK_NO);
	CShape::SetPosXY(x,y);
	// 设置新的阻挡位置
	if(GetFather())
		SetBlock(GetTileX(), GetTileY(), GetBlockType());
}
Beispiel #5
0
bool Shred::Tree(const int x, const int y, const int z, const int height) {
    if ( not InBounds(x+2, y+2, height+z) ) return false;
    // check for room
    for (int i=x; i<=x+2; ++i)
    for (int j=y; j<=y+2; ++j)
    for (int k=z; k<z+height; ++k) {
        if ( AIR != GetBlock(i, j, k)->Sub() ) {
            return false;
        }
    }
    const int leaves_level = z+height/2;
    Block * const leaves = Normal(GREENERY);
    for (int i=x; i<=x+2; ++i)
    for (int j=y; j<=y+2; ++j) {
        for (int k=leaves_level; k<z+height; ++k ) {
            PutBlock(leaves, i, j, k);
        }
    }
    for (int k=qMax(z-1, 1); k < z+height-1; ++k) { // trunk
        SetBlock(Normal(WOOD), x+1, y+1, k);
    }
    // branches
    const int r = qrand();
    if ( r & 0x1 ) SetNewBlock(BLOCK, WOOD, x,   y+1, leaves_level, WEST);
    if ( r & 0x2 ) SetNewBlock(BLOCK, WOOD, x+2, y+1, leaves_level, EAST);
    if ( r & 0x4 ) SetNewBlock(BLOCK, WOOD, x+1, y,   leaves_level, NORTH);
    if ( r & 0x8 ) SetNewBlock(BLOCK, WOOD, x+1, y+2, leaves_level, SOUTH);
    return true;
}
Beispiel #6
0
void World::RemSun() {
	SetBlock(
		Normal(ifStar ? STAR : SKY),
		sun_moon_x,
		SHRED_WIDTH*numShreds/2,
		HEIGHT-1);
}
Beispiel #7
0
bool World::Build(Block * block,
		const ushort x, const ushort y, const ushort z,
		const quint8 dir, Block * const who, const bool anyway)
{
	Block * const target_block = GetBlock(x, y, z);
	if ( ENVIRONMENT!=target_block->Movable() && !anyway ) {
		if ( who ) {
			who->ReceiveSignal(tr("Cannot build here."));
		}
		return false;
	} // else:
	const int old_transparency = target_block->Transparent();
	DeleteBlock(target_block);
	block->Restore();
	block->SetDir(dir);
	block = ReplaceWithNormal(block);
	SetBlock(block, x, y, z);
	if ( old_transparency != block->Transparent() ) {
		ReEnlightenBlockAdd(x, y, z);
	}
	const uchar block_light = block->LightRadius();
	if ( block_light ) {
		AddFireLight(x, y, z, block_light);
	}
	return true;
}
Beispiel #8
0
bool Map::TryBuildBlock(int i, int j, int id)
{
	if (GetBlock(i, j) != 0 || IntersectsEntities(new Box(i, j, 1, 1)))
		return false;
	SetBlock(i, j, id);
	return true;
}
Beispiel #9
0
void CCheckerCtrl::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here

	//Fill the background color of the client area
	dc.FillRect(m_rcClient, &m_backgroundBrush);
	dc.SetBkMode(TRANSPARENT);

	UINT nColumn = 0, nRow = 0;
	
	//Calculate the index of the last visible block
	//within the client area
	UINT nBlockEndPos = m_nBlockStartPos + m_nTotalVisibleBlocks + m_nBlocksPerRow;
	if(nBlockEndPos > m_nNumberofBlocks)
		nBlockEndPos = m_nNumberofBlocks;

	for(UINT i = m_nBlockStartPos; i < nBlockEndPos; i++)
	{
		CString cString(m_crText.GetAt(i));
		if (cString.GetLength())
		{
			CBrush brush(m_crColor.GetAt(i));
			SetBlock(nRow, nColumn, brush, dc, cString);
		}
		if((i + 1 - m_nBlockStartPos) % m_nBlocksPerRow == 0)
		{
			nRow++;
			nColumn = 0;
		}
		else
			nColumn++;
	}
}
Beispiel #10
0
void Shred::SetNewBlock(const int kind, const int sub,
        const int x, const int y, const int z, const int dir)
{
    Block * const block = BlockManager::NewBlock(kind, sub);
    block->SetDir(dir);
    SetBlock(block, x, y, z);
}
Beispiel #11
0
unsigned CRunFile::inputThreadProc()
{
	char buf[BUFSIZ];
	int size=0,l;

	SetBlock(m_inFd,false);

	do
	{
		if(!size)
			size=l=m_inputFn(buf,sizeof(buf),m_inputData);
		if(size<=0)
			break;

		if(m_debugFn) m_debugFn(0,buf+l-size,size,m_debugData);
	
		int s = write(m_inFd,buf+l-size,size);
		if(s<=0)
			break;
		size-=s;
		if(!size)
			size=l=m_inputFn(buf,sizeof(buf),m_inputData);
	} while(size>=0 && WaitForSingleObject(m_hProcess,100)==WAIT_TIMEOUT);

	close(m_inFd);

	return 0;
}
Beispiel #12
0
bool World::Inscribe(const ushort x, const ushort y, const ushort z) {
	Block * block=GetBlock(x, y, z);
	if ( LIQUID==block->Kind() || AIR==block->Sub() ) {
		return false;
	}
	if ( block==Normal(block->Sub()) ) {
		SetBlock(block=NewBlock(block->Kind(), block->Sub()), x, y, z);
	}
	QString str=tr("No note received.");
	emit GetString(str);
	if ( block->Inscribe(str) ) {
		return true;
	} else {
		SetBlock(ReplaceWithNormal(block), x, y, z);
		return false;
	}
}
Beispiel #13
0
// shred generators section
// these functions fill space between the lowest nullstone layer and sky.
// so use k from 1 to HEIGHT-2.
void Shred::CoverWith(const int kind, const int sub) {
    for (int i=0; i<SHRED_WIDTH; ++i)
    for (int j=0; j<SHRED_WIDTH; ++j) {
        int k = HEIGHT-2;
        for ( ; AIR==GetBlock(i, j, k)->Sub(); --k);
        SetBlock(BlockManager::NewBlock(kind, sub), i, j, ++k);
    }
}
Beispiel #14
0
ExtFunc void CopyLine(int scr, int from, int to)
{
	int x;

	if (from != to)
		for (x = 0; x < boardWidth[scr]; ++x)
			SetBlock(scr, to, x, GetBlock(scr, from, x));
}
Beispiel #15
0
void World::Drop(Block * const block_from,
		const ushort x_to, const ushort y_to, const ushort z_to,
		const ushort src, const ushort dest, const ushort num)
{
	Block * block_to=GetBlock(x_to, y_to, z_to);
	if ( AIR==block_to->Sub() ) {
		SetBlock((block_to=NewBlock(PILE, DIFFERENT)),
			x_to, y_to, z_to);
	} else if ( WATER==block_to->Sub() ) {
		Block * const pile = NewBlock(PILE, DIFFERENT);
		SetBlock(pile, x_to, y_to, z_to);
		pile->HasInventory()->Get(block_to);
		block_to = pile;
	}
	Exchange(block_from, block_to, src, dest, num);
	emit Updated(x_to, y_to, z_to);
}
Beispiel #16
0
	void MainScene::Init()
	{
		SetPlayer();
		SetBall();
		SetBlock();

		primeDrawTime = GetTickCount() + 4000;
		UiDrawTime = GetTickCount() + 3000;
	}
Beispiel #17
0
void Disp_Str(u16 x, u16 y, u16 Color, u8 *s)
{
  u16 i, j, b;
  SetBlock(x, y, LCD_ROW-1, y+13);
  for (i=0; i<14; i++) SetPixel(BG);    //×Ö·û´®Ç°Ôö¼ÓÒ»¿Õ°×ÁÐ
  while(*s != 0) {
    for(i=0; i<8; i++){
      b = Get_TAB_8x14(*s, i);
      for(j=0 ;j<14; ++j){
        if(b & 4) SetPixel(Color);
        else      SetPixel(BG);
        b >>= 1;
      }
    }
    s++;                                //×Ö·û´®Ö¸Õë+1
  }
  SetBlock(0, 0, LCD_ROW-1, LCD_COL-1); //»Ö¸´È«³ß´ç´°¿Ú
}
Beispiel #18
0
void Shred::DropBlock(Block * const block, const bool on_water) {
    int y = qrand();
    const int x = CoordInShred(y);
    y = CoordInShred(unsigned(y) >> SHRED_WIDTH_SHIFT);
    int z = HEIGHT-2;
    for ( ; GetBlock(x, y, z)->Sub()==AIR; --z);
    if( on_water || GetBlock(x, y, z)->Sub()!=WATER ) {
        SetBlock(block, x, y, ++z);
    }
}
Beispiel #19
0
ExtFunc void FreezePiece(int scr)
{
	int y, x;
	BlockType type;

	for (y = 0; y < boardHeight[scr]; ++y)
		for (x = 0; x < boardWidth[scr]; ++x)
			if ((type = board[scr][y][x]) < 0)
				SetBlock(scr, y, x, -type);
}
Beispiel #20
0
int32 CCommunicatingSocket::Connect(const string &strForeignAddr, uint16 u16ForeignPort)
{
    sockaddr_in destAddr;
    int32 i32Ret = 0;
    int32 i32Error = 0;
    int32 i32Len = 0;
    timeval tm;
    fd_set set;
    
    m_strForeignAddr = strForeignAddr;
    m_u16ForeignPort = u16ForeignPort;
    
    FillAddr(strForeignAddr, u16ForeignPort, destAddr);
    
    SetBlock(FALSE);
    if(connect(m_streamFd, (struct sockaddr *)&destAddr, sizeof(destAddr)) == -1)
    {
        tm.tv_sec = CONNECT_TIME_OUT_TIME;
        tm.tv_usec = 0;
        FD_ZERO(&set);
        FD_SET(m_streamFd, &set);
        if(select(m_streamFd+1, NULL, &set, NULL, &tm) > 0)
        {
            i32Len = sizeof(int32);
            getsockopt(m_streamFd, SOL_SOCKET, SO_ERROR, &i32Error, (socklen_t *)&i32Len);
            if(i32Error == 0)
            {
                i32Ret = 0;
            }
            else
            {
                i32Ret = -1;
            }
        } 
        else
        {
            i32Ret = -1;
        }
    }    
    SetBlock(TRUE);
    
    return i32Ret;
}
Beispiel #21
0
ExtFunc void InsertJunk(int scr, int count, int column)
{
	int y, x;

	for (y = boardHeight[scr] - count - 1; y >= 0; --y)
		CopyLine(scr, y, y + count);
	for (y = 0; y < count; ++y)
		for (x = 0; x < boardWidth[scr]; ++x)
			SetBlock(scr, y, x, (x == column) ? BT_none : BT_white);
	curY[scr] += count;
}
ADLog::const_iterator&
ADLog::const_iterator::operator--()
{
    if (mCur == mBlockStart) {
        SetBlock(mBlock->mPrev);
        mCur = mBlockEnd;
    }
    --mCur;

    return *this;
}
ADLog::const_iterator&
ADLog::const_iterator::operator++()
{
    ++mCur;
    if (mCur == mBlockEnd) {
        SetBlock(mBlock->mNext);
        mCur = mBlockStart;
    }

    return *this;
}
Beispiel #24
0
void World::Damage(const ushort x, const ushort y, const ushort z,
		const ushort dmg, const int dmg_kind)
{
	Block * temp = GetBlock(x, y, z);
	if ( temp==Normal(temp->Sub()) && AIR!=temp->Sub() ) {
		SetBlock((temp=NewBlock(temp->Kind(), temp->Sub())), x, y, z);
	}
	if ( temp->Damage(dmg, dmg_kind) > 0 ) {
		temp->ReceiveSignal(SOUND_STRINGS[1]); // "Ouch!"
		if ( block_manager.MakeId(BLOCK, STONE)==temp->GetId() &&
				temp->Durability()!=MAX_DURABILITY )
		{ // convert stone into ladder
			DeleteBlock(temp);
			SetBlock(NewBlock(LADDER, STONE), x, y, z);
			emit ReEnlighten(x, y, z);
		} else {
			SetBlock(ReplaceWithNormal(temp), x, y, z);
		}
	}
}
////////
// Copy
void VariableData::operator= (const VariableData &param) {
	switch(param.GetType()) {
		case VARDATA_INT: SetInt(param.AsInt()); break;
		case VARDATA_FLOAT: SetFloat(param.AsFloat()); break;
		case VARDATA_TEXT: SetText(param.AsText()); break;
		case VARDATA_BOOL: SetBool(param.AsBool()); break;
		case VARDATA_COLOUR: SetColour(param.AsColour()); break;
		case VARDATA_BLOCK: SetBlock(param.AsBlock()); break;
		default: DeleteValue();
	}
}
Beispiel #26
0
unsigned CRunFile::outputThreadProc()
{
	char buf[BUFSIZ*10];
	int size;

	if(m_outFd>=0)
		SetBlock(m_outFd,false);
	if(m_errFd>=0)
		SetBlock(m_errFd,false);

	do
	{
		while(m_errFd>=0 && (size=read(m_errFd,buf,BUFSIZ*10))>0)
		{
			if(m_errorFn && m_errorFn != StandardError) m_errorFn(buf,size,m_errorData);
			if(m_debugFn) m_debugFn(2,buf,size,m_debugData);
		}
		while(m_outFd>=0 && (size=read(m_outFd,buf,BUFSIZ*10))>0)
		{
			if(m_outputFn && m_outputFn != StandardOutput) m_outputFn(buf,size,m_outputData);
			if(m_debugFn) m_debugFn(1,buf,size,m_debugData);
		}
	} while(WaitForSingleObject(m_hProcess,100)==WAIT_TIMEOUT);

	while(m_errFd>=0 && (size=read(m_errFd,buf,BUFSIZ*10))>0)
	{
		if(m_errorFn && m_errorFn != StandardError) m_errorFn(buf,size,m_errorData);
		if(m_debugFn) m_debugFn(2,buf,size,m_debugData);
	}
	while(m_outFd>=0 && (size=read(m_outFd,buf,BUFSIZ*10))>0)
	{
		if(m_outputFn && m_outputFn != StandardOutput) m_outputFn(buf,size,m_outputData);
		if(m_debugFn) m_debugFn(1,buf,size,m_debugData);
	}

	if(m_outFd>=0)
		close(m_outFd);
	if(m_errFd>=0)
		close(m_errFd);
	return 0;
}
Beispiel #27
0
 void Field::NewFruit()
 {
   int x;
   int y;
   
   do
   {
     x = rand() % WIDTH;
     y = rand() % HEIGHT;
   } while (GetBlock(x, y) != EMPTY);
   SetBlock(FRUIT, x, y);
 }
Beispiel #28
0
void Shred::PlantGrass() {
    for (int i=0; i<SHRED_WIDTH; ++i)
    for (int j=0; j<SHRED_WIDTH; ++j) {
        int k = HEIGHT - 2;
        for ( ; GetBlock(i, j, k)->Transparent(); --k);
        if ( SOIL==GetBlock(i, j, k)->Sub()
                && AIR==GetBlock(i, j, ++k)->Sub() )
        {
            SetBlock(BlockManager::NewBlock(GRASS, GREENERY), i, j, k);
        }
    }
}
////////
// Copy
void AssOverrideParameter::CopyFrom (const AssOverrideParameter &param) {
	switch(param.GetType()) {
		case VARDATA_INT: SetInt(param.AsInt()); break;
		case VARDATA_FLOAT: SetFloat(param.AsFloat()); break;
		case VARDATA_TEXT: SetText(param.AsText()); break;
		case VARDATA_BOOL: SetBool(param.AsBool()); break;
		case VARDATA_COLOUR: SetColour(param.AsColour()); break;
		case VARDATA_BLOCK: SetBlock(param.AsBlock()); break;
		default: DeleteValue();
	}
	classification = param.classification;
	ommited = param.ommited;
}
Beispiel #30
0
void Map::DestroyBlock(int i, int j)
{
	if (GetBlock(i, j) == 0)
		return;
	int id = blocks[i][j].Id;

	SetBlock(i, j, 0);

	EntityItem* item = new EntityItem(Block::GetBlockById(id)->GetDrop());
	item->SetPosition(glm::vec2(i + rand() % 500 / 1000.0, j + rand() % 500 / 1000.0));
	drops.push_back(item);
	entities.push_back(item);
}