/** * 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); }
/** * 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); }
/** * Close the handle */ void CHandle::close() { if(this) { if(res < RES_CLOSED) { if( client.drv_index > INALID_DRV_INDX && !(client.drv_index & 3)) { //this is a task handle... //1. Cancel any operation in progress.. tsk_cancel(); //2. Close tsk_hcontrol(DCR_CLOSE); //3. Deallocate the signal CURRENT_TASK->aloc_sig &= ~signal; drv_index = INALID_DRV_INDX; res = RES_CLOSED; } else { //this is driver îr callback handle... if( res < RES_BUSY ) { hcontrol(DCR_CLOSE); drv_index = INALID_DRV_INDX; res = RES_CLOSED; } } } } }