///////////////////////////////////////////////////////////////////////
///  Function: XosWebRtcClientConnection::
///
///    Author: $author$
///      Date: 4/1/2012
///////////////////////////////////////////////////////////////////////
bool XosWebRtcClientConnection::SignOut(bool doClose) {
  if (state_ == NOT_CONNECTED || state_ == SIGNING_OUT)
    return true;

  if (hanging_get_->GetState() != talk_base::Socket::CS_CLOSED)
    hanging_get_->Close();

  if (control_socket_->GetState() == talk_base::Socket::CS_CLOSED) {
    state_ = SIGNING_OUT;

    if (my_id_ != -1) {
      char buffer[1024];
      talk_base::sprintfn(buffer, sizeof(buffer),
          "GET /sign_out?peer_id=%i HTTP/1.0\r\n\r\n", my_id_);
      onconnect_data_ = buffer;

      bool result = ConnectControlSocket(doClose);
      if ((doClose))
          Close();
      return result;
    } else {
      // Can occur if the app is closed before we finish connecting.
      return true;
    }
  } else {
    state_ = SIGNING_OUT_WAITING;
  }

  return true;
}
void XTcpClientImpl::DoConnect()
{
	m_nState = CONNECTTING;
	m_asynSock.reset(CreateClientSocket(m_svrSockAddr.ipaddr().family()));
	InitSocketSignals();

	bool ret = ConnectControlSocket();
	if (!ret) {
#ifdef WEBRTC_ANDROID
        LOGI("XTcpClientImpl::DoConnect false, try to reconnect after 3s...\n");
#else
        LOG(WARNING) << "XTcpClientImpl::DoConnect false, try to reconnect after 3s...";
#endif
		m_rCallback.OnServerConnectionFailure();
        CurThread()->PostDelayed(kTryReconnectDelay, this, 1001);
    }
}
///////////////////////////////////////////////////////////////////////
///  Function: XosWebRtcClientConnection::
///
///    Author: $author$
///      Date: 4/1/2012
///////////////////////////////////////////////////////////////////////
bool XosWebRtcClientConnection::Connect(const std::string& server, int port,
                                   const std::string& client_name) {
  ASSERT(!server.empty());
  ASSERT(!client_name.empty());

  if (state_ != NOT_CONNECTED) {
    LOG(WARNING)
        << "The client must not be connected before you can call Connect()";
    return false;
  }

  if (server.empty() || client_name.empty())
    return false;

  if (port <= 0)
    port = default_server_port_;

  server_address_.SetIP(server);
  server_address_.SetPort(port);

  if (server_address_.IsUnresolved()) {
    int errcode = 0;
    hostent* h = talk_base::SafeGetHostByName(
          server_address_.IPAsString().c_str(), &errcode);
    if (!h) {
      LOG(LS_ERROR) << "Failed to resolve host name: "
                    << server_address_.IPAsString();
      return false;
    } else {
      server_address_.SetResolvedIP(
          ntohl(*reinterpret_cast<uint32*>(h->h_addr_list[0])));
      talk_base::FreeHostEnt(h);
    }
  }

  char buffer[1024];
  talk_base::sprintfn(buffer, sizeof(buffer),
           "GET /sign_in?%s HTTP/1.0\r\n\r\n", client_name.c_str());
  onconnect_data_ = buffer;

  bool ret = ConnectControlSocket();
  if (ret)
    state_ = SIGNING_IN;

  return ret;
}
Esempio n. 4
0
//This function does all the work -- connects on a blocking socket
//then sends the appropriate user and password commands
//and then the cwd command, the port command then get and finally the quit
void CFtpGet::WorkerThread()
{
	ConnectControlSocket();
	if(m_State != FTP_STATE_LOGGING_IN)
	{
		return;
	}
	LoginHost();
	if(m_State != FTP_STATE_LOGGED_IN)
	{
		return;
	}
	GetFile();

	//We are all done now, and state has the current state.
	m_Aborted = true;
	

}
///////////////////////////////////////////////////////////////////////
///  Function: XosWebRtcClientConnection::
///
///    Author: $author$
///      Date: 4/1/2012
///////////////////////////////////////////////////////////////////////
bool XosWebRtcClientConnection::SendToPeer
(int peer_id, const std::string& message, bool doingClose) {
  if (state_ != CONNECTED)
    return false;

  ASSERT(is_connected());
  ASSERT(control_socket_->GetState() == talk_base::Socket::CS_CLOSED);
  if (!is_connected() || peer_id == -1)
    return false;

  char headers[1024];
  talk_base::sprintfn(headers, sizeof(headers),
      "POST /message?peer_id=%i&to=%i HTTP/1.0\r\n"
      "Content-Length: %i\r\n"
      "Content-Type: text/plain\r\n"
      "\r\n",
      my_id_, peer_id, message.length());
  onconnect_data_ = headers;
  onconnect_data_ += message;

  return ConnectControlSocket(doingClose);
}