Esempio n. 1
0
int doAccept (int efd, int sfd)
{
	int cfd;
	struct sockaddr_in cliAddr;
	socklen_t cliLen = sizeof (cliAddr);

	bzero (&cliAddr, sizeof (cliAddr));

	cfd = accept (sfd, (struct sockaddr *)&cliAddr, &cliLen);
	if (cfd < 0){
		perror ("doAccept, accept error");
		return -1;
	}
	printf("\naccept ok fd(%d)\n", cfd);
	if(setNonBlock (cfd)==-1) {
		perror ("doAccept, setNonBlock error");
		return -1;
	}
	if(setTcpNoDelay (cfd)==-1) {
		perror ("doAccept, setTcpNoDelay error");
		return -1;
	}

	epoll_in_add (efd, cfd);
	return cfd;
}
void EAbstractSocketSessionConfig::doSetAll(EIoSessionConfig* config) {
	ESocketSessionConfig* ssc = dynamic_cast<ESocketSessionConfig*>(config);
	if (!ssc) {
		return;
	}

	EAbstractSocketSessionConfig* cfg = dynamic_cast<EAbstractSocketSessionConfig*>(config);
	if (cfg) {
        // Minimize unnecessary system calls by checking all 'propertyChanged' properties.
        if (cfg->isKeepAliveChanged()) {
            setKeepAlive(cfg->isKeepAlive());
        }
        if (cfg->isOobInlineChanged()) {
            setOobInline(cfg->isOobInline());
        }
        if (cfg->isReceiveBufferSizeChanged()) {
            setReceiveBufferSize(cfg->getReceiveBufferSize());
        }
        if (cfg->isReuseAddressChanged()) {
            setReuseAddress(cfg->isReuseAddress());
        }
        if (cfg->isSendBufferSizeChanged()) {
            setSendBufferSize(cfg->getSendBufferSize());
        }
        if (cfg->isSoLingerChanged()) {
            setSoLinger(cfg->getSoLinger());
        }
        if (cfg->isTcpNoDelayChanged()) {
            setTcpNoDelay(cfg->isTcpNoDelay());
        }
        if (cfg->isTrafficClassChanged() && getTrafficClass() != cfg->getTrafficClass()) {
            setTrafficClass(cfg->getTrafficClass());
        }
    } else {
        ESocketSessionConfig* cfg = dynamic_cast<ESocketSessionConfig*>(config);
        setKeepAlive(cfg->isKeepAlive());
        setOobInline(cfg->isOobInline());
        setReceiveBufferSize(cfg->getReceiveBufferSize());
        setReuseAddress(cfg->isReuseAddress());
        setSendBufferSize(cfg->getSendBufferSize());
        setSoLinger(cfg->getSoLinger());
        setTcpNoDelay(cfg->isTcpNoDelay());
        if (getTrafficClass() != cfg->getTrafficClass()) {
            setTrafficClass(cfg->getTrafficClass());
        }
    }
}
Esempio n. 3
0
extern "C" JNIEXPORT void JNICALL
Java_java_nio_channels_SocketChannel_natSetTcpNoDelay(JNIEnv *e,
                                                      jclass,
                                                      jint socket,
                                                      jboolean on)
{
  setTcpNoDelay(e, socket, on);
}
Esempio n. 4
0
void Socket::createSocket(const SocketAddress& sa) const
{
    SOCKET& socket = impl->fd;
    if (socket != INVALID_SOCKET) Socket::close();

    SOCKET s = ::socket (getAddrInfo(sa).ai_family,
                         getAddrInfo(sa).ai_socktype,
                         0);
    if (s == INVALID_SOCKET) throw QPID_WINDOWS_ERROR(WSAGetLastError());
    socket = s;

    try {
        if (nonblocking) setNonblocking();
        if (nodelay) setTcpNoDelay();
    } catch (std::exception&) {
        ::closesocket(s);
        socket = INVALID_SOCKET;
        throw;
    }
}
Esempio n. 5
0
bool serverSocket::listen() {
	if (!checkSocketHandle()) {
		return false;	
	}

	setSoLinger(false,0);
	setReuseAddress(true);
	setIntOption(SO_KEEPALIVE,1);	
	setIntOption(SO_SNDBUF,640000);	
	setIntOption(SO_RCVBUF,640000);
	setTcpNoDelay(true);

	if(::bind(_socketHandle,(struct sockaddr *) &_address,sizeof(_address)) <0) {
		return false;	
	}

	if(::listen(_socketHandle,backLog) <0) {
		return false;	
	}

	return true;
}
Esempio n. 6
0
/*
 * Read bytes from the server
 */
bool VNCSockets::ReadFromRFBServer(char *out, uint32_t n)
{
    if (n <= buffered)
    {
        memcpy(out, bufoutptr, n);
        bufoutptr += n;
        buffered -= n;
        return true;
    }
    memcpy(out, bufoutptr, buffered);

    out += buffered;
    n -= buffered;
    bufoutptr = buf;
    buffered = 0;

    if (n <= sizeof(buf))
    {
        while (buffered < n)
        {

#ifdef _WIN32
            int i = recv(rfbsock, (char *)buf + buffered, sizeof(buf) - buffered, 0);
#else
            int i = read(rfbsock, buf + buffered, sizeof(buf) - buffered);
#endif
            if (i <= 0)
            {
                if (i < 0)
                {
#ifdef _WIN32
                    if (errno == EAGAIN || (errno == EINTR))
                    {
#else
                    if (errno == EWOULDBLOCK || errno == EAGAIN || (errno == EINTR))
                    {
#endif
                        i = 0;
                    }
                    else
                    {
                        fprintf(stderr, "VNCSockets::ReadFromRFBServer(): read\n");
                        return false;
                    }
                }
                else
                {
                    fprintf(stderr, "VNCSockets::ReadFromRFBServer(): VNC server closed connection\n");
                    return false;
                }
            }
            buffered += i;
        }
        memcpy(out, bufoutptr, n);
        bufoutptr += n;
        buffered -= n;
        return true;
    }
    else
    {
        while (n > 0)
        {
#ifdef _WIN32
            int i = recv(rfbsock, out, n, 0);
#else
            int i = read(rfbsock, out, n);
#endif
            if (i <= 0)
            {
                if (i < 0)
                {
#ifdef _WIN32
                    if (errno == EAGAIN || (errno == EINTR))
                    {
#else
                    if (errno == EWOULDBLOCK || errno == EAGAIN || (errno == EINTR))
                    {
#endif
                        i = 0;
                    }
                    else
                    {
                        fprintf(stderr, "VNCSockets::ReadFromRFBServer(): read\n");
                        return false;
                    }
                }
                else
                {
                    fprintf(stderr, "VNCSockets::ReadFromRFBServer(): VNC server closed connection\n");
                    return false;
                }
            }
            out += i;
            n -= i;
        }
        return true;
    }
}

/*
 * Write an exact number of bytes, and don't return until you've sent them.
 */
bool VNCSockets::WriteExact(char *buf, int n)
{
    for (int i = 0; i < n;)
    {
#ifdef _WIN32
        int j = send(rfbsock, buf + i, (n - i), 0);
#else
        int j = write(rfbsock, buf + i, (n - i));
#endif

        if (j <= 0)
        {
            if (j < 0)
            {
#ifdef _WIN32
                if (errno == EAGAIN || (errno == EINTR))
                {
#else
                if (errno == EWOULDBLOCK || errno == EAGAIN || (errno == EINTR))
                {
#endif
                    fd_set fds;
                    FD_ZERO(&fds);
                    FD_SET(rfbsock, &fds);
                    if (select(rfbsock + 1, NULL, &fds, NULL, NULL) <= 0)
                    {
                        fprintf(stderr, "VNCSockets::WriteExact(): select\n");
                        return false;
                    }
                    j = 0;
                }
                else
                {
                    fprintf(stderr, "VNCSockets::WriteExact(): write\n");
                    return false;
                }
            }
            else
            {
                fprintf(stderr, "VNCSockets::WriteExact() err: write failed\n");
                return false;
            }
        }
        i += j;
    }
    return true;
}

/*
 * ConnectToTcpAddr connects to the given TCP port.
 */
int VNCSockets::ConnectToTcpAddr()
{
    if ((rfbsock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        fprintf(stderr, "VNCSockets::ConnectToTcpAddr() err: socket\n");
        return -1;
    }

    struct sockaddr_in sa;

    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);
    sa.sin_addr.s_addr = htonl(ipaddr);

    fprintf(stderr, "VNCSockets::ConnectToTcpAddr(): connecting to %s:%i %x\n",
            ServerName, ntohs(sa.sin_port), ntohl(sa.sin_addr.s_addr));

    if (connect(rfbsock, (const struct sockaddr *)&sa, sizeof(sa)) < 0)
    {
        perror("VNC: connect");
        fprintf(stderr, "VNCSockets::ConnectToTcpAddr() err: %s (%d) sock=%d\n", strerror(errno), errno, rfbsock);
        closesocket(rfbsock);
        return -1;
    }
    if (setTcpNoDelay(rfbsock) < 0)
    {
        fprintf(stderr, "VNCSockets::ConnectToTcpAddr() err: TCP_NODELAY %s (%d)\n", strerror(errno), errno);
        //pd close(rfbsock);
        //pd return -1;
    }
    return rfbsock;
}

/*
 * SetNonBlocking sets a socket into non-blocking mode.
 */
bool VNCSockets::SetNonBlocking()
{
    if (setNoBlocking(rfbsock) < 0)
        return false;
    return true;
}

/*
 * StringToIPAddr - convert a host string to an IP address.
 */
bool VNCSockets::StringToIPAddr()
{
    struct hostent *hp;

    if ((hp = my_gethostbyname(ServerName, AF_INET)) != NULL)
    {
        memcpy(&ipaddr, hp->h_addr, hp->h_length);
        ipaddr = ntohl(ipaddr);
        fprintf(stderr, "VNCSockets::StringToIPAddr(): ServerName=%s (%x)\n", ServerName, ipaddr);
        return true;
    }
    return false;
}

/*
 * Test if the other end of a socket is on the same machine.
 */
bool VNCSockets::SameMachine()
{
    struct sockaddr_in peersa, mysa;
#ifdef WIN32
    int slen = sizeof(struct sockaddr_in);
#else
    socklen_t slen = sizeof(struct sockaddr_in);
#endif

    getpeername(rfbsock, (struct sockaddr *)&peersa, &slen);
    getsockname(rfbsock, (struct sockaddr *)&mysa, &slen);

    return (peersa.sin_addr.s_addr == mysa.sin_addr.s_addr);
}

#if 0 //unused
/*
 * Print out the contents of a packet for debugging.
 */
void VNCSockets::PrintInHex(char *buf, int len)
{
  int i;
  char c, str[17];

  str[16] = 0;

  trace(DBG_VNC, "ReadExact: ");

  for (i = 0; i < len; i++) {
    if ((i % 16 == 0) && (i != 0))
      fprintf(stderr, "           ");
    c = buf[i];
    str[i % 16] = (((c > 31) && (c < 127)) ? c : '.');
    fprintf(stderr,"%02x ", (uint8_t) c);
    if ((i % 4) == 3)
      fprintf(stderr, " ");
    if ((i % 16) == 15)
      fprintf(stderr,"%s\n", str);
  }
  if ((i % 16) != 0) {
    for (int j = i % 16; j < 16; j++) {
      fprintf(stderr, "   ");
      if ((j % 4) == 3) fprintf(stderr, " ");
    }
    str[i % 16] = 0;
    fprintf(stderr,"%s\n", str);
  }
  fflush(stderr);
}
#endif //unused

/*
 * Returns the socket used
 */
int VNCSockets::GetSock()
{
    return rfbsock;
}
Esempio n. 7
0
ULXR_API_IMPL(void) TcpIpConnection::init(unsigned prt)
{
  ULXR_TRACE(ULXR_PCHAR("init"));
#if defined(__WIN32__)  && !defined (ULXR_NO_WSA_STARTUP)
  WORD wVersionRequested;
  WSADATA wsaData;
  wVersionRequested = MAKEWORD( 2, 0 );

  if (WSAStartup( wVersionRequested, &wsaData) != 0)
    throw ConnectionException(SystemError,
                              ulxr_i18n(ULXR_PCHAR("Could not initialize Windows sockets: "))
                                + ULXR_GET_STRING(getErrorString(getLastError())), 500);
#endif

  pimpl->server_data = 0;
  setTcpNoDelay(false);
  pimpl->serverdomain = ULXR_PCHAR("");
  pimpl->remote_name = ULXR_PCHAR("");
  setTimeout(10);
  pimpl->port = prt;
  pimpl->hostdata_len = sizeof(pimpl->hostdata);
  pimpl->remotedata_len = sizeof(pimpl->remotedata);
  memset(&pimpl->hostdata, 0, sizeof(pimpl->hostdata));
  memset(&pimpl->remotedata, 0, sizeof(pimpl->remotedata));
  pimpl->hostdata.sin_port = htons(pimpl->port);
  pimpl->hostdata.sin_family = AF_INET;

  char buffer [1000];
  memset(buffer, 0, sizeof(buffer));
  int ret = gethostname(buffer, sizeof(buffer)-1);
  if (ret != 0)
    throw ConnectionException(SystemError,
                              ulxr_i18n(ULXR_PCHAR("Could not get host name: "))
                                   + ULXR_GET_STRING(getErrorString(getLastError())), 500);

  pimpl->host_name = ULXR_GET_STRING(buffer);

#if defined(__SUN__)

  long status = sysinfo(SI_SRPC_DOMAIN ,buffer, sizeof(buffer)-1);
  if (status == -1)
    throw ConnectionException(SystemError,
                              ulxr_i18n(ULXR_PCHAR("Could not get domain name: "))
                                   + ULXR_GET_STRING(getErrorString(getLastError())), 500);

  if (buffer[0] != 0)
  {
    pimpl->host_name += ULXR_PCHAR(".");
    pimpl->host_name += ULXR_GET_STRING(buffer);
  }

#elif defined(__unix__) || defined(__CYGWIN__)

  ret = getdomainname(buffer, sizeof(buffer)-1);
  if (ret != 0)
    throw ConnectionException(SystemError,
                              ulxr_i18n(ULXR_PCHAR("Could not get domain name: "))
                                   + ULXR_GET_STRING(getErrorString(getLastError())), 500);

  if (buffer[0] != 0)
  {
    pimpl->host_name += ULXR_PCHAR(".");
    pimpl->host_name += ULXR_GET_STRING(buffer);
  }

#elif _WIN32

#ifndef ULXR_OMIT_REENTRANT_PROTECTOR
  Mutex::Locker lock(gethostbynameMutex);
#endif

  struct hostent *hostEntPtr = gethostbyname(getLatin1(pimpl->host_name).c_str());
  if (!hostEntPtr)
    throw ConnectionException(SystemError,
                              ulxr_i18n(ULXR_PCHAR("Could not get host+domain name: "))
                                   + ULXR_GET_STRING(getErrorString(getLastError())), 500);
  pimpl->host_name = ULXR_GET_STRING(hostEntPtr->h_name);

#else
# pragma message ("don't know how to determine the domain name")
#endif
}