Пример #1
0
void Client::Logic()
{
  clock_t time = clock();
  clock_t actime = time;

  SOCKET bsock;

  SocketBase bcomm;
  sockaddr_in &bsin = bcomm.Connect( PORT_BROADCAST );
  bsock = bcomm.CreateSocket();
  bcomm.Bind();
  bcomm.SetBroadcast( true );

  int keysize = int( key.size() + 1 );
  int portsize = sizeof port;

  char *message = new char[ keysize + portsize ];

  //memcpy( message, this.key.c_str(), keysize );
  //memcpy( message + keysize, &this.port, portsize );

  while ( running )
  {
    LONGLONG flowend = Clock() - flowstart;
    if ( flowend >  1000000LL )
    {
      flowstart = Clock();
      currentdatasent = 0;
      float l = GetRTT();
      if ( l < 0.0001f )
        BandWidth += 512;
      else if ( l > 0.001f )
        BandWidth -= 512;
    }

    if ( !ServerFound )
    {
      FindServer( bsock, message, portsize, keysize );
    }

    SendPackets();

    ReceivePackets();

    HandlePackets();

    UpdateRTT();

    Sleep( 10 );
  }
}
Пример #2
0
void CServer::Run ()
{
    while (m_bRun)
    {
        if (static_cast<int> (m_vpClients.size ()) > 1)
        {
            ReceivePackets ();
            if (m_Clock.getElapsedTime () >= sf::milliseconds (1000 / m_uiTickRate))
            {
                SendPackets ();
                m_Clock.restart ();
            }
        }
        else
            ListenOnIncomingConnections ();
        if (sf::Keyboard::isKeyPressed (sf::Keyboard::Escape))
            m_bRun = false;
        sf::sleep (sf::milliseconds (1));
    }
}
Пример #3
0
    void ConnectorInterface::ThreadFunc()
    {
#if BEHAVIAC_COMPILER_MSVC
        //printf("ThreadFunc gs_threadFlag = %d\n", (int)gs_threadFlag.value());
        BEHAVIAC_ASSERT(gs_threadFlag.value() == 0);
#endif
        {
            ScopedInt_t scopedInt(&gs_threadFlag);
            Log("behaviac: Socket Thread Starting\n");
#if BEHAVIAC_COMPILER_MSVC
            BEHAVIAC_ASSERT(t_packetBufferIndex != TLS_OUT_OF_INDEXES);
#else
            //printf("ThreadFunc t_packetBufferIndex = %d\n", t_packetBufferIndex);
            //BEHAVIAC_ASSERT(t_packetBufferIndex != (unsigned int)-1);
#endif//
        }
        namespace Socket = behaviac::Socket;
        const bool blockingSocket = true;
        behaviac::Socket::Handle	serverSocket = 0;
        {
            ScopedInt_t scopedInt(&gs_threadFlag);
            serverSocket = Socket::Create(blockingSocket);

            if (!serverSocket)
            {
                Log("behaviac: Couldn't create server socket.\n");
                return;
            }

            char bufferTemp[64];
            string_sprintf(bufferTemp, "behaviac: Listening at port %d...\n", m_port);
            Log(bufferTemp);

            // max connections: 1, don't allow multiple clients?
            if (!Socket::Listen(serverSocket, m_port, 1))
            {
                Log("behaviac: Couldn't configure server socket.\n");
                Socket::Close(serverSocket);
                return;
            }
        }
#if BEHAVIAC_COMPILER_MSVC
        BEHAVIAC_ASSERT(gs_threadFlag.value() == 0);
#endif

        this->ReserveThreadPacketBuffer();

        while (!m_terminating)
        {
#if BEHAVIAC_COMPILER_MSVC

            //wait for connecting
            while (!m_terminating)
            {
                //Log("Socket::TestConnection.\n");
                if (Socket::TestConnection(serverSocket))
                {
                    break;
                }

                behaviac::Thread::Sleep(100);
            }

#endif

            if (!m_terminating)
            {
                BEHAVIAC_ASSERT(gs_threadFlag.value() == 0);
                Log("behaviac: accepting...\n");
                {
                    ScopedInt_t scopedInt(&gs_threadFlag);
                    m_writeSocket = Socket::Accept(serverSocket, kSocketBufferSize);

                    if (!m_writeSocket)
                    {
                        Log("behaviac: Couldn't create write socket.\n");
                        Socket::Close(serverSocket);
                        return;
                    }

                    Log("behaviac: connection accepted\n");
                }

                BEHAVIAC_ASSERT(gs_threadFlag.value() == 0);

                {
                    ScopedInt_t scopedInt(&gs_threadFlag);

                    AtomicInc(m_isConnected);
                    behaviac::Thread::Sleep(1);

                    OnConnection();

                    AtomicInc(m_isConnectedFinished);
                    behaviac::Thread::Sleep(1);

                    //this->OnConnectionFinished();

                    Log("behaviac: after Connected.\n");
                }

                BEHAVIAC_ASSERT(gs_threadFlag.value() == 0);

                while (!m_terminating && this->m_writeSocket)
                {
                    behaviac::Thread::Sleep(1);
                    SendAllPackets();

                    ReceivePackets();
                }

                BEHAVIAC_ASSERT(gs_threadFlag.value() == 0);

                // One last time, to send any outstanding packets out there.
                if (this->m_writeSocket)
                {
                    SendAllPackets();

					Socket::Close(m_writeSocket);
				}

                this->Clear();

                Log("behaviac: disconnected. \n");
            }
        }//while (!m_terminating)

        Socket::Close(serverSocket);

        this->Clear();

        BEHAVIAC_ASSERT(gs_threadFlag.value() == 0);

        Log("behaviac: ThreadFunc exited. \n");
    }
Пример #4
0
void UpdateTime()
{
  RECT r;
  ::GetClientRect(hWnd, &r);
  double now = GetRealTime();
  if (keyDown[VK_F3]) {
    gPaused = !gPaused;
    if (gPaused) {
      lastPauseTime = now;
    }
    keyDown[VK_F3] = false;
    ::InvalidateRect(hWnd, 0, true);
  }
  if (keyDown[VK_F4]) {
    ::InvalidateRect(hWnd, 0, true);
    gPaused = false;
    gStepping = true;
    keyDown[VK_F4] = false;
  }
  if (keyDown[VK_F2]) {
    gPointDisplay = !gPointDisplay;
    keyDown[VK_F2] = false;
    ::InvalidateRect(hWnd, 0, true);
  }

  if (gPaused) {
    pauseDelta += (now - lastPauseTime);
    lastPauseTime = now;
    return;
  }
  
  now -= pauseDelta;
  ReceivePackets(now);
  double dt = now - lastReadTime;
  lastReadTime = now;
  if (keyDown[VK_RIGHT]) {
    dir -= float(dt * turn);
  }
  if (keyDown[VK_LEFT]) {
    dir += float(dt * turn);
  }
  if (dir < -M_PI) {
    dir += 2 * M_PI;
  }
  if (dir > M_PI) {
    dir -= 2 * M_PI;
  }
  myPos.x += float(cos(dir) * vel * dt);
  myPos.y -= float(sin(dir) * vel * dt);
  if (myPos.x > r.right) {
    myPos.x = (float)r.right;
    //  bounce
    if (fabs(dir) < M_PI / 2) {
      if (dir > 0) {
        dir = M_PI - dir;
      }
      else {
        dir = -M_PI - dir;
      }
    }
  }
  if (myPos.x < 0) {
    myPos.x = 0;
    if (fabs(dir) > M_PI / 2) {
      if (dir > 0) {
        dir = M_PI - dir;
      }
      else {
        dir = -M_PI - dir;
      }
    }
  }
  if (myPos.y > r.bottom) {
    myPos.y = (float)r.bottom;
    if (dir < 0) {
      dir = -dir;
    }
  }
  if (myPos.y < 0) {
    myPos.y = 0;
    if (dir > 0) {
      dir = -dir;
    }
  }

  recordArray[0] = myPos;
  intPos.ReadPosition(now, &extrapolatedPos[0].x);
  if (now >= lastRecordedPos + 1.0/STORERATE) {
    memmove(&extrapolatedPos[1], &extrapolatedPos[0], sizeof(extrapolatedPos) - sizeof(extrapolatedPos[0]));
    memmove(&recordArray[1], &recordArray[0], sizeof(recordArray) - sizeof(recordArray[0]));
    lastRecordedPos = now;
    if (gStepping) {
      gStepping = false;
      gPaused = true;
      lastPauseTime = GetRealTime();
      ::InvalidateRect(hWnd, 0, true);
    }
  }
  if (now >= lastSentTime + 1.0/SENDRATE) {
    SendPacket(myPos, now);
    lastSentTime = now;
  }
  DrawWindow(hWnd, GetDC(hWnd));
}
Пример #5
0
void ProcessSockets(void)
{
	if (socketpool.length >= events.capacity)
	{
		bprintf("Reserving more space for events\n");
		vec_reserve(&events, socketpool.length * 2);
	}

	bprintf("Entering epoll_wait\n");

	int total = epoll_wait(EpollHandle, &vec_first(&events), events.capacity, config->readtimeout * 1000);

	if (total == -1)
	{
		if (errno != EINTR)
			fprintf(stderr, "Error processing sockets: %s\n", strerror(errno));
		return;
	}

	for (int i = 0; i < total; ++i)
	{
		epoll_t *ev = &(events.data[i]);

		socket_t s;
		if (FindSocket(ev->data.fd, &s) == -1)
		{
			bfprintf(stderr, "Unknown FD in multiplexer: %d\n", ev->data.fd);
			// We don't know what socket this is. Someone added something
			// stupid somewhere so shut this shit down now.
			// We have to create a temporary socket_t object to remove it
			// from the multiplexer, then we can close it.
			socket_t tmp = { ev->data.fd, 0, 0, 0, 0 };
			RemoveFromMultiplexer(tmp);
			close(ev->data.fd);
			continue;
		}

		// Call our event.
		CallEvent(EV_SOCKETACTIVITY, &s);

		if (ev->events & (EPOLLHUP | EPOLLERR))
		{
			bprintf("Epoll error reading socket %d, destroying.\n", s.fd);
			DestroySocket(s, 1);
			continue;
		}

		// Process socket write events
		if (ev->events & EPOLLOUT && SendPackets(s) == -1)
		{
			bprintf("Destorying socket due to send failure!\n");
			DestroySocket(s, 1);
		}

		// process socket read events.
		if (ev->events & EPOLLIN && ReceivePackets(s) == -1)
		{
			bprintf("Destorying socket due to receive failure!\n");
			DestroySocket(s, 1);
		}
	}
}