Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 196 lines (148 sloc) 3.231 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
#ifndef RBX_WINDOWS
#include <sys/select.h>
#endif
#include <vector>

#include "auxiliary_threads.hpp"
#include "gc/managed.hpp"
#include "util/thread.hpp"

#include "windows_compat.h"

namespace rubinius {
  class SharedState;

  namespace agent {
    class VariableAccess;
    class Output;
  }

  class QueryAgent : public AuxiliaryThread, public Lockable {
    struct Client {
      enum State {
        eUnknown,
        eWaitingAuth,
        eRunning
      } state;

      int socket;
      int auth_key;

      Client(int s)
        : state(eUnknown)
        , socket(s)
        , auth_key(0)
      {}

      void set_running() {
        state = eRunning;
      }

      bool needs_auth_p() {
        return state == eWaitingAuth;
      }

      void begin_auth(int key) {
        auth_key = key;
        state = eWaitingAuth;
      }

    };

    class Thread : public utilities::thread::Thread {
    private:
      QueryAgent* agent_;
      bool exit_;

    public:
      Thread(QueryAgent* agent);

      void stop();
      virtual void perform();
    };

  private:
    SharedState& shared_;
    Thread* thread_;
    int port_;
    int server_fd_;
    bool verbose_;
    fd_set fds_;
    int max_fd_;

    int control_[2];
    int loopback_[2];

    int a2r_[2];
    int r2a_[2];

    std::vector<Client> sockets_;

    agent::VariableAccess* vars_;

    bool local_only_;
    bool use_password_;
    std::string password_;
    uint32_t tmp_key_;

    const static int cBackLog = 10;

  public:
    QueryAgent(STATE);
    virtual ~QueryAgent();

    void set_verbose() {
      verbose_ = true;
    }

    int port() {
      return port_;
    }

    fd_set fds() {
      return fds_;
    }

    int max_fd() {
      return max_fd_;
    }

    int server_fd() {
      return server_fd_;
    }

    bool local_only() {
      return local_only_;
    }

    bool use_password() {
      return use_password_;
    }

    bool verbose() {
      return verbose_;
    }

    uint32_t incr_tmp_key() {
      return tmp_key_++;
    }

    void add_socket(Client client) {
      sockets_.push_back(client);
    }

    void add_fd(int fd) {
      FD_SET((int_fd_t)fd, &fds_);
      if(fd > max_fd_) max_fd_ = fd;
    }

    void remove_fd(int fd) {
      FD_CLR((int_fd_t)fd, &fds_);
    }

    int loopback_socket() {
      return loopback_[1];
    }

    int read_control() {
      return control_[0];
    }

    int write_control() {
      return control_[1];
    }

    int a2r_agent() {
      return a2r_[1];
    }

    int a2r_ruby() {
      return a2r_[0];
    }

    int r2a_agent() {
      return r2a_[0];
    }

    int r2a_ruby() {
      return r2a_[1];
    }

    void initialize(STATE);
    void start_thread(STATE);
    void stop_thread(STATE);
    void wakeup();

    bool setup_local();
    bool bind(int port);

    void make_discoverable();

    bool check_password(Client& client);
    bool check_file_auth(Client& client);
    bool process_commands(Client& client);
    bool process_clients(fd_set fds);

    void cleanup();

    void shutdown(STATE);
    void before_exec(STATE);
    void after_exec(STATE);
    void before_fork(STATE);
    void after_fork_parent(STATE);
    void after_fork_child(STATE);
  };
}
Something went wrong with that request. Please try again.