Example #1
0
int Socket::readImpl(char* data,int size)
{
	if(m_socket == int(INVALID_SOCKET))
		return 0;

	int res=recv(m_socket,data,size,0);
	
	if(res==SOCKET_ERROR)
	{
#ifdef WIN32
		if(WSAGetLastError()!=WSAEWOULDBLOCK)		
#else
		if(errno!=EWOULDBLOCK)	
#endif
		{
			close();
			throw ConnectionClosedException("Reading failed: " + getErrorMessage());
		}
		
		return 0;
	}
	
	if(res==0)
	{
		close();
		throw ConnectionClosedException("Connection closed gracefully.");
	}
	
	return res;
}
Example #2
0
void Socket::commitWrite()
{
	if(m_write_buffer.size() == 0)
		return;
	
	int res;
		
	do
	{	
		res=send(m_socket,m_write_buffer.c_str(),m_write_buffer.size(),0);
		
		if(res==SOCKET_ERROR)
		{
#ifdef WIN32
			if(WSAGetLastError()!=WSAEWOULDBLOCK)		
#else
			if(errno!=EWOULDBLOCK)	
#endif
			{
				close();
				throw ConnectionClosedException("Writing failed: " + getErrorMessage());
			}
			
			return;
		}
		
		m_write_buffer.erase(0,res);
	}
	while(res);
}
Example #3
0
void ChannelImpl::CheckIsConnected()
{
    if (!m_is_connected)
    {
        throw ConnectionClosedException();
    }
}
Example #4
0
	/**
	 * ソケットからのデータの取得
	 * @param buffer 読み込んだデータへのポインタ
	 * @param readSize 読み込み可能な最大サイズ
	 * @return 実際に読み込まれたデータサイズ
	 * @note winsockのぼけぇ!!recvの戻り値がsize_tじゃないなんて・・・
	 */
	size_t read(void* buffer, const size_t readSize) 
	{
		const int readed = recv(socket,
								static_cast<char*>(buffer),
								static_cast<int>(readSize), 0);
		if (readed <= 0)
		{
			close();
			open();
			throw ConnectionClosedException();
		}
		return readed;
	}
Example #5
0
	/**
	 * ソケットへのデータ書き込み
	 * @param buffer 書き込むデータへのポインタ
	 * @param writeSize 書き込むデータのサイズ
	 * @return 実際に書き込まれたデータのサイズ
	 * @throw ConnectionClosedException 
	 */
	size_t write(const void* buffer, const size_t writeSize) 
	{
		const int writed = send(socket,
								static_cast<const char*>(buffer),
								static_cast<int>(writeSize), 0);
		if (writed < 0)
		{
			close();
			open();
			throw ConnectionClosedException();
		}

		return writed;
	}
Example #6
0
bool CTReader::read()
{
	ControllerInf *con = m_decoder.getController();

	static bool start_sim = false;

	static double timewidth = 0.0;

	struct timeval start, eoa;
	timeval now; 

	static double server_startTime = 0.0;

	if (start_sim) {

		//now = clock();
		gettimeofday(&now, NULL);

		double tmp_time = (double)(now.tv_sec - eoa.tv_sec) + (double)(now.tv_usec - eoa.tv_usec) * 0.001 * 0.001;
		if (tmp_time >= timewidth) {
			ActionEvent aevt;
      
			double nowtime = (double)(now.tv_sec - start.tv_sec) + (double)(now.tv_usec - start.tv_usec) * 0.001 * 0.001;

			nowtime += server_startTime;
			aevt.setTime(nowtime);
      
			timewidth = con->onAction(aevt);
      
			gettimeofday(&eoa, NULL);

			Controller *conn = (Controller*)con;
			//conn->updateObjs();
		}
	}

	SOCKET s = m_sock;

	fd_set rfds;
	struct timeval tv;
	FD_ZERO(&rfds);
	FD_SET(s, &rfds);

	ControllerImpl* coni = (ControllerImpl*)con;
	std::map<std::string, SOCKET> ssocks = coni->getSrvSocks();
	std::map<std::string, SOCKET>::iterator it = ssocks.begin();
	while (it != ssocks.end()) {
		FD_SET((*it).second, &rfds);
		it++;
	}
	tv.tv_sec = 0;
	//tv.tv_usec = 100000;
	tv.tv_usec = 1000; 
  
	int ret = select(FD_SETSIZE, &rfds, NULL, NULL, &tv);
	if (ret == -1) {
		perror("select");
		return false;
	}
  
	else if (ret == 0) {
		return true;
	}

	else if (ret > 0) {

		if (FD_ISSET(s, &rfds)) {

			int rbytes;
			if (m_buf->datasize() == 0) {
				rbytes = m_buf->read(s, 4);
#if 1
				// sekikawa(FIX20100826)
				if (rbytes < 0) {
					if (errno == ECONNRESET) {
						LOG_SYS(("connection closed by service provider [%s:%d]", __FILE__, __LINE__));
					} else {
						LOG_SYS(("socket error (errno=%d) [%s:%d]", errno, __FILE__, __LINE__));
					}
					throw ConnectionClosedException();
				}
#endif

				if (rbytes > 0) {

					char *data = m_buf->data();
					char *p = data;
					unsigned short token = BINARY_GET_DATA_S_INCR(p, unsigned short);

					if (token == COMM_DATA_PACKET_START_TOKEN) {
						unsigned short size = BINARY_GET_DATA_S_INCR(p, unsigned short);
#if 1
						// sekikawa(FIX20100826)
						int rbytes2 = m_buf->read(s, size-4);
						if (rbytes2 < 0) {
							if (errno == ECONNRESET) {
								LOG_SYS(("connection closed by service provider [%s:%d]", __FILE__, __LINE__));
							} else {
								LOG_SYS(("socket error (errno=%d) [%s:%d]", errno, __FILE__, __LINE__));
							}
	    
							throw ConnectionClosedException();
						}
						rbytes += rbytes2;
						/*
						  #else
						  // orig
						  rbytes += m_buf->read(s, size-4);
						  }
						*/
#endif
					}
				
					else if (token == START_SIM) {