//how states change between various states void create_states(i64 n, vector<vector<int> >& vtransit ) { //states gives us all effective numbers, the last one is n vector<string> states;//for each state, empty, a, ab, abc....; states.push_back(string()); string tstr(to_string(n)); for(unsigned int i = 0; i < tstr.size(); ++i){ states.push_back(tstr.substr(0,i+1)); } vtransit.clear(); //vtransit tells us how the state changes when we have //some new input vtransit.resize(states.size()); for(unsigned int i = 0; i < vtransit.size(); ++i){ vtransit[i].resize(states.size(), 0); } //the last state is not necessary to consider for(unsigned int j = 0; j < states.size()-1; ++j){ i64 n0 = 0; if(!states[j].empty()) n0 = stoll(states[j]); n0 *= 10; for(unsigned int i = 0; i < 10; ++i){ i64 num = n0 + i; string si(to_string(num)); int matched = check_states(si, j+1, states); ++vtransit[j][matched]; } } }
void TCPSocket::accept(const TCPSocket& socket) throw (TCPSocketException) { struct sockaddr_in client; socklen_t client_len; /* check states */ check_states(); if (!socket.listening) { throw TCPSocketException("Invalid listener socket."); } /* accept socket */ client_len = sizeof(client_len); this->socket = ::accept(socket.socket, reinterpret_cast<struct sockaddr *>(&client), &client_len); if (this->socket < 0) { throw TCPSocketException("Accept failed: " + std::string(strerror(errno))); } connected = true; }
void TCPSocket::listen(const char *address, unsigned short port, int backlog) throw (TCPSocketException) { struct sockaddr_in server; int rv; /* check states */ check_states(); /* create socket */ if ((socket = ::socket(AF_INET, SOCK_STREAM, 0)) < 0) { throw TCPSocketException("Cannot create socket."); } /* setup listener socket */ #ifdef __unix__ const int yes = 1; setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes); #elif _WIN32 const char yes = 1; setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes); #endif server.sin_family = AF_INET; if (address) { server.sin_addr.s_addr = inet_addr(address); } else { server.sin_addr.s_addr = htons(INADDR_ANY); } server.sin_port = htons(port); rv = bind(socket, reinterpret_cast<struct sockaddr *>(&server), sizeof(server)); if (rv < 0) { closesocket(socket); throw TCPSocketException("Bind failed: " + std::string(strerror(errno))); } /* listen */ listening = true; rv = ::listen(socket, backlog); if (rv < 0) { listening = connected = false; closesocket(socket); throw TCPSocketException("Listen failed: " + std::string(strerror(errno))); } }
client_t *new_client(Window w) { client_t *c; XWindowAttributes attr; XColor exact; long supplied; Atom win_type; c = malloc(sizeof *c); c->next = head; head = c; c->name = get_wm_name(w); c->win = w; c->frame = None; c->size.flags = 0; c->ignore_unmap = 0; #ifdef SHAPE c->shaped = 0; #endif c->shaded = 0; c->zoomed = 0; c->decor = 1; XGetWMNormalHints(dpy, c->win, &c->size, &supplied); XGetTransientForHint(dpy, c->win, &c->trans); XGetWindowAttributes(dpy, c->win, &attr); c->geom.x = attr.x; c->geom.y = attr.y; c->geom.w = attr.width; c->geom.h = attr.height; c->cmap = attr.colormap; c->old_bw = attr.border_width; #ifdef DEBUG dump_name(c, "creating", 'w'); dump_geom(c, "initial"); #endif XAllocNamedColor(dpy, c->cmap, opt_fg, &fg, &exact); XAllocNamedColor(dpy, c->cmap, opt_bg, &bg, &exact); XAllocNamedColor(dpy, c->cmap, opt_bd, &bd, &exact); if (get_atoms(c->win, net_wm_wintype, XA_ATOM, 0, &win_type, 1, NULL)) c->decor = HAS_DECOR(win_type); if (get_atoms(c->win, net_wm_desk, XA_CARDINAL, 0, &c->desk, 1, NULL)) { if (c->desk == -1) c->desk = DESK_ALL; /* FIXME */ if (c->desk >= ndesks && c->desk != DESK_ALL) c->desk = cur_desk; } else { set_atoms(c->win, net_wm_desk, XA_CARDINAL, &cur_desk, 1); c->desk = cur_desk; } #ifdef DEBUG dump_info(c); #endif check_states(c); /* We are not actually keeping the stack one in order. However, every * fancy panel uses it and nothing else, no matter what the spec says. * (I'm not sure why, as rearranging the list every time the stacking * changes would be distracting. GNOME's window list applet doesn't.) */ append_atoms(root, net_client_list, XA_WINDOW, &c->win, 1); append_atoms(root, net_client_stack, XA_WINDOW, &c->win, 1); return c; }
void TCPSocket::connect(const char *ip_address, unsigned short port) throw (TCPSocketException) { unsigned long address; struct sockaddr_in server; struct hostent *host_info; int rv; /* check states */ check_states(); /* create socket */ if ((socket = ::socket(PF_INET, SOCK_STREAM, 0)) < 0) { throw TCPSocketException("Failed to create socket."); } /* convert string to Internet address */ if ((address = inet_addr(ip_address)) != INADDR_NONE) { memcpy(&server.sin_addr, &address, sizeof(address)); } else { host_info = gethostbyname(ip_address); if (!host_info) { closesocket(socket); throw TCPSocketException("Unknown server: " + std::string(ip_address)); } memcpy(&server.sin_addr, host_info->h_addr, host_info->h_length); } server.sin_family = AF_INET; server.sin_port = htons(port); /* set to non blocking mode */ #ifdef __unix__ int flags = fcntl(socket, F_GETFL, 0); flags |= O_NONBLOCK; fcntl(socket, F_SETFL, flags); #elif _WIN32 u_long mode = 0; ioctlsocket(socket, FIONBIO, &mode); #endif /* open connection */ disconnecting = false; rv = ::connect(socket, reinterpret_cast<struct sockaddr *>(&server), sizeof(server)); if (rv < 0) { if (errno == EINPROGRESS) { int valopt; struct timeval tv; fd_set myset; socklen_t lon; tv.tv_sec = 0; tv.tv_usec = 0; while (true) { FD_ZERO(&myset); FD_SET(socket, &myset); if (select(socket + 1, 0, &myset, 0, &tv) > 0) { lon = sizeof(int); #ifdef __unix__ getsockopt(socket, SOL_SOCKET, SO_ERROR, static_cast<void *>(&valopt), &lon); #elif _WIN32 char cvalopt = 0; getsockopt(socket, SOL_SOCKET, SO_ERROR, &cvalopt, &lon); valopt = static_cast<int>(cvalopt); #endif if (valopt) { std::string err("Cannot connect to server: "); err.append(strerror(valopt)); throw TCPSocketException(err); } else { connected = true; break; } } #ifdef __unix__ usleep(100000); #elif _WIN32 Sleep(1); #endif if (disconnecting) return; } if (!connected) { throw TCPSocketException("Cannot connect to server, timed out."); } } else { std::string err("Cannot connect to server: "); err.append(strerror(errno)); throw TCPSocketException(err); } } else if (rv == 0) { connected = true; } /* set to blocking mode again */ #ifdef __unix__ flags = fcntl(socket, F_GETFL, 0); flags &= (~O_NONBLOCK); fcntl(socket, F_SETFL, flags); #elif _WIN32 mode = 0; ioctlsocket(socket, FIONBIO, &mode); #endif }