Esempio n. 1
0
void CBaseSocket::_AcceptNewSocket()
{
    SOCKET fd = 0;
    sockaddr_in peer_addr;
    socklen_t addr_len = sizeof(sockaddr_in);
    char ip_str[64];
    
    while ( (fd = accept(m_socket, (sockaddr*)&peer_addr, &addr_len)) != INVALID_SOCKET )
    {
        CBaseSocket* pSocket = new CBaseSocket();
        
        uint32_t ip = ntohl(peer_addr.sin_addr.s_addr);
        uint16_t port = ntohs(peer_addr.sin_port);
        
        snprintf(ip_str, sizeof(ip_str), "%d.%d.%d.%d", ip >> 24, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF);
        
//        log("AcceptNewSocket, socket=%d from %s:%d\n", fd, ip_str, port);
        
        pSocket->SetSocket(fd);
        pSocket->SetCallback(m_callback);
        pSocket->SetCallbackData(m_callback_data);
        pSocket->SetState(SOCKET_STATE_CONNECTED);
        pSocket->SetRemoteIP(ip_str);
        pSocket->SetRemotePort(port);
        
        _SetNoDelay(fd);
        _SetNonblock(fd);
        AddBaseSocket(pSocket);
        CEventDispatch::Instance()->AddEvent(fd, SOCKET_READ | SOCKET_EXCEP);
        m_callback(m_callback_data, NETLIB_MSG_CONNECT, (net_handle_t)fd, NULL);
    }
}
Esempio n. 2
0
int netlib_option(net_handle_t handle, int opt, void* optval)
{
	CBaseSocket* pSocket = FindBaseSocket(handle);
	if (!pSocket)
		return NETLIB_ERROR;

	if ((opt >= NETLIB_OPT_GET_REMOTE_IP) && !optval)
		return NETLIB_ERROR;

	switch (opt)
	{
	case NETLIB_OPT_SET_CALLBACK:
		pSocket->SetCallback((callback_t)optval);
		break;
	case NETLIB_OPT_SET_CALLBACK_DATA:
		pSocket->SetCallbackData(optval);
		break;
	case NETLIB_OPT_GET_REMOTE_IP:
		*(string*)optval = pSocket->GetRemoteIP();
		break;
	case NETLIB_OPT_GET_REMOTE_PORT:
		*(uint16_t*)optval = pSocket->GetRemotePort();
		break;
	case NETLIB_OPT_GET_LOCAL_IP:
		*(string*)optval = pSocket->GetLocalIP();
		break;
	case NETLIB_OPT_GET_LOCAL_PORT:
		*(uint16_t*)optval = pSocket->GetLocalPort();
	}

	pSocket->ReleaseRef();
	return NETLIB_OK;
}
Esempio n. 3
0
int netlib_option( net_handle_t handel, int opt, void *optval)
{
    CBaseSocket *pSocket = FindBaseSocket(handel);
    if( NULL == pSocket )
    {
        return NETLIB_ERROR;
    }
    
    if((opt >= NETLIB_OPT_GET_REMOTE_IP) && !optval )
    {
        return NETLIB_ERROR;
    }
    
    switch (opt) {
        case NETLIB_OPT_SET_CALLBACK:
            pSocket->SetCallback((callback_t)optval);
            break;
        case NETLIB_OPT_SET_CALLBACK_DATA:
            pSocket->SetCallbackData(optval);
            break;
        case NETLIB_OPT_GET_REMOTE_IP:
            *(string*)optval = pSocket->GetRemoteIP();
            break;
        case NETLIB_OPT_GET_REMOTE_PORT:
            *(string*)optval = pSocket->GetRemotePort();
            break;
        case NETLIB_OPT_GET_LOCAL_IP:
            *(string*)optval = pSocket->GetLocalIP();
            break;
        case NETLIB_OPT_GET_LOCAL_PORT:
            *(string*)optval = pSocket->GetLocalPort();
            break;
        case NETLIB_OPT_SET_SEND_BUF_SIZE:
            pSocket->SetSendBufSize(*(uint32_t*)optval);
            break;
        case NETLIB_OPT_SET_RECV_BUF_SIZE:
            pSocket->SetRecvBufSize(*(uint32_t*)optval);
            break;
    }
    pSocket->ReleaseRef();
    return NETLIB_OK;
}