示例#1
0
void serverms(int mode, int numplayers, int minremain, char *smapname, int seconds, bool isfull) {
  checkmasterreply();
  updatemasterserver(seconds);

  // reply all server info requests
  ENetBuffer buf;
  ENetAddress addr;
  u8 pong[MAXTRANS], *p;
  int len;
  enet_uint32 events = ENET_SOCKET_WAIT_RECEIVE;
  buf.data = pong;
  while (enet_socket_wait(pongsock, &events, 0) >= 0 && events) {
    buf.dataLength = sizeof(pong);
    len = enet_socket_receive(pongsock, &addr, &buf, 1);
    if (len < 0) return;
    p = &pong[len];
    putint(p, PROTOCOL_VERSION);
    putint(p, mode);
    putint(p, numplayers);
    putint(p, minremain);
    string mname;
    strcpy_s(mname, isfull ? "[FULL] " : "");
    strcat_s(mname, smapname);
    sendstring(mname, p);
    sendstring(serverdesc, p);
    buf.dataLength = p - pong;
    enet_socket_send(pongsock, &addr, &buf, 1);
  }
}
示例#2
0
void httpgetrecieve(ENetBuffer &buf) {
  if (mssock==ENET_SOCKET_NULL)
    return;
  enet_uint32 events = ENET_SOCKET_WAIT_RECEIVE;
  if (enet_socket_wait(mssock, &events, 0) >= 0 && events) {
    int len = enet_socket_receive(mssock, NULL, &buf, 1);
    if (len<=0) {
      enet_socket_destroy(mssock);
      mssock = ENET_SOCKET_NULL;
      return;
    }
    buf.data = ((char *)buf.data)+len;
    ((char*)buf.data)[0] = 0;
    buf.dataLength -= len;
  }
}
示例#3
0
文件: protocol.c 项目: wangeek/Egoboo
/** Waits for events on the host specified and shuttles packets between
    the host and its peers.

    @param host    host to service
    @param event   an event structure where event details will be placed if one occurs
    @param timeout number of milliseconds that ENet should wait for events
    @retval > 1 if an event occurred within the specified time limit
    @retval 0 if no event occurred
    @retval < 1 on failure
    @remarks enet_host_service should be called fairly regularly for adequate performance
    @ingroup host
*/
int
enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout)
{
    enet_uint32 waitCondition;

    event -> type = ENET_EVENT_TYPE_NONE;
    event -> peer = NULL;
    event -> packet = NULL;

    switch (enet_protocol_dispatch_incoming_commands (host, event))
    {
    case 1:
       return 1;

    case -1:
       perror ("Error dispatching incoming packets");

       return -1;

    default:
       break;
    }

    timeCurrent = enet_time_get ();

    timeout += timeCurrent;

    do
    {
       if (ENET_TIME_DIFFERENCE (timeCurrent, host -> bandwidthThrottleEpoch) >= ENET_HOST_BANDWIDTH_THROTTLE_INTERVAL)
         enet_host_bandwidth_throttle (host);

       switch (enet_protocol_send_outgoing_commands (host, event, 1))
       {
       case 1:
          return 1;

       case -1:
          perror ("Error sending outgoing packets");

          return -1;

       default:
          break;
       }

       switch (enet_protocol_receive_incoming_commands (host, event))
       {
       case 1:
          return 1;

       case -1:
          perror ("Error receiving incoming packets");

          return -1;

       default:
          break;
       }

       switch (enet_protocol_send_outgoing_commands (host, event, 1))
       {
       case 1:
          return 1;

       case -1:
          perror ("Error sending outgoing packets");

          return -1;

       default:
          break;
       }

       switch (enet_protocol_dispatch_incoming_commands (host, event))
       {
       case 1:
          return 1;

       case -1:
          perror ("Error dispatching incoming packets");

          return -1;

       default:
          break;
       }

       timeCurrent = enet_time_get ();

       if (ENET_TIME_GREATER_EQUAL (timeCurrent, timeout))
         return 0;

       waitCondition = ENET_SOCKET_WAIT_RECEIVE;

       if (enet_socket_wait (host -> socket, & waitCondition, ENET_TIME_DIFFERENCE (timeout, timeCurrent)) != 0)
         return -1;

       timeCurrent = enet_time_get ();
    } while (waitCondition == ENET_SOCKET_WAIT_RECEIVE);

    return 0;
}