Exemple #1
0
int SecureSocket::DoPhysicalConnect(const struct sockaddr* name, int namelen, int& timeout)
{
	const DWORD start = GetTickCount();
	const int result = ::connect(m_socket, name, namelen);
	if (SOCKET_ERROR == result)
	{
		return SOCKET_ERROR;
	}

	const DWORD finish = GetTickCount();
	if (0 == result)
	{
		EnableKeepAlive(m_socket);
	}
	if (finish > start)
	{
		timeout = finish - start;
	}
	else
	{
		timeout = finish + (std::numeric_limits<DWORD>::max() - start) + 1;
	}
	timeout = std::min<DWORD>(1000 + 4 * timeout, 16000);
	return result;
}
void CTcpSocketService::CloseOnProblem()
{
    //call this function internally in order to close the socket in case of error situation
    EnableKeepAlive(false);
    Close();//CBaseBerkeleySocket::Close();

    if(m_Sink)
        m_Sink->OnClose();
    else
        LogEvent(LE_ERROR, "CTcpSocketService::OnBaseSocketClose: m_Sink=NULL");
}
Exemple #3
0
bool CLrpStClientImpl::Connect(uint32 timeoutInMilliseconds)
{
	if (INVALID_SOCKET != m_socket)
	{
		m_logger.Output("Closing socket");
		closesocket(m_socket);
		m_socket = INVALID_SOCKET;
		m_logger.Output("Socket has been closed");
	}
	m_logger.Output("Creating a new socket");
	SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == s)
	{
		m_logger.Output("Couldn't create a new socket: WSAGetLastError() = ", WSAGetLastError());
		return false;
	}
	CSocketGuard guard(s);
	m_logger.Output("New socket has been created");

	m_logger.Output("Resolving connection string");
	sockaddr addr;
	ZeroMemory(&addr, sizeof(addr));
	if (!ResolveConnectionString(m_address, m_port, addr))
	{
		m_logger.Output("Could not resolve connection string: WSAGetLastError() = ", WSAGetLastError());
	}
	m_logger.Output("Connection string has been resolved");

	m_logger.Output("Connectiong to {0}:{1}", m_address, m_port);

	const int status = connect(s, &addr, sizeof(addr));
	if (SOCKET_ERROR == status)
	{
		m_logger.Output("Could not establish new connection: WSAGetLastError() = {0}", WSAGetLastError());
		return false;
	}
	m_logger.Output("New connection has been established");
	m_logger.Output("Enabling keep alive");
	EnableKeepAlive(s);
	CTimeout timeout(timeoutInMilliseconds);
	// send protocol version
	m_logger.Output("Sending protocol version = {0}", cInitialProtocolVersion);
	if (!SendEx(s, timeout, &cInitialProtocolVersion, sizeof(cInitialProtocolVersion)))
	{
		m_logger.Output("Could not send protocol version");
		return false;
	}
	m_logger.Output("Protocol version has been sent");
	HRESULT answer = S_OK;
	m_logger.Output("Receiving server protocol answer");
	if (!ReceiveEx(s, timeout, &answer, sizeof(answer)))
	{
		m_logger.Output("Could not receive server protocol answer");
		return false;
	}
	m_logger.Output("Server protocol answer: code = {0}; status = {1}", answer, SUCCEEDED(answer));
	if (FAILED(answer))
	{
		return false;
	}
	m_logger.Output("Sending username = {0}", m_username);
	if (!SendEx(s, timeout, m_username))
	{
		m_logger.Output("Could not send username");
		return false;
	}
	m_logger.Output("Username has been sent");
	m_logger.Output("Sending password = {0}", m_password);
	if (!SendEx(s, timeout, m_password))
	{
		m_logger.Output("Could not send password");
		return false;
	}
	m_logger.Output("Password has been sent");
	answer = S_OK;
	m_logger.Output("Receiving server authorization answer");
	if(!ReceiveEx(s, timeout, &answer, sizeof(answer)))
	{
		m_logger.Output("Could not receive server authorization answer");
		return false;
	}
	m_logger.Output("Server authorization answer: code = {0}; status = {1}", answer, SUCCEEDED(answer));
	if (FAILED(answer))
	{
		return false;
	}
	m_logger.Output("Receiving remote signature");
	string remoteSignature;
	if (!ReceiveEx(s, timeout, remoteSignature))
	{
		m_logger.Output("Could not receive remote signature");
		return false;
	}
	m_logger.Output("Remote signature has been received = {0}", remoteSignature);
	m_logger.Output("Initializing translators");
	Initialize(remoteSignature);
	m_logger.Output("Translators have been initialized");
	m_socket = guard.Release();
	FlushTranlators(m_logger);
	return true;
}