sock_result_t socket_create_tcp_server(uint16_t port, network_interface_t nif) { socket_t* socket = new socket_t(); tcp_server_t* server = new tcp_server_t(); wiced_result_t result = WICED_OUT_OF_HEAP_SPACE; if (socket && server) { result = wiced_tcp_server_start(server, wiced_wlan_interface(nif), port, WICED_MAXIMUM_NUMBER_OF_SERVER_SOCKETS, server_connected, server_received, server_disconnected, NULL); } if (result!=WICED_SUCCESS) { if (socket) { delete socket; socket = NULL; } if (server) { server->close(); delete server; server = NULL; } } else { socket->set_server(server); SocketListLock lock(list_for(socket)); add_socket(socket); } return socket ? as_sock_result(socket) : as_sock_result(result); }
/** * Create a new socket handle. * @param family Must be {@code AF_INET} * @param type Either SOCK_DGRAM or SOCK_STREAM * @param protocol Either IPPROTO_UDP or IPPROTO_TCP * @return */ sock_handle_t socket_create(uint8_t family, uint8_t type, uint8_t protocol, uint16_t port, network_interface_t nif) { if (family!=AF_INET || !((type==SOCK_DGRAM && protocol==IPPROTO_UDP) || (type==SOCK_STREAM && protocol==IPPROTO_TCP))) return SOCKET_INVALID; sock_handle_t result = SOCKET_INVALID; socket_t* socket = new socket_t(); if (socket) { wiced_result_t wiced_result; socket->set_type((protocol==IPPROTO_UDP ? socket_t::UDP : socket_t::TCP)); if (protocol==IPPROTO_TCP) { wiced_result = wiced_tcp_create_socket(tcp(socket), wiced_wlan_interface(nif)); } else { wiced_result = wiced_udp_create_socket(udp(socket), port, wiced_wlan_interface(nif)); } if (wiced_result!=WICED_SUCCESS) { socket->set_type(socket_t::NONE); delete socket; result = as_sock_result(wiced_result); } else { SocketListLock lock(list_for(socket)); add_socket(socket); result = as_sock_result(socket); } } return result; }
/** * Discards a previously allocated socket. If the socket is already invalid, returns silently. * Once a socket has been passed to the client, this is the only time the object is * deleted. Since the client initiates this call, the client is aware can the * socket is no longer valid. * @param handle The handle to discard. * @return SOCKET_INVALID always. */ sock_handle_t socket_dispose(sock_handle_t handle) { if (socket_handle_valid(handle)) { socket_t* socket = from_handle(handle); SocketList& list = list_for(socket); /* IMPORTANT: SocketListLock is acquired first */ SocketListLock lock(list); std::lock_guard<socket_t> lk(*socket); if (list.remove(socket)) delete socket; } return SOCKET_INVALID; }
/** * Fetch the next waiting client socket from the server * @param sock * @return */ sock_result_t socket_accept(sock_handle_t sock) { sock_result_t result = SOCKET_INVALID; socket_t* socket = from_handle(sock); if (is_open(socket) && is_server(socket)) { std::lock_guard<socket_t> lk(*socket); tcp_server_t* server = socket->s.tcp_server; tcp_server_client_t* client = server->next_accept(); if (client) { socket_t* socket = new socket_t(); socket->set_client(client); { SocketListLock lock(list_for(socket)); add_socket(socket); } result = (sock_result_t)socket; } } return result; }
PRIVATE void list_horse(char **buf, struct comal_line *line) { if (!line) return; switch (line->cmd) { case 0: break; case runSYM: case delSYM: case chdirSYM: case rmdirSYM: case mkdirSYM: case osSYM: case dirSYM: case unitSYM: case select_outputSYM: case select_inputSYM: case returnSYM: case elifSYM: case traceSYM: case untilSYM: list_symsp(buf, line->cmd); list_exp(buf, line->lc.exp); break; case exitSYM: list_sym(buf, line->cmd); if (line->lc.exp) { list_char(buf, ' '); list_symsp(buf, whenSYM); list_exp(buf, line->lc.exp); } break; case stopSYM: list_sym(buf, line->cmd); if (line->lc.exp) { list_char(buf, ' '); list_exp(buf, line->lc.exp); } break; case elseSYM: case endSYM: case endcaseSYM: case endifSYM: case endloopSYM: case endwhileSYM: case otherwiseSYM: case loopSYM: case nullSYM: case retrySYM: case pageSYM: case handlerSYM: case endtrapSYM: list_sym(buf, line->cmd); break; case repeatSYM: list_repeat(buf, line); break; case trapSYM: list_sym(buf, line->cmd); if (line->lc.traprec.esc) { list_char(buf, ' '); list_sym(buf, escSYM); list_sym(buf, line->lc.traprec.esc); } break; case execSYM: if (show_exec) list_symsp(buf, execSYM); list_exp(buf, line->lc.exp); break; case caseSYM: list_symsp(buf, line->cmd); list_expsp(buf, line->lc.exp); list_sym(buf, ofSYM); break; case cursorSYM: list_symsp(buf, line->cmd); list_twoexp(buf, &line->lc.twoexp, ",", 1); break; case closeSYM: list_sym(buf, line->cmd); if (line->lc.exproot) { list_char(buf, ' '); list_symsp(buf, fileSYM); list_explist(buf, line->lc.exproot, 0); } break; case sysSYM: case dataSYM: list_symsp(buf, line->cmd); list_explist(buf, line->lc.exproot, 0); break; case localSYM: case dimSYM: list_dim(buf, line); break; case forSYM: list_for(buf, line); break; case funcSYM: case procSYM: list_pf(buf, line); break; case ifSYM: list_ifwhile(buf, thenSYM, line); break; case importSYM: list_import(buf, line); break; case inputSYM: list_input(buf, line); break; case openSYM: list_symsp(buf, line->cmd); list_symsp(buf, fileSYM); list_exp(buf, line->lc.openrec.filenum); list_text(buf, ", "); list_exp(buf, line->lc.openrec.filename); list_text(buf, ", "); list_symsp(buf, line->lc.openrec.type); if (line->lc.openrec.reclen) { list_exp(buf, line->lc.openrec.reclen); if (line->lc.openrec.read_only) { list_char(buf, ' '); list_sym(buf, read_onlySYM); } } break; case printSYM: list_print(buf, line); break; case readSYM: list_symsp(buf, line->cmd); if (line->lc.readrec.modifier) list_file(buf, line->lc.readrec.modifier); list_explist(buf, line->lc.readrec.lvalroot, 0); break; case endfuncSYM: case endprocSYM: list_sym(buf, line->cmd); if (line->lineptr) { list_char(buf, ' '); list_text(buf, line->lineptr->lc.pfrec.id->name); } break; case endforSYM: list_sym(buf, line->cmd); if (line->lineptr) { list_char(buf, ' '); list_exp(buf, line->lineptr->lc.forrec.lval); } break; case restoreSYM: list_sym(buf, line->cmd); if (line->lc.id) { list_char(buf, ' '); list_text(buf, line->lc.id->name); } break; case whenSYM: list_symsp(buf, line->cmd); list_whenlist(buf, line->lc.whenroot); break; case whileSYM: list_ifwhile(buf, doSYM, line); break; case writeSYM: list_symsp(buf, line->cmd); list_file(buf, &line->lc.writerec.twoexp); list_explist(buf, line->lc.writerec.exproot, 0); break; case becomesSYM: list_assign(buf, line); break; case idSYM: list_text(buf, line->lc.id->name); list_char(buf, ':'); break; default: list_text(buf, "<error: List default action>"); } }
/** * Determines if the given socket still exists in the list of current sockets. */ bool exists_socket(socket_t* socket) { return socket && list_for(socket).exists(socket); }
void add_socket(socket_t* socket) { if (socket) { list_for(socket).add(socket); } }
void listline(char *var, statement *stmt, byte showline) { int x; outcount = 0; if (stmt->errorflag) { mysprintf(var, "*ERR "); for (x=0; x<tokenpos; x++) mysprintf(var, " "); mysprintf(var, "V\n"); } if (showline) mysprintf(var, "%.5d ", stmt->linenum); if (stmt->errorflag) { mysprintf(var, "%s", stmt->metalist[1].stringarg); return; } if (stmt->numlabels) { for (x=0; x<stmt->numlabels; x++) mysprintf(var, "%s: ", get_labelname(stmt->labelset[x]->labelnum)); } mysprintf(var, "%s ", get_opname(stmt->opcode)); switch(stmt->opcode) { case CMD_DEFFN: list_deffn(var, stmt); break; case CMD_REM: mysprintf(var, "%s", stmt->metalist[1].stringarg); break; case CMD_IF: list_if(var, stmt); break; case CMD_LET: list_let(var, stmt); break; case CMD_SETESC: case CMD_SETERR: case CMD_GOTO: case CMD_GOSUB: if (stmt->metalist[1].operation == LABELREF) mysprintf(var, "%s", get_labelname(stmt->metalist[1].shortarg)); else mysprintf(var, "%.5d", stmt->metalist[1].shortarg); break; case CMD_FOR: list_for(var, stmt); break; case CMD_NEXT: mysprintf(var, "%s", get_symname(stmt->metalist[1].shortarg)); break; case CMD_LIST: case CMD_DELETE: if (stmt->metapos == 1) mysprintf(var, "00001, 65534"); else if (stmt->metapos == 2) mysprintf(var, "%.5d", flt2int(stmt->metalist[1].floatarg)); else mysprintf(var, "%.5d, %.5d", flt2int(stmt->metalist[1].floatarg), flt2int(stmt->metalist[3].floatarg)); break; case CMD_RETURN: case CMD_WEND: case CMD_RETRY: case CMD_SETERRON: case CMD_SETERROFF: break; case CMD_ON: list_on(var, stmt); break; default: if (stmt->metapos == 1) break; simplifylist(var, stmt, 0, stmt->metapos); mysprintf(var, "%s", listpop()); break; } }