Esempio n. 1
0
void *do_chld(void *arg)
{
  int 	client_socket = (int) arg;
  // the socket to the server if we are the controller
  int backend_socket = -1;
  int len = 0;
  int flag = 1;

  printf("Child thread: Socket number = %d\n", client_socket);

  while (flag) {
    struct op_hdr get_hdr;

    /* Read the header*/
    printf("\t\t\tfirst read\n");
    READ_SOC(len, client_socket, &get_hdr, HDR_SIZE);
    printf("***HDR***: op: %d, p1: %d, p2: %d, p3: %d\n",
	   get_hdr.op, get_hdr.p1, get_hdr.p2, get_hdr.p3);

    /* ACT */
    switch (get_hdr.op) {

    case OP_OPEN:
      controller_open(&backend_socket, client_socket, get_hdr);
      break;

    case OP_READ:
      controller_read(&backend_socket, client_socket, get_hdr);
      break;

    case OP_WRITE:
      controller_write(&backend_socket, client_socket, get_hdr);
      break;

    case OP_SEEK:
      controller_seek(&backend_socket, client_socket, get_hdr);
      break;

    case OP_SCANDIR:
      controller_scandir(&backend_socket, client_socket, get_hdr);
      break;

    case OP_CLOSE:
      controller_close(&backend_socket, client_socket, get_hdr);
      break;

    case OP_END:
      flag = 0;
      WRITE_SOC(len, backend_socket, &get_hdr, HDR_SIZE);
      break;

    default:

      /* ERROR! */
      /* Since we are the server, we dont know how to handle
	 errors on the client */
      get_hdr.op = -1;
      WRITE_SOC(len, client_socket, &get_hdr, HDR_SIZE);
      printf("Bad opcode: %d\n", get_hdr.op);
      break;

    }

  }
  printf("Child: Done Processing...\n"); 
  
  /* close the socket and exit this thread */
  close(client_socket);
  pthread_exit((void *)NULL);

  /* never reached */
  return(NULL);
}
Esempio n. 2
0
int net_controller_tick(controller *ctrl, int ticks, ctrl_event **ev) {
    ENetEvent event;
    wtf *data = ctrl->data;
    ENetHost *host = data->host;
    ENetPeer *peer = data->peer;
    serial *ser;
    /*int handled = 0;*/
    while (enet_host_service(host, &event, 0) > 0) {
        switch (event.type) {
            case ENET_EVENT_TYPE_RECEIVE:
                ser = malloc(sizeof(serial));
                serial_create(ser);
                ser->data = malloc(event.packet->dataLength);
                ser->len = event.packet->dataLength;
                memcpy(ser->data, event.packet->data, event.packet->dataLength);
                switch(serial_read_int8(ser)) {
                    case EVENT_TYPE_ACTION:
                        {
                            // dispatch keypress to scene
                            int action = serial_read_int16(ser);
                            controller_cmd(ctrl, action, ev);
                            /*handled = 1;*/
                            serial_free(ser);
                            free(ser);
                        }
                        break;
                    case EVENT_TYPE_HB:
                        {
                            // got a tick
                            int id = serial_read_int8(ser);
                            if (id == data->id) {
                                int start = serial_read_int32(ser);
                                int peerticks = serial_read_int32(ser);
                                int newrtt = abs(start - ticks);
                                data->rttbuf[data->rttpos++] = newrtt;
                                if (data->rttpos >= 100) {
                                    data->rttpos = 0;
                                    data->rttfilled = 1;
                                }
                                if (data->rttfilled == 1) {
                                    ctrl->rtt = avg_rtt(data->rttbuf, 100);
                                    data->tick_offset = (peerticks + (ctrl->rtt/2)) - ticks;
                                    /*DEBUG("I am %d ticks away from server: %d %d", data->tick_offset, ticks, peerticks);*/
                                }
                                data->outstanding_hb = 0;
                                data->last_hb = ticks;
                                serial_free(ser);
                            } else {
                                // a heartbeat from the peer, bounce it back
                                ENetPacket *packet;
                                // write our own ticks into it
                                serial_write_int32(ser, ticks);
                                packet = enet_packet_create(ser->data, ser->len, ENET_PACKET_FLAG_UNSEQUENCED);
                                if (peer) {
                                    enet_peer_send(peer, 0, packet);
                                    enet_host_flush (host);
                                }
                            }
                            free(ser);
                        }
                        break;
                    case EVENT_TYPE_SYNC:
                        controller_sync(ctrl, ser, ev);
                        /*handled = 1;*/
                        break;
                    default:
                        serial_free(ser);
                        free(ser);
                }
                enet_packet_destroy(event.packet);
                break;
            case ENET_EVENT_TYPE_DISCONNECT:
                DEBUG("peer disconnected!");
                data->disconnected = 1;
                controller_close(ctrl, ev);
                return 1; // bail the f**k out
                break;
            default:
                break;
        }
    }

    int tick_interval = 5;
    if (data->rttfilled) {
        tick_interval = 20;
    }

    if ((data->last_hb == -1 || ticks - data->last_hb > tick_interval) || !data->outstanding_hb) {
        data->outstanding_hb = 1;
        serial ser;
        ENetPacket *packet;
        serial_create(&ser);
        serial_write_int8(&ser, EVENT_TYPE_HB);
        serial_write_int8(&ser, data->id);
        serial_write_int32(&ser, ticks);
        packet = enet_packet_create(ser.data, ser.len, ENET_PACKET_FLAG_UNSEQUENCED);
        if (peer) {
            enet_peer_send(peer, 0, packet);
            enet_host_flush (host);
        } else {
            DEBUG("peer is null~");
            data->disconnected = 1;
            controller_close(ctrl, ev);
        }
    }

    /*if(!handled) {*/
        /*controller_cmd(ctrl, ACT_STOP, ev);*/
    /*}*/
    return 0;
}