Exemple #1
0
bool MemcachedClient::Request(const char* buf, int len, CmdType& t, map<string, string>& resdata, map<string, int>& flags){
  t = CMD_NOTDEF;
  cur_recv_len_ = 0;
  total_recv_len_ = 0;
  res_.clear();
  int retry = 2;
  
  //服务端重启的情况下,会导致send失败,需要重新链接再send
  while(retry-- > 0){
    if(!CheckConnect()){
      return false;
    }
    if(Send(buf, len)){
      break;
    }else{
      connected_ = false;
      continue;
    }
  }
  int pos = 0;
  bool suc = false;
  int count = 0;
  while(true){
    struct pollfd fds;
    fds.fd=sockfd_;
    fds.events=POLLIN;
    int res = poll(&fds, 1, timeout_);
    if(res <= 0){
      //printf("@@@@@@@@3 %d\n", count);
      if(res < 0){
        MCE_WARN("MemcachedClient::Request --> poll err, " << res << " " << strerror(errno));
      }else{
        MCE_INFO("MemcachedClient::Request --> poll timeout, " << res << " " << strerror(errno));
      }
      return false;
    }else if(res==1){
      bool s = HandleRead(t, resdata, flags);
      //printf("HandleRead %d %d\n", s, t);
      if(!s){
        //printf("@@@@@@@@4\n");
        return false;
      }
      if(t==CMD_VALUE || t==CMD_NOTDEF){
        count++;
        continue;
      }else if(t==CMD_END || t==CMD_STORED || t==CMD_NOT_STORED || t==CMD_DELETED || t==CMD_NOT_FOUND){
        return true; 
      }else{
        MCE_WARN("MemcachedClient::Request --> parser err " << t);
        return false;
      }
    }
    count++;
  }
  //printf("@@@@@@@@ 2222\n");
  MCE_WARN("MemcachedClient::Request --> parser err3 " << t);
  return false;
}
Exemple #2
0
STDMETHODIMP
BasePin::ReceiveConnection(IPin * aPin,
                             const AM_MEDIA_TYPE *aMediaType)
{
  if (!aPin)
    return E_POINTER;

  if (!aMediaType)
    E_POINTER;

  CriticalSectionAutoEnter monitor(*mLock);

  if (IsConnected())
    return VFW_E_ALREADY_CONNECTED;

  if (!IsStopped())
    return VFW_E_NOT_STOPPED;

  HRESULT hr = CheckConnect(aPin);
  if (FAILED(hr)) {
    BreakConnect();
    return hr;
  }

  // See if subclass supports the specified media type.
  const MediaType* mediaType = reinterpret_cast<const MediaType*>(aMediaType);
  hr = CheckMediaType(mediaType);
  if (FAILED(hr)) {
    BreakConnect();
    return hr;
  }

  // Supported, set it.
  hr = SetMediaType(mediaType);
  if (FAILED(hr))
    return hr;

  // Complete the connection.
  mConnectedPin = aPin;
  // Give the subclass one last chance to refuse the connection.
  hr = CompleteConnect(aPin);
  if (FAILED(hr)) {
    // Subclass refused connection, fail...
    mConnectedPin = NULL;
    BreakConnect();
    return hr;
  }

  // It's all good, we're connected.
  return S_OK;
}
Exemple #3
0
// Attempt to connect this pin to |aPin| using given media type.
HRESULT
BasePin::AttemptConnection(IPin* aPin,
                             const MediaType* aMediaType)
{
  CriticalSectionAutoEnter monitor(*mLock);

  // Ensure we can connect to the other pin. Gives subclasses a chance
  // to prevent connection.
  HRESULT hr = CheckConnect(aPin);
  if (FAILED(hr)) {
    BreakConnect();
    return hr;
  }

  // Ensure we can connect with this media type. This gives subclasses a
  // chance to abort the connection.
  hr = CheckMediaType(aMediaType);
  if (FAILED(hr))
    return hr;

  hr = SetMediaType(aMediaType);
  if (FAILED(hr))
    return hr;

  // Ask the other pin if it will accept a connection with our media type.
  hr = aPin->ReceiveConnection(static_cast<IPin*>(this), aMediaType);
  if (FAILED(hr))
    return hr;

  // Looks good so far, give subclass one final chance to refuse connection...
  mConnectedPin = aPin;
  hr = CompleteConnect(aPin);

  if (FAILED(hr)) {
    // Subclass refused the connection, inform the other pin that we're
    // disconnecting, and break the connection.
    aPin->Disconnect();
    BreakConnect();
    mConnectedPin = NULL;
    mMediaType.Clear();
    return VFW_E_TYPE_NOT_ACCEPTED;
  }

  // Otherwise, we're all good!
  return S_OK;
}
ErrorCode BasePinImpl::ReceiveConnection(IPin *pConnector, MediaType* pMediaType)
{
	if (pConnector == NULL)
	{
		ASSERT(0);
		//return E_INVALIDARG;
		throw -1;
	}

	if (m_dir != PINDIR_INPUT)
	{
		ASSERT(0);
		return Error;
	}

	ErrorCode hr;

	hr = CheckConnect(pConnector);
	if (hr < 0)
	{
		BreakConnect();
		return hr;
	}

	hr = CheckMediaType(pMediaType);
	if (hr < 0)
	{
		BreakConnect();
		return hr;
	}

	// Is the order of these last two correct?

	SetMediaType(pMediaType);

	hr = CompleteConnect(pConnector);
	if (hr < 0)
	{
		// BreakConnect(); ??
		return hr;
	}

	return 0;
}
ErrorCode BasePinImpl::AttemptConnection(IPin *pReceivePin, MediaType* mt)
{
	ErrorCode hr;

	//CheckConnect( Why Is this called here, it will be called multiple times if it's here?

	hr = CheckConnect(pReceivePin);
	if (hr < 0)
	{
		BreakConnect();
		return hr;
	}

	hr = CheckMediaType(mt);
	if (hr < 0)
	{
		BreakConnect();
		return hr;
	}

	hr = pReceivePin->ReceiveConnection(this, mt);
	if (hr < 0) return hr;

	hr = SetMediaType(mt);	// ?? Have this here??
	if (hr < 0)
	{
		BreakConnect();
		return hr;
	}

	hr = CompleteConnect(pReceivePin);
	if (hr < 0)
	{
		Disconnect();
		return hr;
	}

	return 0;
}
bool CGameClient::SetSignonState(int state, int spawncount)
{
	if ( state == SIGNONSTATE_CONNECTED )
	{
		if ( !CheckConnect() )
			return false;

		m_NetChannel->SetTimeout( SIGNON_TIME_OUT ); // allow 5 minutes to load map
		m_NetChannel->SetFileTransmissionMode( false );
		m_NetChannel->SetMaxBufferSize( true, NET_MAX_PAYLOAD );
	}
	else if ( state == SIGNONSTATE_NEW )
	{
		if ( !sv.IsMultiplayer() )
		{
			// local client as received and create string tables,
			// now link server tables to client tables
			SV_InstallClientStringTableMirrors();
		}
	}
	else if ( state == SIGNONSTATE_FULL )
	{
		if ( sv.m_bLoadgame )
		{
			// If this game was loaded from savegame, finish restoring game now
			sv.FinishRestore();
		}

		m_NetChannel->SetTimeout( sv_timeout.GetFloat() ); // use smaller timeout limit
		m_NetChannel->SetFileTransmissionMode( true );

#ifdef _XBOX
		// to save memory on the XBOX reduce reliable buffer size from 96 to 8 kB
		m_NetChannel->SetMaxBufferSize( true, 8*1024 );
#endif
	}

	return CBaseClient::SetSignonState( state, spawncount );
}
Exemple #7
0
static void on_accept(int sock,short event,void *arg)
{
    Proxyer* pserver = (Proxyer*) arg;
    int clientsocket = -1;
    int remotesocket = -1;
    Context* newcontext = NULL;
    int err = 0;
    struct sockaddr_in client_addr,remote_addr;
    socklen_t len = sizeof(client_addr);

    do {
        clientsocket = accept(sock,(struct sockaddr *)&client_addr,&len);
        if ( -1 == clientsocket ) {
            std::cerr<<"accept client failed! err: "<<strerror(errno)<<std::endl;
            break;
        }
        std::cout<<"receive connection "<<GetPeerInfo(&client_addr)<<std::endl;
        
        err = SetNonblocking(clientsocket);
        if ( 0 != err ) {
            std::cerr<<"set nonblocking for client socket failed! err: "<<strerror(err)<<std::endl;
            break;
        }

        remotesocket = socket(AF_INET,SOCK_STREAM,0);
        if ( -1 == remotesocket ) {
            std::cerr<<"create remote socket failed!"<<std::endl;
            break;
        }

        memset(&remote_addr,0,sizeof(remote_addr));
        remote_addr.sin_family = AF_INET;
        remote_addr.sin_port = htons(pserver->remote_port);
        remote_addr.sin_addr.s_addr = inet_addr(pserver->remote_addr.c_str());

        err = connect(remotesocket,(struct sockaddr*)&remote_addr,sizeof(remote_addr));
        if ( 0 != err ) {
            if ( EINPROGRESS != err ) {
                err = errno;
                std::cerr<<"connect remote failed! err: "<<strerror(err)<<std::endl;
                break;
            }
            err = CheckConnect(remotesocket);
            if ( 0 != err ) {
                err = errno;
                std::cerr<<"connect remote failed! err: "<<strerror(err)<<std::endl;
                break;
            }
            std::cout<<"connect remote success!"<<std::endl;
        } else {
            std::cout<<"connect remote success!"<<std::endl;
        }

        /**
         * connect remote success,then we need to create a Context
         * */

        newcontext = new Context(pserver,clientsocket,remotesocket);
        if ( NULL == newcontext ) {
            std::cerr<<"Alloc Context failed!"<<std::endl;
            break;
        }

        err = newcontext->Init();
        if ( 0 != err ) {
            std::cerr<<"newcontext init failed!"<<std::endl;
            break;
        }

        /**
         * 将context添加到server的队列中
         * */
        pserver->m_context_list.push_back(newcontext);
    } while(0);

    do{
        if ( 0 != err ) {
            if ( -1 == clientsocket ) {
                 break;
             }
            close(clientsocket);

            if ( -1 == remotesocket ) {
                break;
            }
            close(remotesocket);

            if ( NULL == newcontext ) {
                break;
            }
            delete newcontext;
       }
    } while(0);

}
Exemple #8
0
STDMETHODIMP
CBasePin::ReceiveConnection(
							IPin * pConnector,      // this is the pin who we will connect to
							const AM_MEDIA_TYPE *pmt    // this is the media type we will exchange
							)
{
	CheckPointer(pConnector,E_POINTER);
	CheckPointer(pmt,E_POINTER);
	ValidateReadPtr(pConnector,sizeof(IPin));
	ValidateReadPtr(pmt,sizeof(AM_MEDIA_TYPE));
	CComAutoLock cObjectLock(m_pLock);

#ifdef _DEBUG
	PIN_INFO PinInfo;

	if(!FAILED(pConnector->QueryPinInfo(&PinInfo)))
	{

		CEasyString PinName=PinInfo.achName;



		PrintSystemLog(0,"RecvPin:%s",(LPCTSTR)PinName);


		if(PinInfo.pFilter)
		{

			FILTER_INFO FilterInfo;
			if (!FAILED(PinInfo.pFilter->QueryFilterInfo(&FilterInfo)))
			{

				CLSID ClassID;

				PinInfo.pFilter->GetClassID(&ClassID);

				CEasyString FilterName=FilterInfo.achName;

				PrintSystemLog(0,"RecvPinFilter:%s",(LPCTSTR)FilterName);


				// The FILTER_INFO structure holds a pointer to the Filter Graph
				// Manager, with a reference count that must be released.
				if (FilterInfo.pGraph != NULL)
				{
					FilterInfo.pGraph->Release();
				}
			}

			PinInfo.pFilter->Release();
		}

		//MessageBox(NULL, (LPCTSTR)PinName, TEXT("Filter Name"), MB_OK);
	}

#endif

	/* Are we already connected */
	if (m_Connected) {
		return VFW_E_ALREADY_CONNECTED;
	}

	/* See if the filter is active */
	if (!IsStopped() && !m_bCanReconnectWhenActive) {
		return VFW_E_NOT_STOPPED;
	}

	HRESULT hr = CheckConnect(pConnector);
	if (FAILED(hr)) {
		// Since the procedure is already returning an error code, there
		// is nothing else this function can do to report the error.
		EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) );


		return hr;
	}

	/* Ask derived class if this media type is ok */

	CMediaType * pcmt = (CMediaType*) pmt;
	hr = CheckMediaType(pcmt);
	if (hr != NOERROR) {
		// no -we don't support this media type

		// Since the procedure is already returning an error code, there
		// is nothing else this function can do to report the error.
		EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) );

		// return a specific media type error if there is one
		// or map a general failure code to something more helpful
		// (in particular S_FALSE gets changed to an error code)
		if (SUCCEEDED(hr) ||
			(hr == E_FAIL) ||
			(hr == E_INVALIDARG)) {
				hr = VFW_E_TYPE_NOT_ACCEPTED;
		}


		return hr;
	}

	/* Complete the connection */

	m_Connected = pConnector;
	m_Connected->AddRef();
	hr = SetMediaType(pcmt);
	if (SUCCEEDED(hr)) {
		hr = CompleteConnect(pConnector);
		if (SUCCEEDED(hr)) {


			return NOERROR;
		}
	}

	DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("Failed to set the media type or failed to complete the connection.")));
	m_Connected->Release();
	m_Connected = NULL;

	// Since the procedure is already returning an error code, there
	// is nothing else this function can do to report the error.
	EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) );


	return hr;
}
Exemple #9
0
// given a specific media type, attempt a connection (includes
// checking that the type is acceptable to this pin)
HRESULT
CBasePin::AttemptConnection(
							IPin* pReceivePin,      // connect to this pin
							const CMediaType* pmt   // using this type
							)
{
	// The caller should hold the filter lock becasue this function
	// uses m_Connected.  The caller should also hold the filter lock
	// because this function calls SetMediaType(), IsStopped() and
	// CompleteConnect().
	ASSERT(CritCheckIn(m_pLock));

	// Check that the connection is valid  -- need to do this for every
	// connect attempt since BreakConnect will undo it.
	HRESULT hr = CheckConnect(pReceivePin);
	if (FAILED(hr)) {
		DbgLog((LOG_TRACE, CONNECT_TRACE_LEVEL, TEXT("CheckConnect failed")));

		// Since the procedure is already returning an error code, there
		// is nothing else this function can do to report the error.
		EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) );

		return hr;
	}

	DisplayTypeInfo(pReceivePin, pmt);

	/* Check we will accept this media type */

	hr = CheckMediaType(pmt);
	if (hr == NOERROR) {

		/*  Make ourselves look connected otherwise ReceiveConnection
		may not be able to complete the connection
		*/
		m_Connected = pReceivePin;
		m_Connected->AddRef();
		hr = SetMediaType(pmt);
		if (SUCCEEDED(hr)) {
			/* See if the other pin will accept this type */

			hr = pReceivePin->ReceiveConnection((IPin *)this, pmt);
			if (SUCCEEDED(hr)) {
				/* Complete the connection */

				hr = CompleteConnect(pReceivePin);
				if (SUCCEEDED(hr)) {
					return hr;
				} else {
					DbgLog((LOG_TRACE,
						CONNECT_TRACE_LEVEL,
						TEXT("Failed to complete connection")));
					pReceivePin->Disconnect();
				}
			}
		}
	} else {
		// we cannot use this media type

		// return a specific media type error if there is one
		// or map a general failure code to something more helpful
		// (in particular S_FALSE gets changed to an error code)
		if (SUCCEEDED(hr) ||
			(hr == E_FAIL) ||
			(hr == E_INVALIDARG)) {
				hr = VFW_E_TYPE_NOT_ACCEPTED;
		}
	}

	// BreakConnect and release any connection here in case CheckMediaType
	// failed, or if we set anything up during a call back during
	// ReceiveConnection.

	// Since the procedure is already returning an error code, there
	// is nothing else this function can do to report the error.
	EXECUTE_ASSERT( SUCCEEDED( BreakConnect() ) );

	/*  If failed then undo our state */
	if (m_Connected) {
		m_Connected->Release();
		m_Connected = NULL;
	}

	return hr;
}