示例#1
0
//--------------------------------------------------------------------------------------------------
void le_msg_AdvertiseService
(
    le_msg_ServiceRef_t serviceRef  ///< [in] Reference to the service.
)
//--------------------------------------------------------------------------------------------------
{
    // Open a socket and connect it to the Service Directory.
    int fd = unixSocket_CreateSeqPacketUnnamed();

    // Check for failure.
    LE_FATAL_IF(fd == LE_NOT_PERMITTED, "Permission to open socket denied.");
    LE_FATAL_IF(fd == LE_FAULT, "Failed to open socket.");

    // Warn if one of the three standard I/O streams have been somehow connected to the
    // Service Directory.
    if (fd < 3)
    {
        const char* streamNameStr;
        switch (fd)
        {
            case 0:
                streamNameStr = "stdin";
                break;
            case 1:
                streamNameStr = "stdout";
                break;
            case 2:
                streamNameStr = "stderr";
                break;
        }
        LE_WARN("Service Directory connection mapped to %s.", streamNameStr);
    }

    le_result_t result = unixSocket_Connect(fd, LE_SVCDIR_SERVER_SOCKET_NAME);
    LE_FATAL_IF(result != LE_OK,
                "Failed to connect to Service Directory. Result = %d (%s).",
                result,
                LE_RESULT_TXT(result));

    serviceRef->directorySocketFd = fd;

    // Set the socket non-blocking.
    fd_SetNonBlocking(fd);

    // Start monitoring the socket for events.
    StartMonitoringDirectorySocket(serviceRef);

    // Send the Service ID to the Service Directory.
    msgService_SendServiceId(serviceRef, fd);

    // Wait for the Service Directory to respond by either dropping the connection
    // (meaning that we have been denied permission to offer this service) or by
    // forwarding us file descriptors for authenticated client connections.
}
示例#2
0
//--------------------------------------------------------------------------------------------------
static void DirectorySocketWriteable
(
    int fd
)
//--------------------------------------------------------------------------------------------------
{
    Service_t* servicePtr = le_event_GetContextPtr();

    if (servicePtr->state == LE_MSG_SERVICE_CONNECTING)
    {
        // Must have connected (or failed to do so).
        int errCode = unixSocket_GetErrorState(servicePtr->directorySocketFd);

        // Disable writeability notification.
        le_event_ClearFdHandlerByEventType(servicePtr->fdMonitorRef, LE_EVENT_FD_WRITEABLE);

        // If connection successful,
        if (errCode == 0)
        {
            // Send the Service ID to the Service Directory.
            msgService_SendServiceId(servicePtr, fd);

            servicePtr->state = LE_MSG_SERVICE_ADVERTISED;

            // Wait for the Service Directory to respond by either dropping the connection
            // (meaning that we have been denied permission to offer this service) or by
            // forwarding us file descriptors for authenticated client connections.
        }
        // If connection failed,
        else
        {
            LE_FATAL("Failed to connect to Service Directory. SO_ERROR %d (%s).",
                     errCode,
                     strerror(errCode));
        }
    }
    else
    {
        LE_CRIT("Unexpected writeability notification in state %d.", servicePtr->state);
    }
}