std::string endpoint::ip() const { switch(family()) { case pf_inet: { #ifndef BOOSTER_WIN32 char buf[INET_ADDRSTRLEN+1] = {0}; char const *res = ::inet_ntop(AF_INET,&d->sa.in.sin_addr,buf,sizeof(buf)); if(res) return std::string(res); throw_invalid(); #else std::ostringstream tmp; tmp.imbue(std::locale::classic()); unsigned char const *p = reinterpret_cast<unsigned char const *>(&d->sa.in.sin_addr); tmp << int(p[0]) <<"." << int(p[1]) <<"."<<int(p[2]) <<"." << int(p[3]); return tmp.str(); #endif } break; #ifndef BOOSTER_AIO_NO_PF_INET6 case pf_inet6: { char buf[INET6_ADDRSTRLEN+1] = {0}; char const *res = ::inet_ntop(AF_INET6,&d->sa.in6.sin6_addr,buf,sizeof(buf)); if(res) return std::string(res); throw_invalid(); } break; #endif default: throw_invalid(); } }
void endpoint::ip(std::string const &ip_in) { if(ip_in.find('.')!=std::string::npos) { struct in_addr inaddr; #ifndef BOOSTER_WIN32 if(::inet_pton(AF_INET,ip_in.c_str(),&inaddr)==0) throw_invalid(); #else unsigned addr = inet_addr(ip_in.c_str()); if(addr==INADDR_NONE) throw_invalid(); else memcpy(&inaddr,&addr,sizeof(inaddr)); #endif d->size=sizeof(struct sockaddr_in); d->sa.in.sin_family=AF_INET; d->sa.in.sin_addr=inaddr; } #ifndef BOOSTER_AIO_NO_PF_INET6 else if(ip_in.find(':')!=std::string::npos) { struct in6_addr in6addr; if(::inet_pton(AF_INET6,ip_in.c_str(),&in6addr)==0) throw_invalid(); d->size=sizeof(struct sockaddr_in6); d->sa.in6.sin6_family=AF_INET6; d->sa.in6.sin6_addr=in6addr; } #endif else { throw_invalid(); } }
void endpoint::port(int port_no) { if(port_no > 0xFFFF || port_no < 0) throw_invalid(); switch(family()) { case pf_inet: d->sa.in.sin_port = htons(port_no); return; #ifndef BOOSTER_AIO_NO_PF_INET6 case pf_inet6: d->sa.in6.sin6_port = htons(port_no); return; #endif default: throw_invalid(); } }
typename std::iterator_traits<CharIterator>::value_type get_next_char(CharIterator& begin, CharIterator end) const { if (begin == end) { throw_invalid(); } return *begin++; }
query& query::update(const std::string &table) { throw_invalid(QUERY_UPDATE, state); sql_.append("UPDATE " + table + " SET "); state = QUERY_UPDATE; return *this; }
void check_close_brace(char c, char open_brace) const { if (open_brace == '{' && c == '}') { //great } else { throw_invalid(); } }
void check_close_brace(wchar_t c, wchar_t open_brace) const { if (open_brace == L'{' && c == L'}') { // great } else { throw_invalid(); } }
query& query::select() { throw_invalid(QUERY_SELECT, state); sql_.append("SELECT "); state = QUERY_SELECT; return *this; }
void endpoint::raw(sockaddr const *p,int size) { if(size > int(sizeof(d->sa))) throw_invalid(); d->size=size; memcpy(&d->sa.sa,p,size); }
family_type endpoint::family() const { if(d->size < 2) throw_invalid(); switch(d->sa.sa.sa_family) { case AF_INET: return pf_inet; #ifndef BOOSTER_AIO_NO_PF_INET6 case AF_INET6: return pf_inet6; #endif case AF_UNIX: return pf_unix; default: throw_invalid(); } }
void endpoint::path(std::string const &local_socket) { if(local_socket.size() + 1 > sizeof(d->sa.un.sun_path)) throw_invalid(); d->size = sizeof(d->sa.un); ::strncpy(d->sa.un.sun_path,local_socket.c_str(),sizeof(d->sa.un.sun_path) - 1); d->sa.un.sun_path[sizeof(d->sa.un.sun_path) - 1]=0; d->sa.un.sun_family = AF_UNIX; }
query& query::or_(const condition &c) { throw_invalid(QUERY_OR, state); sql_.append(std::string(" OR ")); sql_.append(c); state = QUERY_OR; return *this; }
query& query::remove(const prototype_node &node) { throw_invalid(QUERY_DELETE, state); sql_.append(std::string("DELETE FROM ") + node.type); state = QUERY_DELETE; return *this; }
query& query::where(const condition &c) { throw_invalid(QUERY_COND_WHERE, state); sql_.append(std::string(" WHERE ")); sql_.append(c); state = QUERY_COND_WHERE; return *this; }
query& query::column(const std::string &name, data_type_t type) { throw_invalid(QUERY_COLUMN, state); if (state == QUERY_COLUMN) { sql_.append(", "); } sql_.append(name.c_str(), type); state = QUERY_COLUMN; return *this; }
query& query::and_(const condition &c) { throw_invalid(QUERY_AND, state); sql_.append(std::string(" AND ")); sql_.append(c); state = QUERY_AND; return *this; }
query& query::group_by(const std::string &fld) { throw_invalid(QUERY_GROUPBY, state); sql_.append(std::string(" GROUP BY ") + fld); state = QUERY_GROUPBY; return *this; }
query& query::order_by(const std::string &by) { throw_invalid(QUERY_ORDERBY, state); sql_.append(std::string(" ORDER BY ") + by); state = QUERY_ORDERBY; return *this; }
query& query::where(const std::string &clause) { // throw on invalid state throw_invalid(QUERY_WHERE, state); sql_.append(std::string(" WHERE ") + clause); state = QUERY_WHERE; return *this; }
query& query::select(object_atomizable *o) { throw_invalid(QUERY_SELECT, state); sql_.append("SELECT "); query_select s(sql_); o->serialize(s); state = QUERY_SELECT; return *this; }
uuid operator()(CharIterator begin, CharIterator end) const { typedef typename std::iterator_traits<CharIterator>::value_type char_type; // check open brace char_type c = get_next_char(begin, end); bool has_open_brace = is_open_brace(c); char_type open_brace_char = c; if (has_open_brace) { c = get_next_char(begin, end); } bool has_dashes = false; uuid u; int i=0; for (uuid::iterator it_byte=u.begin(); it_byte!=u.end(); ++it_byte, ++i) { if (it_byte != u.begin()) { c = get_next_char(begin, end); } if (i == 4) { has_dashes = is_dash(c); if (has_dashes) { c = get_next_char(begin, end); } } if (has_dashes) { if (i == 6 || i == 8 || i == 10) { if (is_dash(c)) { c = get_next_char(begin, end); } else { throw_invalid(); } } } *it_byte = get_value(c); c = get_next_char(begin, end); *it_byte <<= 4; *it_byte |= get_value(c); } // check close brace if (has_open_brace) { c = get_next_char(begin, end); check_close_brace(c, open_brace_char); } return u; }
query& query::update(const std::string &type, object_atomizable *o) { throw_invalid(QUERY_OBJECT_UPDATE, state); sql_.append(std::string("UPDATE ") + type + std::string(" SET ")); query_update s(sql_); o->serialize(s); state = QUERY_OBJECT_UPDATE; return *this; }
int endpoint::port() const { switch(family()) { case pf_inet: return ntohs(d->sa.in.sin_port); #ifndef BOOSTER_AIO_NO_PF_INET6 case pf_inet6: return ntohs(d->sa.in6.sin6_port); #endif default: throw_invalid(); } }
query& query::select(const prototype_node &node) { throw_invalid(QUERY_OBJECT_SELECT, state); sql_.append("SELECT "); object *o = node.producer->create(); query_select s(sql_); o->serialize(s); delete o; sql_.append(" FROM "); sql_.append(node.type); state = QUERY_OBJECT_SELECT; return *this; }
query& query::insert(object_atomizable *o, const std::string &type) { throw_invalid(QUERY_OBJECT_INSERT, state); sql_.append(std::string("INSERT INTO ") + type + std::string(" (")); query_insert s(sql_); s.fields(); o->serialize(s); sql_.append(") VALUES ("); s.values(); o->serialize(s); sql_.append(")"); state = QUERY_OBJECT_INSERT; return *this; }
std::string endpoint::path() const { if(family()!=pf_unix) throw_invalid(); return d->sa.un.sun_path; }