Пример #1
0
static int initClient(struct _client *client, int fd){
    protocolInit(&client->m_protocol);
    writeBuffInit(&client->m_manager);
    client->used = 1;
    client->rx = clientRx;
    client->sendCommand = sendCommand;
    client->socket  = fd;
    client->write = write;
    client->read = read;
    return 0;
}
Пример #2
0
/*******************************************************************************
* Function Name  : main.
* Description    : Main routine.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
int main(void)
{
    led_init();
    timer_init();

    led_sys_on();
    timer_sleep(10000);
    led_sys_off();

    protocolInit();
    clockInit();

    while(1)
    {
        //led_sys_on();
        protocolPoll();
        //led_sys_off();
        clockPoll();
    }
}
Пример #3
0
bool mainLoop(int argc, char **argv)
{
  const char *port = PORT;
  int backlog = BACKLOG;
  int bufferSize = BUFFER_SIZE;
  bool interactive = INTERACTIVE;
  args_param_t args_param_list[] =
  {
    {"-p",            &port,        argsString },
    {"--port",        &port,        argsString },
    {"-b",            &backlog,     argsInteger },
    {"--backlog",     &backlog,     argsInteger },
    {"-B",            &bufferSize,  argsInteger },
    {"--buffer",      &bufferSize,  argsInteger },
    {"-i",            &interactive, argsBoolTrue },
    {"--interactive", &interactive, argsBoolTrue },
    {"-d",            &interactive, argsBoolFalse },
    {"--daemon",      &interactive, argsBoolFalse },
    ARGS_DONE
  };
  argsProcess(argc, argv, args_param_list);

  int socket = -1;
  fd_set socketSet;

  bool success = true;
  success = success && socketOpen(port, backlog, &socket);
  success = success && socketSetInitialize(socket, &socketSet, interactive);
  if (success) {
    infof("Message buffer size set to %d.", bufferSize);
  }

  // main loop
  int maxSocket = socket;
  success = success && protocolInit(socket, &socketSet, maxSocket, bufferSize);
  bool done = !success; // skip loop on error
  while (!done) {
    fd_set readSocketSet;
    FD_COPY(&socketSet, &readSocketSet);
    success = success && socketSelect(maxSocket, &readSocketSet);
    for (int s = 0; !done && s <= maxSocket; s++) {
      if (FD_ISSET(s, &readSocketSet)) {
        if (s == socket) {
          socketConnectionNew(s, &maxSocket, &socketSet, bufferSize); // failure is not terminal
        }
        else if (STDIN_FILENO == s) {
          // system control
          done = localControl(s, &socketSet, maxSocket, bufferSize);
        }
        else {
          // existing connection
          protocolUpdate(s, &socketSet, maxSocket, bufferSize); // failure is not terminal
        }
      }
    }
    done = done || !success;
  }
  protocolCleanup(socket, &socketSet, maxSocket, bufferSize); // always

  close(socket);

  return done;
}