Пример #1
0
OP_STATUS 
WebResource::OnConnectionClosedBase()
{
	OP_ASSERT(m_service != NULL);
	m_service = NULL;
	OP_STATUS status = OpStatus::OK;
	OP_STATUS status2;
	OP_STATUS temp_status;
	if (m_HttpConnectionListeners.Cardinal() > 0)	
	{		
		for (Webserver_HttpConnectionListener * listener = (Webserver_HttpConnectionListener *) m_HttpConnectionListeners.First(); listener; listener = (Webserver_HttpConnectionListener *) listener->Suc()) 
		{
			   /*We signal the ecmascript listeners (mainly  DOM_WebserverEcmaSendObject) that the connection has been closed.
			 * This will cause any ongoing blocking function (like flush and write) to return with and exception)
			 */ 

			if (OpStatus::IsError(temp_status = listener->WebServerConnectionKilled()))
			{
				status = temp_status;
			}
		}
	}
	
	RETURN_IF_MEMORY_ERROR(status2 = OnConnectionClosed());
	RETURN_IF_MEMORY_ERROR(status);
	return status2;
}
Пример #2
0
void NET_Server::PrivateStopListen()
{
	if (!m_created || !m_listening)
		return;

	LOG_V0_AddMessage(LOG_V0_DEBUG_MESSAGE, GetName() + "::PrivateStopListen() started");

	CHECK(m_socket != INVALID_SOCKET);
	SelectEvent(m_wait_thread.Event(WT_ACCEPTED), 0);
	{
		MutexWrap connections_access(m_connections_mutex);
		while (!m_connections.empty())
		{
			NET_Connection* connection = m_connections.back();
			connection->m_read_thread.Stop();
			connection->m_write_thread.Stop();
			connection->m_server = NULL;
			m_connections.pop_back();
			connection->PrivateClose();

			OnConnectionClosed(connection);
			delete connection;
		}
	}
	m_wait_thread.Stop();
	OP2( shutdown(m_socket, SD_BOTH), m_last_error != WSAENOTCONN, "NET_Server::PrivateStopListen, shutdown" );
	OP( closesocket(m_socket), "NET_Server::PrivateStopListen, closesocket" );
	m_socket = INVALID_SOCKET;
	m_listening = false;
	m_listening_port = -1;
	m_created = false;

	LOG_V0_AddMessage(LOG_V0_DEBUG_MESSAGE, GetName() + "::PrivateStopListen() finished");
}
Пример #3
0
void SessionHandler::on_readable(int notifyingSocket)
{
   if (notifyingSocket != m_fd)
   {
      return;
   }

   auto bytesReceived = recv(m_fd, m_readBuffer + m_bytesRead,
                          m_bufferSize - m_bytesRead, 0);
   if (bytesReceived == -1)
   {
      m_tracer.trace0("receive unexpectedly failed");
      return;
   }

   if (bytesReceived == 0)
   {
      m_tracer.trace0("Connection to %d closed", m_fd);

      OnConnectionClosed();

      WatchStream(m_fd, false);
      return;
   }

   m_bytesRead += bytesReceived;
   DrainReadBuffer();
}
Пример #4
0
bool SampleSocketPort::CloseSocket(void)
{
	if(m_bOpen && m_bDoDisconnect)
	{									//This is where the disconnection really occurs
		m_bOpen = false;				//If m_bDoDisconnect == true we know this has been called 
		OnConnectionClosed();			//through the timer, so 'delete this' is safe!
		delete this;
	}
	else if(m_bOpen)
	{
		m_bDoDisconnect = true;			//Just set the timer and the flag so we can 
		setTimer(DISCONNECT_MS);		//disconnect safely, in DISCONNECT_MS
	}
	return(true);
}
Пример #5
0
bool NetworkConnection::openConnection(const QString & host, const unsigned short port, bool not_main_connection)
{	
	qsocket = new QTcpSocket();	//try with no parent passed for now
	if(!qsocket)
		return 0;
	//connect signals
	
	//connect(qsocket, SIGNAL(hostFound()), SLOT(OnHostFound()));
	connect(qsocket, SIGNAL(connected()), SLOT(OnConnected()));
	connect(qsocket, SIGNAL(readyRead()), SLOT(OnReadyRead()));
	connect(qsocket, SIGNAL(disconnected ()), SLOT(OnConnectionClosed()));
//	connect(qsocket, SIGNAL(delayedCloseFinished()), SLOT(OnDelayedCloseFinish()));
//	connect(qsocket, SIGNAL(bytesWritten(qint64)), SLOT(OnBytesWritten(qint64)));
    connect(qsocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(OnError(QAbstractSocket::SocketError)));

	if(qsocket->state() != QTcpSocket::UnconnectedState)
	{
		qDebug("Called openConnection while in state %d", qsocket->isValid());
		return 0;
	}
	//remove asserts later
	Q_ASSERT(host != 0);
	Q_ASSERT(port != 0);

	if(!not_main_connection)
		drawPleaseWait();

	qDebug("Connecting to %s %d...\n", host.toLatin1().constData(), port);
	// assume info.host is clean
	qsocket->connectToHost(host, (quint16) port);
	
	/* If dispatch does not have a UI, the thing that sets the UI
	 * will setupRoomAndConsole */
	/* Tricky now without dispatches... who sets up the UI?
	 * there's always a mainwindow... but maybe things aren't setup? */
	
	/* connectionInfo as a message with those pointers is probably a bad idea */
	return (qsocket->state() != QTcpSocket::UnconnectedState);
}
Пример #6
0
int SessionHandler::SendData(const char* buffer, size_t length)
{
    auto bytesWritten = write(m_fd, buffer, length);

    if (bytesWritten == -1)
    {
        switch (errno)
        {
            case EAGAIN:
                m_tracer.trace5("EAGAIN when trying %d bytes to socket %d", length, m_fd);
                watch_writable(m_fd, true);
                return length;

            case ECONNRESET:
                m_tracer.trace0("connection closed while trying to write %d bytes to %d",
                                length, m_fd);

                OnConnectionClosed();
                WatchStream(m_fd, false);
                return 0;

            default:
                m_tracer.trace0("unexpected error code from send(): %d", errno);
                break;
        }
    }
    else if ((size_t)bytesWritten < length)
    {
        watch_writable(m_fd, true);
    }
    else
    {
        watch_writable(m_fd, false);
    }

    return length - bytesWritten;
}
Пример #7
0
void NET_Server::PrivateOnConnectionClosed(NET_Connection* connection)
{
	CString connection_name = connection->GetName();
	LOG_V0_AddMessage(LOG_V0_DEBUG_MESSAGE, GetName() + (TXT("::PrivateOnConnectionClosed(%s) started") << connection_name));

	list<NET_Connection*>::iterator current = m_connections.begin();
	while (current != m_connections.end())
	{
		if (*current == connection)
		{
			connection->m_read_thread.Stop();
			connection->m_write_thread.Stop();
			connection->m_server = NULL;
			m_connections.erase(current);
			connection->PrivateClose();

			OnConnectionClosed(connection);
			delete connection;
			break;
		}
		current++;
	}
	LOG_V0_AddMessage(LOG_V0_DEBUG_MESSAGE, GetName() + (TXT("::PrivateOnConnectionClosed(%s) finished") << connection_name));
}