Exemple #1
0
void BaseTCPServer::ListenNewConnections()
{
    Socket*         sock;
    sockaddr_in     from;
    unsigned int    fromlen;

    from.sin_family = AF_INET;
    fromlen = sizeof( from );

    MutexLock lock( mMSock );

    // Check for pending connects
    while( ( sock = mSock->accept( (sockaddr*)&from, &fromlen ) ) != NULL )
    {
#ifdef WIN32
        unsigned long nonblocking = 1;
        sock->ioctl( FIONBIO, &nonblocking );
#else
        sock->fcntl( F_SETFL, O_NONBLOCK );
#endif /* !WIN32 */

        unsigned int bufsize = 64 * 1024; // 64kbyte receive buffer, up from default of 8k
        sock->setopt( SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof( bufsize ) );

        // New TCP connection, this must consume the socket.
        CreateNewConnection( sock, from.sin_addr.s_addr, ntohs( from.sin_port ) );
    }
}
Exemple #2
0
/*!
 * \brief The primary purpose of this action is to rebuild a netlist
 * from a script, in conjunction with the clear action above.
 */
static int
netlist_add (const char *netname, const char *pinname)
{
  int ni, pi;
  LibraryType *netlist = &PCB->NetlistLib;
  LibraryMenuType *net = NULL;
  LibraryEntryType *pin = NULL;

  for (ni=0; ni<netlist->MenuN; ni++)
    if (strcmp (netlist->Menu[ni].Name+2, netname) == 0)
      {
	net = & (netlist->Menu[ni]);
	break;
      }
  if (net == NULL)
    {
      net = CreateNewNet (netlist, (char *)netname, NULL);
    }

  for (pi=0; pi<net->EntryN; pi++)
    if (strcmp (net->Entry[pi].ListEntry, pinname) == 0)
      {
	pin = & (net->Entry[pi]);
	break;
      }
  if (pin == NULL)
    {
      pin = CreateNewConnection (net, (char *)pinname);
    }

  NetlistChanged (0);
  return 0;
}
Exemple #3
0
void NET_Server::PrivateOnConnectionAccepted()
{
	sockaddr sa;
	int sa_len = sizeof(sa);
	SOCKET new_conn_socket = WSAAccept(m_socket, &sa, &sa_len, NULL, 0);
	if (new_conn_socket == INVALID_SOCKET)
	{
		OnError("NET_Server::PrivateOnConnectionAccepted, new_conn_socket == INVALID_SOCKET");
		return;
	}
	NET_Connection* new_connection = CreateNewConnection();
	new_connection->SetBuffersSize(m_in_buf_size, m_in_data_size, m_out_buf_size, m_out_data_size);
	{
		MutexWrap connections_access(m_connections_mutex);
		new_connection->InitConnection(new_conn_socket, this);
		m_connections.push_back(new_connection);
	}
	OnConnectionAccepted(new_connection);
}
Exemple #4
0
CJaxerConnection* CJCPool::GetConnection()
{
    m_lock.Lock();
    CJaxerConnection* c = 0;
    if (m_header && m_header->m_next)
    {
       CJCList* l = m_header->m_next;
       m_header->m_next = l->m_next;
       if (m_header->m_next == 0)
       {
           m_tail = m_header;
           if (m_nConnections != 1)
           {
               GetJaxerLog().Log(eWARN, "GetConnection: remaing # of idle connections=%d (expected 1).", m_nConnections);
           }
       }

       c = l->m_connection;
       l->m_connection = 0;
       delete l;
       l = 0;

       m_nConnections--;

       GetJaxerLog().Log(eDEBUG, "GetConnection: remaing # of idle connections=%d.", m_nConnections);

       if(!c->BeginRequest())
       {
           GetJaxerLog().Log(eINFO, "GetConnection: Existing connection cannot be reused; will create new connection.");
           delete c;
           c = 0;
       }
    }
    
    if (!c)
        c = CreateNewConnection();

    m_lock.Unlock();

    return c;
}
Exemple #5
0
void BaseTCPServer::ListenNewConnections() {
	SOCKET tmpsock;
	struct sockaddr_in	from;
	struct in_addr	in;
	unsigned int	fromlen;
	unsigned short	port;

	from.sin_family = AF_INET;
	fromlen = sizeof(from);
	LockMutex lock(&MSock);
#ifndef DARWIN // Corysia - On OSX, 0 is a valid fd.
	if (!sock)
		return;
#else
	if (sock == -1) return;
#endif

	// Check for pending connects
#ifdef _WINDOWS
	unsigned long nonblocking = 1;
	while ((tmpsock = accept(sock, (struct sockaddr*) &from, (int *) &fromlen)) != INVALID_SOCKET) {
		ioctlsocket (tmpsock, FIONBIO, &nonblocking);
#else
#ifdef __CYGWIN__
	while ((tmpsock = accept(sock, (struct sockaddr *) &from, (int *) &fromlen)) != INVALID_SOCKET) {
#else
	while ((tmpsock = accept(sock, (struct sockaddr*) &from, &fromlen)) != INVALID_SOCKET) {
#endif
		fcntl(tmpsock, F_SETFL, O_NONBLOCK);
#endif
		int bufsize = 64 * 1024; // 64kbyte recieve buffer, up from default of 8k
		setsockopt(tmpsock, SOL_SOCKET, SO_RCVBUF, (char*) &bufsize, sizeof(bufsize));
		port = from.sin_port;
		in.s_addr = from.sin_addr.s_addr;

		// New TCP connection, this must consume the socket.
		CreateNewConnection(GetNextID(), tmpsock, in.s_addr, ntohs(from.sin_port));
	}
}

bool BaseTCPServer::Open(uint16 in_port, char* errbuf) {
	if (errbuf)
		errbuf[0] = 0;
	LockMutex lock(&MSock);
	if (sock != 0) {
		if (errbuf)
			snprintf(errbuf, TCPServer_ErrorBufferSize, "Listening socket already open");
		return false;
	}
	if (in_port != 0) {
		pPort = in_port;
	}

#ifdef _WINDOWS
	SOCKADDR_IN address;
	unsigned long nonblocking = 1;
#else
	struct sockaddr_in address;
#endif
	int reuse_addr = 1;

//	Setup internet address information.
//	This is used with the bind() call
	memset((char *) &address, 0, sizeof(address));
	address.sin_family = AF_INET;
	address.sin_port = htons(pPort);
	address.sin_addr.s_addr = htonl(INADDR_ANY);

//	Setting up TCP port for new TCP connections
	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock == INVALID_SOCKET) {
		if (errbuf)
			snprintf(errbuf, TCPServer_ErrorBufferSize, "socket(): INVALID_SOCKET");
		return false;
	}

// Quag: dont think following is good stuff for TCP, good for UDP
// Mis: SO_REUSEADDR shouldn't be a problem for tcp--allows you to restart
// without waiting for conns in TIME_WAIT to die
	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse_addr, sizeof(reuse_addr));


	if (bind(sock, (struct sockaddr *) &address, sizeof(address)) < 0) {
#ifdef _WINDOWS
		closesocket(sock);
#else
		close(sock);
#endif
		sock = 0;
		if (errbuf)
			sprintf(errbuf, "bind(): <0");
		return false;
	}

	int bufsize = 64 * 1024; // 64kbyte recieve buffer, up from default of 8k
	setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*) &bufsize, sizeof(bufsize));
#ifdef _WINDOWS
	ioctlsocket (sock, FIONBIO, &nonblocking);
#else
	fcntl(sock, F_SETFL, O_NONBLOCK);
#endif

	if (listen(sock, SOMAXCONN) == SOCKET_ERROR) {
#ifdef _WINDOWS
		closesocket(sock);
		if (errbuf)
			snprintf(errbuf, TCPServer_ErrorBufferSize, "listen() failed, Error: %d", WSAGetLastError());
#else
		close(sock);
		if (errbuf)
			snprintf(errbuf, TCPServer_ErrorBufferSize, "listen() failed, Error: %s", strerror(errno));
#endif
		sock = 0;
		return false;
	}

	return true;
}

void BaseTCPServer::Close() {
	StopLoopAndWait();

	LockMutex lock(&MSock);
	if (sock) {
#ifdef _WINDOWS
		closesocket(sock);
#else
		close(sock);
#endif
	}
	sock = 0;
}

bool BaseTCPServer::IsOpen() {
	MSock.lock();
	bool ret = (bool) (sock != 0);
	MSock.unlock();
	return ret;
}