/** * Initiate a connection to a remote port and address with timeout * @param ip_adr * @param port * @param timeout * @return */ NET_CODE CSocket::connect(const char* ip_adr, unsigned int port, unsigned int timeout) { NET_CODE result = NET_IDLE; if(complete()) { src.as_charptr = (char*) ip_adr; dst.as_int = port; set_res_cmd(SOCK_CMD_CONNECT_ADR); if(timeout) { tsk_start_handle(); if(tsk_wait_signal(signal, timeout)) { res &= ~FLG_SIGNALED; if(res == RES_OK) result = NET_OK; else result = error; } else tsk_cancel(); } else { tsk_start_and_wait(); if(res == RES_OK) result = NET_OK; else result = error; } } return (result); }
/** * Start command * @param c * @param ptr * @return */ bool CHandle::tsk_start_command(void * c, void *ptr) { if(!complete()) return (false); //handle is idle and open set_res_cmd(CMD_COMMAND); dst.as_voidptr = ptr; src.as_voidptr = c; tsk_start_handle(); return (true); }
/** * Start write * @param buf * @param l * @return */ bool CHandle::tsk_start_write(const void * buf, unsigned int l) { if(!complete()) return (false); //handle is idle and open len = l; set_res_cmd(CMD_WRITE); src.as_voidptr = (void*)buf; tsk_start_handle(); return (true); }
/** * Start read operation * @param buf * @param l * @return */ bool CHandle::tsk_start_read(void * buf, unsigned int l) { if(!complete()) return (false); //handle is idle and open len = l; set_res_cmd(CMD_READ); dst.as_voidptr = buf; tsk_start_handle(); return (true); }
/** * Resume write operation * @param buf * @param l * @return */ RES_CODE CHandle::tsk_resume_write(const void * buf, unsigned int l) { if(complete()) { //handle is idle and open len = l; set_res_cmd(CMD_WRITE); src.as_voidptr = (void*)buf; tsk_start_handle(); if(tsk_resume_wait_signal(signal)) res &= ~FLG_SIGNALED; else tsk_cancel(); } return (res); }
/** * Time limited locked read * @param buf * @param l * @param time * @return */ RES_CODE CHandle::tsk_read_locked(void * buf, unsigned int l, unsigned int time) { if(complete()) { //handle is idle and open len = l; set_res_cmd((CMD_READ|FLAG_LOCK)); dst.as_voidptr = buf; tsk_start_handle(); if(tsk_wait_signal(signal, time)) res &= ~FLG_SIGNALED; else tsk_cancel(); } return (res); }
/** * Accept a connection request. * @param timeout * @return */ NET_CODE CSocket::accept(CSocket* new_sock, unsigned int timeout) { if(complete()) { dst.as_voidptr = new_sock; set_res_cmd(SOCK_CMD_ACCEPT); tsk_start_handle(); if(tsk_wait_signal(signal, timeout)) { res &= ~FLG_SIGNALED; } else tsk_cancel(); } return (res); }