/*! Returns the socket's protocol family, which is one of \c Unknown, \c IPv4, or \c IPv6. Q3SocketDevice either creates a socket with a well known protocol family or it uses an already existing socket. In the first case, this function returns the protocol family it was constructed with. In the second case, it tries to determine the protocol family of the socket; if this fails, it returns \c Unknown. \sa Protocol setSocket() */ Q3SocketDevice::Protocol Q3SocketDevice::protocol() const { if ( d->protocol == Unknown ) d->protocol = getProtocol(); return d->protocol; }
int WebSocketTcpServer::__callback_websocket__(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { if ( nullptr == m_context ) return 0; WebSocketTcpServer *server = static_cast<WebSocketTcpServer*>(lws_context_user(m_context)); if ( server->m_run_thread == false ) return 0; switch ( reason ) { case LWS_CALLBACK_WSI_CREATE: break; case LWS_CALLBACK_ESTABLISHED: { LOG_DEBUG("LWS_CALLBACK_ESTABLISHED \n"); string protocol = server->__getProtocolFromHeader__(wsi); LOG_DEBUG("user : %s \n", protocol.data()); int client_fd = lws_get_socket_fd(wsi); struct sockaddr_in clientaddr; socklen_t peeraddrlen = sizeof(clientaddr); if ( 0 > getpeername(client_fd, (struct sockaddr*)&clientaddr, &peeraddrlen) ) { perror("getpeername error "); } size_t client_id = server->addClientInfo(ClientInfoPtr( new WebSocketTcpClientInfo(client_fd, &clientaddr, wsi, protocol))); NOTIFY_CLIENT_CONNECTED(server->m_server_id, client_id); } break; case LWS_CALLBACK_RECEIVE: LOG_DEBUG("LWS_CALLBACK_RECEIVE \n"); { LOG_DEBUG("received data: %s\n", (char *) in); int client_fd = lws_get_socket_fd(wsi); auto client = static_pointer_cast<WebSocketTcpClientInfo>(server->findClientInfo(client_fd)); NOTIFY_SERVER_RECEIVED_FROM_PROTOCOL( server->m_server_id, client->getClientId(), (char*)in, len, client->getProtocol()); } break; case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: LOG_DEBUG("LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION \n"); break; case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: LOG_DEBUG("LWS_CALLBACK_WS_PEER_INITIATED_CLOSE \n"); break; case LWS_CALLBACK_CLOSED: LOG_DEBUG("LWS_CALLBACK_CLOSED \n"); { int client_fd = lws_get_socket_fd(wsi); size_t client_id = server->removeClientInfo(client_fd); NOTIFY_CLIENT_DISCONNECTED(server->m_server_id, client_id); } break; case LWS_CALLBACK_PROTOCOL_INIT: LOG_DEBUG("LWS_CALLBACK_PROTOCOL_INIT \n"); break; case LWS_CALLBACK_PROTOCOL_DESTROY: LOG_DEBUG("LWS_CALLBACK_PROTOCOL_DESTROY \n"); break; case LWS_CALLBACK_GET_THREAD_ID: /* to be silent */ break; case LWS_CALLBACK_DEL_POLL_FD: LOG_DEBUG("LWS_CALLBACK_DEL_POLL_FD \n"); { int client_fd = lws_get_socket_fd(wsi); size_t client_id = server->removeClientInfo(client_fd); NOTIFY_CLIENT_DISCONNECTED(server->m_server_id, client_id); } break; default: LOG_WARNING("Unhandled callback reason [%d] \n", reason); break; } return 0; }
void KeyExchangeProtocol::add(std::map<int, KeyExchangeProtocol*> &list) { list[getProtocol()] = this; }
KeyExchangeSession* KeyExchangeProtocol::createSession(const dtn::data::EID &peer, unsigned int uniqueId) { return new KeyExchangeSession(getProtocol(), peer, uniqueId, NULL); }
void AtDrv::startClient(uint8_t sock, uint32_t ipAddress, uint16_t port, uint8_t protMode) { // if we enable CHECK_TCP_STATE feature, always call reConnect(), or we won't get right // tcp port status, since we disable tcp auto reconnect. #ifndef CHECK_TCP_STATE bool needReConn = false; #else bool needReConn = true; #endif int curMode; uint32_t curIp; uint8_t curProtocol; uint16_t curPort; uint16_t curLocalPort; uint32_t curTimeout; bool curTcpAuto; // clear uart buffer first stopClient(sock); if(!isAtMode()) { if(!switchToAtMode()) { INFO1("Can't switch to at mode"); goto end; } } if(!getMode(sock, &curMode) || curMode != MODE_CLIENT) { needReConn = true; INFO1("curMode != MODE_CLIENT"); if(!setMode(sock, MODE_CLIENT)) { INFO1("Can't set mode"); goto end; } } if(!getRemoteIp(sock, (uint8_t *)&curIp) || curIp != ipAddress) { needReConn = true; INFO1("curIp != ipAddress"); if(!setRemoteIp(sock, ipAddress)) { INFO1("Can't set ip"); goto end; } } if(!getProtocol(sock, &curProtocol) || curProtocol != protMode) { needReConn = true; INFO1("curProtocol != protMode"); if(!setProtocol(sock, protMode)) { INFO1("Can't set protocol"); goto end; } } if(!getRemotePort(sock, &curPort) || curPort != port) { needReConn = true; INFO1("curPort != port"); if(!setPort(sock, port)) { INFO1("Can't set port"); goto end; } } if(!getTcpAuto(sock, &curTcpAuto) || curTcpAuto != false) { needReConn = true; INFO1("curTcpAuto != false"); if(!setTcpAuto(sock, false)) { INFO1("Can't set tcp auto"); goto end; } } if(!getLocalPort(sock, &curLocalPort) || curLocalPort != localSockPort[sock]) { needReConn = true; INFO1("curLocalPort != port"); if(!setLocalPort(sock, localSockPort[sock])) { INFO1("Can't set port"); goto end; } } if(needReConn) { if(!reConnect()) { INFO1("Can't reconnect"); goto end; } } sockPort[sock] = localSockPort[sock]; sockConnected[sock] = true; end: return; }
void AtDrv::startClient(uint8_t sock, const char *host, uint16_t port, uint8_t protMode) { // if we enable CHECK_TCP_STATE feature, always call reConnect(), or we won't get right // tcp port status, since we disable tcp auto reconnect. #ifndef CHECK_TCP_STATE bool needReConn = false; #else bool needReConn = true; #endif int curMode; char curHostBuf[MAX_HOST_NAME_BUF_SIZE]; uint8_t curProtocol; uint16_t curPort; uint16_t curLocalPort; uint32_t curTimeout; bool curTcpAuto; // clear uart buffer first stopClient(sock); if(!isAtMode()) { if(!switchToAtMode()) { INFO1("Can't switch to at mode"); goto end; } } if(!getMode(sock, &curMode) || curMode != MODE_CLIENT) { needReConn = true; INFO1("curMode != MODE_CLIENT"); if(!setMode(sock, MODE_CLIENT)) { INFO1("Can't set mode"); goto end; } } if(!getRemoteHost(sock, curHostBuf) || (strcmp(curHostBuf, host) != 0)) { needReConn = true; INFO1("curHostBuf != host"); if(!setRemoteHost(sock, host)) { INFO1("Can't set host"); goto end; } } if(!getProtocol(sock, &curProtocol) || curProtocol != protMode) { needReConn = true; INFO1("curProtocol != protMode"); if(!setProtocol(sock, protMode)) { INFO1("Can't set protocol"); goto end; } } if(!getRemotePort(sock, &curPort) || curPort != port) { needReConn = true; INFO1("curPort != port"); if(!setPort(sock, port)) { INFO1("Can't set port"); goto end; } } if(!getTcpAuto(sock, &curTcpAuto) || curTcpAuto != false) { needReConn = true; INFO1("curTcpAuto != false"); if(!setTcpAuto(sock, false)) { INFO1("Can't set tcp auto"); goto end; } } if(!getLocalPort(sock, &curLocalPort) || curLocalPort != localSockPort[sock]) { needReConn = true; INFO1("curLocalPort != port"); if(!setLocalPort(sock, localSockPort[sock])) { INFO1("Can't set port"); goto end; } } if(needReConn) { if(!reConnect()) { INFO1("Can't reconnect"); goto end; } } sockPort[sock] = localSockPort[sock]; sockConnected[sock] = true; end: return; }
void initWorkerProcess(struct workerProcess *worker, char *worker_name) { struct configuration *conf; int i; struct app *app; struct moduleAttr *module; if (Server.stat_fd != 0) close(Server.stat_fd); setProctitle(worker_name); worker->pid = getpid(); worker->ppid = getppid(); worker->alive = 1; worker->start_time = Server.cron_time; worker->worker = spotWorker(worker_name); worker->master_stat_fd = 0; ASSERT(worker->worker); worker->stats = NULL; initWorkerSignals(); worker->center = eventcenterInit(WHEAT_CLIENT_MAX); if (!worker->center) { wheatLog(WHEAT_WARNING, "eventcenter_init failed"); halt(1); } if (createEvent(worker->center, Server.ipfd, EVENT_READABLE, acceptClient, NULL) == WHEAT_WRONG) { wheatLog(WHEAT_WARNING, "createEvent failed"); halt(1); } // It may nonblock after fork??? if (wheatNonBlock(Server.neterr, Server.ipfd) == NET_WRONG) { wheatLog(WHEAT_WARNING, "Set nonblock %d failed: %s", Server.ipfd, Server.neterr); halt(1); } module = NULL; conf = getConfiguration("protocol"); if (conf->target.ptr) { module = getModule(PROTOCOL, conf->target.ptr); if (module && getProtocol(module)->initProtocol() == WHEAT_WRONG) { wheatLog(WHEAT_WARNING, "init protocol failed"); halt(1); } } if (!module) { wheatLog(WHEAT_WARNING, "find protocol %s failed", conf->target.ptr); halt(1); } worker->protocol = getProtocol(module); StatTotalClient = getStatItemByName("Total client"); gettimeofday(&Server.cron_time, NULL); if (worker->worker->setup) worker->worker->setup(); WorkerProcess->refresh_time = Server.cron_time.tv_sec; FreeClients = createAndFillPool(); Clients = createList(); StatBufferSize = getStatItemByName("Max buffer size"); StatTotalRequest = getStatItemByName("Total request"); StatFailedRequest = getStatItemByName("Total failed request"); StatRunTime = getStatItemByName("Worker run time"); worker->apps = arrayCreate(sizeof(struct app*), 3); if (!worker->apps) { wheatLog(WHEAT_WARNING, "array create failed"); halt(1); } getAppsByProtocol(worker->apps, worker->protocol); for (i = 0; i < narray(worker->apps); i++) { app = *(struct app**)arrayIndex(worker->apps, i); if (app->initApp(worker->protocol) == WHEAT_WRONG) { wheatLog(WHEAT_WARNING, "init app failed %s", getModuleName(APP, app)); halt(1); } } sendStatPacket(WorkerProcess); }
void AtDrv::startServer(uint8_t sock, uint16_t port, uint8_t protMode) { bool needReConn = false; int curMode; uint8_t curProtocol; uint16_t curPort; uint32_t curTimeout; if(!isAtMode()) { if(!switchToAtMode()) { INFO1("Can't switch to at mode"); goto end; } } if(!getMode(sock, &curMode) || curMode != MODE_SERVER) { needReConn = true; INFO1("curMode != MODE_SERVER"); if(!setMode(sock, MODE_SERVER)) { INFO1("Can't set mode"); goto end; } } if(!getProtocol(sock, &curProtocol) || curProtocol != protMode) { needReConn = true; INFO1("curProtocol != protMode"); if(!setProtocol(sock, protMode)) { INFO1("Can't set protocol"); goto end; } } if(!getRemotePort(sock, &curPort) || curPort != port) { needReConn = true; INFO1("curPort != port"); if(!setPort(sock, port)) { INFO1("Can't set port"); goto end; } } if(!getNetworkTimeout(sock, &curTimeout) || curTimeout != 0) { needReConn = true; INFO1("curTimeout != 0"); if(!setNetworkTimeout(sock, 0)) { INFO1("Can't set timeout"); goto end; } } if(needReConn) { if(!reConnect()) { INFO1("Can't reconnect"); goto end; } } sockPort[sock] = port; sockConnected[sock] = true; end: return; }
/*! ** @brief Xsocket implemention of the standard getsockopt function. ** ** Xgetsockopt is used to retrieve the settings of the underlying Xsocket ** in the Click layer. It does not access the settings of the actual ** socket passed in which is used by the API to communicate with Click. ** ** Supported Options: ** \n XOPT_HLIM Retrieves the 'hop limit' element of the XIA header as an integer value ** \n XOPT_NEXT_PROTO Gets the next proto field in the XIA header ** \n SO_TYPE Returns the type of socket (SOCK_STREAM, etc...) ** ** @param sockfd The control socket ** @param optname The socket option to set (currently must be IP_TTL) ** @param optval A pointer to the value to retrieve ** @param optlen A pointer to the length of optval. On input this ** should be set to the length of the data passed in. If optlen is not ** large enough, Xgetsockopt will set it to the size needed and return ** with errno set to EINVAL. If larger than needed, Xgetsockopt will set ** it to the actual length of the returned data. ** ** @returns 0 on success ** @returns -1 on error with errno set ** @returns errno values: ** @returns EBADF: The socket descriptor is invalid ** @returns EINVAL: Either optval is null, or optlen is invalid, or optval is out of range ** @returns ENOPROTOOPT: the specified optname is not recognized */ int Xgetsockopt(int sockfd, int optname, void *optval, socklen_t *optlen) { int rc = 0; if (getSocketType(sockfd) == XSOCK_INVALID) { errno = EBADF; return -1; } if (!optval || !optlen) { errno = EINVAL; return -1; } switch (optname) { // get these values from click ****************************** case XOPT_HLIM: case XOPT_NEXT_PROTO: case XOPT_ERROR_PEEK: case SO_ACCEPTCONN: case SO_ERROR: rc = ssoGetInt(sockfd, optname, (int *)optval, optlen); break; case SO_DEBUG: // stored in the API & in click, return from the API rc = ssoGetParam(getDebug(sockfd), optval, optlen); break; case SO_DOMAIN: // FIXME: conver this to AF_INET in wrapper rc = ssoGetParam(AF_XIA, optval, optlen); break; case SO_PROTOCOL: rc = ssoGetParam(getProtocol(sockfd), optval, optlen); break; case SO_TYPE: ssoGetParam(getSocketType(sockfd), optval, optlen); break; // SHOIULD IMPLEMENT! *************************************** // FIXME: implement these! case SO_SNDBUF: case SO_RCVBUF: case SO_SNDTIMEO: case SO_RCVTIMEO: case SO_LINGER: LOGF("Uh Oh, we need support for %s in XIA\n", optValue(optname)); rc = -1; break; // FIXME: MAY NEED ****************************************** case SO_BROADCAST: // FIXME: can we just go ahead and mark this as success? // or do we need to do something to check to see if bradcast DAGS are allowed on this socket? if (getSocketType(sockfd) == SOCK_DGRAM) { rc = -1; errno = ENOPROTOOPT; break; } // else silently ignore break; case SO_REUSEADDR: // just say it's not enabled ssoGetParam(0, optval, optlen); rc = 0; break; case SO_RCVLOWAT: case SO_SNDLOWAT: // Probably will never need to support this // return the default linux value of 1 rc = ssoGetParam(1, optval, optlen); break; // CAN SAFELY IGNORE **************************************** case SO_KEEPALIVE: // we don't have keepalive so lie and say we do ssoGetParam(0, optval, optlen); rc = 0; break; case SO_BSDCOMPAT: // safe to mark as unsupported, being phased out on linux anyway errno = ENOPROTOOPT; rc = -1; break; // NOT SUPPORTED/DOESN"T MAKE SENSE IN XIA ****************** case SO_BINDTODEVICE: // should we support this one when we get multihoming? case SO_DONTROUTE: case SO_MARK: case SO_OOBINLINE: case SO_PASSCRED: case SO_PEERCRED: case SO_PRIORITY: case SO_RCVBUFFORCE: case SO_SNDBUFFORCE: case SO_TIMESTAMP: default: // return generic error LOGF("Option %s not supported in XIA\n", optValue(optname)); errno = ENOPROTOOPT; rc = -1; } return rc; }
// ICommandRule implemntation boost::shared_ptr< xplcore::CXplMessage > CRemoteBasic::createXplCommand(boost::shared_ptr<const yApi::IDeviceCommand> & commandData, const std::string & rfxAddress, const shared::CDataContainer & innerDetails) { //////////////////////////// // create the message //////////////////////////// //check the device address is valid auto device = commandData->getDevice(); //create the message auto newMessage(boost::make_shared<xplcore::CXplMessage>()); //the AC.BSACI XplMessage if a xpl-trig newMessage->setTypeIdentifier(xplcore::CXplMessage::kXplCommand); //set hop to 1 newMessage->setHop(1); //set the target (rfxcom-lan-<hax mac address>) newMessage->setTarget(xplcore::CXplActor::parse(rfxAddress)); //set the ac.basic newMessage->setMessageSchemaIdentifier(getProtocol()); //set the device address and unit (parse from argetDevice.Address) newMessage->addToBody(m_keywordDevice, device); int code; if (boost::istarts_with(device, ERemoteType::kPC.toString())) { data::CRemotePC remote(commandData->getBody()); code = remote.get().toInteger(); } else if (boost::istarts_with(device, ERemoteType::kMedion.toString())) { data::CRemoteMedion remote(commandData->getBody()); code = remote.get().toInteger(); } else if (boost::istarts_with(device, ERemoteType::kAtiPlus.toString())) { throw shared::exception::CNotSupported("ATI Wonder Plus commands are not supported"); /* uncomment this code to allow sending command data::CRemoteAtiWonderPlus remote("atiplusremote"); remote.set(commandData->getBody()); code = remote.get().toInteger(); */ } else if (boost::istarts_with(device, ERemoteType::kAti.toString())) { data::CRemoteAtiWonder remote(commandData->getBody()); code = remote.get().toInteger(); } else throw shared::exception::CNotSupported("remote type not supported"); //convert int to hex string std::stringstream stream; stream << "0x" << std::hex << code; auto hexNumber(stream.str()); newMessage->addToBody(m_keywordKeys, hexNumber); return newMessage; }
void net::URL::setPort(int port) { mPort = port; if (mPort < 0) mPort = net::getStandardPort(getProtocol()); }
std::string net::URL::toString() const { return net::urlJoin(getProtocol(), getHost(), getPort(), getPath(), getQuery(), getFragment()); }
void CSyncSource::processServerResponse_ver3(CJSONArrayIterator& oJsonArr) { PROF_START("Data1"); int nVersion = 0; if ( !oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("version") ) { nVersion = oJsonArr.getCurItem().getInt("version"); oJsonArr.next(); } if ( nVersion != getProtocol().getVersion() ) { LOG(ERROR) + "Sync server send data with incompatible version. Client version: " + convertToStringA(getProtocol().getVersion()) + "; Server response version: " + convertToStringA(nVersion) + ". Source name: " + getName(); getSync().stopSync(); m_nErrCode = RhoAppAdapter.ERR_SYNCVERSION; return; } if ( !oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("token")) { processToken(oJsonArr.getCurItem().getUInt64("token")); oJsonArr.next(); } if ( !oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("source") ) { //skip it. it uses in search only oJsonArr.next(); } if ( !oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("count") ) { setCurPageCount(oJsonArr.getCurItem().getInt("count")); oJsonArr.next(); } if ( !oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("refresh_time") ) { setRefreshTime(oJsonArr.getCurItem().getInt("refresh_time")); oJsonArr.next(); } if ( !oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("progress_count") ) { //TODO: progress_count //setTotalCount(oJsonArr.getCurItem().getInt("progress_count")); oJsonArr.next(); } if ( !oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("total_count") ) { setTotalCount(oJsonArr.getCurItem().getInt("total_count")); oJsonArr.next(); } //if ( getServerObjectsCount() == 0 ) // getNotify().fireSyncNotification(this, false, RhoAppAdapter.ERR_NONE, ""); if ( getToken() == 0 ) { //oo conflicts getDB().executeSQL("DELETE FROM changed_values where source_id=? and sent>=3", getID() ); // } LOG(INFO) + "Got " + getCurPageCount() + "(Processed: " + getServerObjectsCount() + ") records of " + getTotalCount() + " from server. Source: " + getName() + ". Version: " + nVersion; PROF_STOP("Data1"); if ( !oJsonArr.isEnd() && getSync().isContinueSync() ) { CJSONEntry oCmds = oJsonArr.getCurItem(); PROF_START("Data"); //TODO: use isUIWaitDB inside processSyncCommand // if ( getDB().isUIWaitDB() ) // { // LOG(INFO) + "Commit transaction because of UI request."; // getDB().endTransaction(); // getDB().startTransaction(); // } if ( oCmds.hasName("schema-changed") ) { getSync().setSchemaChanged(true); }else if ( oCmds.hasName("source-error") ) { CJSONEntry errSrc = oCmds.getEntry("source-error"); CJSONStructIterator errIter(errSrc); for( ; !errIter.isEnd(); errIter.next() ) { m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER; m_strError = errIter.getCurValue().getString("message"); m_strErrorType = errIter.getCurKey(); } }else if ( oCmds.hasName("search-error") ) { CJSONEntry errSrc = oCmds.getEntry("search-error"); CJSONStructIterator errIter(errSrc); for( ; !errIter.isEnd(); errIter.next() ) { m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER; m_strError = errIter.getCurValue().getString("message"); m_strErrorType = errIter.getCurKey(); } }else if ( oCmds.hasName("create-error") ) { m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER; m_strErrorType = "create-error"; }else if ( oCmds.hasName("update-error") ) { m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER; m_strErrorType = "update-error"; }else if ( oCmds.hasName("delete-error") ) { m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER; m_strErrorType = "delete-error"; }else { getDB().startTransaction(); if (getSync().getSourceOptions().getBoolProperty(getID(), "pass_through")) { if ( m_bSchemaSource ) getDB().executeSQL( (String("DELETE FROM ") + getName()).c_str() ); else getDB().executeSQL("DELETE FROM object_values WHERE source_id=?", getID() ); } if ( oCmds.hasName("metadata") && getSync().isContinueSync() ) { String strMetadata = oCmds.getString("metadata"); getDB().executeSQL("UPDATE sources SET metadata=? WHERE source_id=?", strMetadata, getID() ); } if ( oCmds.hasName("links") && getSync().isContinueSync() ) processSyncCommand("links", oCmds.getEntry("links") ); if ( oCmds.hasName("delete") && getSync().isContinueSync() ) processSyncCommand("delete", oCmds.getEntry("delete") ); if ( oCmds.hasName("insert") && getSync().isContinueSync() ) processSyncCommand("insert", oCmds.getEntry("insert") ); PROF_STOP("Data"); PROF_START("DB"); getDB().endTransaction(); PROF_STOP("DB"); getNotify().fireObjectsNotification(); } } PROF_START("Data1"); if ( getCurPageCount() > 0 ) getNotify().fireSyncNotification(this, false, RhoAppAdapter.ERR_NONE, ""); PROF_STOP("Data1"); }
int midParseStruct(int identifier,int fd, char *data, int len) { int recvlen; int cmd = 0; //char *actualdata = NULL; char actualdata[1024] = {0}; char out[2048] = {0}; int vallen = 0; int status = 0; int ret = 0; char senddata[1024] = {0}; int totallen = 0; int web_ret = SERVER_RET_OK; int need_send = 0; recvlen = recv(fd, data, len, 0); if(recvlen < 0) { DEBUG(DL_ERROR, "recv failed,errno = %d,error message:%s \n", errno, strerror(errno)); //return -1; web_ret = SERVER_RET_INVAID_PARM_LEN; status = -1; goto EXIT; } vallen = len - sizeof(int); //actualdata = (char *)malloc(vallen); memcpy(&cmd, data, sizeof(int)); memcpy(actualdata, data + sizeof(int), len - sizeof(int)); //PRINTF("cmd = 0x%04x,vallen=%d=len=%d\n", cmd, vallen, len); switch(cmd) { case MSG_SETOUTPUTVIDEOINFO: PRINTF("cmd = 0x%04x is MSG_SETOUTPUTVIDEOINFO.\n", cmd); setOutputVideoInfo(identifier,fd, cmd, actualdata, vallen); break; case MSG_GETOUTPUTVIDEOINFO: PRINTF("cmd = 0x%04x is MSG_GETOUTPUTVIDEOINFO.\n", cmd); getOutputVideoInfo(identifier,fd, cmd, actualdata, vallen); break; case MSG_GETVIDEOPARAM: PRINTF("cmd = 0x%04x is MSG_GETVIDEOPARAM.\n", cmd); GetVideoParam_1260(identifier,fd, cmd, actualdata, vallen); break; case MSG_GETAUDIOPARAM: PRINTF("cmd = 0x%04x is MSG_GETAUDIOPARAM.\n", cmd); GetAudioParam_1260(identifier,fd, cmd, actualdata, vallen); break; case MSG_GETSYSPARAM: PRINTF("cmd = 0x%04x is MSG_GETSYSPARAM.\n", cmd); getSysParams(identifier,fd, cmd, actualdata, vallen); break; case MSG_SETSYSPARAM: PRINTF("cmd = 0x%04x is MSG_SETSYSPARAM.\n", cmd); setSysParams(identifier,fd, cmd, actualdata, vallen); break; case MSG_SETVIDEOPARAM: PRINTF("cmd = 0x%04x is MSG_SETVIDEOPARAM.\n", cmd); setVideoParam_1260(identifier,fd, cmd, actualdata, vallen); break; case MSG_SETAUDIOPARAM: PRINTF("cmd = 0x%04x is MSG_SETAUDIOPARAM.\n", cmd); setAudioParam_1260(identifier,fd, cmd, actualdata, vallen); break; case MSG_SYNCTIME: PRINTF("cmd = 0x%04x is MSG_SYNCTIME.\n", cmd); syncTime(identifier,fd, cmd, actualdata, vallen); break; case MSG_GET_PROTOCOL: PRINTF("cmd = 0x%04x is MSG_GET_PROTOCOL.\n", cmd); getProtocol(identifier,fd, cmd, actualdata, vallen); break; case MSG_GET_LOGOINFO: PRINTF("cmd = 0x%04x is MSG_GET_LOGOINFO.\n", cmd); ret = getLogoInfo(actualdata, vallen, out); if(ret == 0) { web_ret = SERVER_RET_INVAID_PARM_VALUE; } need_send = 1; break; case MSG_SET_LOGOINFO: PRINTF("cmd = 0x%04x is MSG_SET_LOGOINFO.\n", cmd); ret = setLogoInfo(actualdata, vallen, out); if(ret == -1) { web_ret = SERVER_RET_INVAID_PARM_LEN; } if(ret == -2) { web_ret = SERVER_RET_INVAID_PARM_VALUE; } need_send = 1; break; case MSG_GET_TEXTINFO: PRINTF("cmd = 0x%04x is MSG_GET_TEXTINFO.\n", cmd); ret = getTextInfo(actualdata, vallen, out); if(ret == -1) { web_ret = SERVER_RET_INVAID_PARM_VALUE; } need_send = 1; break; case MSG_SET_TEXTINFO: PRINTF("cmd = 0x%04x is MSG_SET_TEXTINFO.\n", cmd); ret = setTextInfo(actualdata, vallen, out); need_send = 1; break; case MSG_GETENCODETIME: PRINTF("cmd = 0x%04x is MSG_GETENCODETIME.\n", cmd); getEncodeTime(identifier,fd, cmd, actualdata, vallen); break; case MSG_RTSP_GET_GINFO: case MSG_RTSP_SET_GINFO: case MSG_RTSP_GET_CINFO: case MSG_RTSP_SET_CINFO: case MSG_RTSP_ADD_SERVER: case MSG_MULT_ADD_SERVER: case MSG_MULT_GET_CINFO: case MSG_MULT_SET_CINFO: case MSG_RTSP_SET_STATUS: case MSG_MULT_SET_STATUS: case MSG_MULT_DEL_SERVER: PRINTF("cmd = 0x%04x is MSG_STREAMOUTPUT\n", cmd); app_web_stream_output_process_struct(identifier,fd, cmd, actualdata, vallen); break; default: PRINTF("UNKOWN cmd %d\n", cmd); break; } if(ret < 0) { web_ret = SERVER_RET_INVAID_PARM_VALUE; } EXIT: if(need_send == 1) { totallen = MSGINFOHEAD + sizeof(cmd) + sizeof(web_ret) + vallen; msgPacket(identifier,senddata, STRING_TYPE, totallen, cmd, web_ret); memcpy(senddata + (totallen - vallen), out, vallen); PRINTF("the cmd =%04x,,the ret=%04x\n", cmd, web_ret); send(fd, senddata, totallen, 0); } // free(actualdata); return status; }
AWE::int32 DatagramProtocol::receiveFrom( Address* pAddress, AWE::int8* pData, AWE::uint32 iMaxLength ) { AWE_CHECK_PTR( pAddress ); AWE_CHECK_PTR( pData ); AWE_CHECK_PTR( m_pSocket ); AWE_ASSERT( m_pSocket->getSocket() != AWE_INVALID_SOCKET ); sockaddr_storage kSS; socklen_t iSSLen( sizeof( kSS ) ); //memset( &kSS, 0, iSSLen ); // http://msdn.microsoft.com/en-us/library/ms740120(VS.85).aspx int iResult( ::recvfrom( m_pSocket->getSocket(), // socket pData, // buffer iMaxLength, // max buffer length 0, // flags (struct sockaddr*)&kSS, // address &iSSLen // address length ) ); if ( iResult < 0 ) { return iResult; } else if ( iResult == 0 ) { // 0 indicates connection closed return iResult;//(AWE::int32)AWE::Socket::Error::ConnectionClosed; } // successfully received data // convert incoming address to something we can use struct sockaddr* pAddr = (struct sockaddr*)&kSS; socklen_t iAddrLength( static_cast< socklen_t >( sizeof( kSS ) ) ); // convert to string char pAddressStr[ NI_MAXHOST ]; char pPortStr[ NI_MAXSERV ]; memset( pAddressStr, 0, NI_MAXHOST ); memset( pPortStr, 0, NI_MAXSERV ); int iResolveResult( ::getnameinfo( pAddr, iAddrLength, pAddressStr, NI_MAXHOST, pPortStr, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV ) ); if ( iResolveResult != 0 ) { return iResolveResult; } int iSock( atoi( pPortStr ) ); // convert string to addrinfo iResolveResult = pAddress->resolve( pAddr->sa_family, getProtocol(), getSocketType(), AI_NUMERICHOST, pAddressStr, iSock ); if ( iResolveResult != 0 ) { return iResolveResult; } // return the amount of data received return iResult; }