int main(int argc, const char *argv[]) { sqlite3 *db; char buff[256] = {0}; char name[100] = {0}; char info[256] = {0}; int *len = NULL; struct socket_data sock_data; char *ptr = sock_data.buff; db = sqlite_init(); sqlite_read(db, &sock_data); printf("id = %d\n",*((int *)ptr)); ptr+=4; len = (int *)ptr; //printf("len of name is :%d \n", *len); ptr+=4; strncpy(name, ptr, *len); printf("name is :%s\n",name); ptr+=*len; printf("price = %d\n",*((int *)ptr)); ptr+=4; len = (int *)ptr; //printf("len of info is :%d \n", *len); ptr+=4; strncpy(info, ptr, *len); printf("info is :%s\n",info); sqlite_close(db); return 0; }
void daccept(int socket_bind) { int socket_client; int ret = 0; fd_set fds_set; int max_fds; struct timeval timeout = {3, 0}; struct socket_data sock_data; int i = 0; int ret_thread = 0; pthread_t tid = 0; pthread_attr_t attr; int err = 0; // 初始化sqlite sock_data.db = sqlite_init(); //初始化attr结构,是创建的线程为分离状态,退出时自动回收线程资源 err = pthread_attr_init(&attr); if( !err ) debug("pthread attr init error"); err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if( !err ) debug("pthread attr set detachstate error"); while(1) { FD_ZERO(&fds_set); FD_SET(socket_bind, &fds_set); max_fds = socket_bind; ret = select(max_fds+1, &fds_set, NULL , NULL, &timeout); //对socketserver进行select,等待链接时不阻塞。 if(ret < 0) { debug("select error\n"); } else if(ret == 0) { } else { debug("select ok\n"); if(FD_ISSET(socket_bind, &fds_set)) { debug("accept one client\n"); if ( (socket_client = accept(socket_bind, NULL, NULL)) < 0) { debug("accept error\n"); exit(1); } else { debug("accept ok\n"); printf("socket:%d\n",socket_client); } pthread_mutex_lock(&mutex);// 加入互斥锁,在线程中再解锁 sock_data.socket = socket_client; ret_thread = pthread_create(&tid, &attr,(void *)thread_handler, &sock_data); if(ret_thread != 0) { debug("pthread create error\n"); } else { debug("pthread create ok"); } /* 新建线程,与client通信 pthread_creat(); //当有连接时,新建线程 * * * */ } } //if ( (sock_client = accept(sock_server, NULL, NULL)) < 0) { //printf("accept error\n"); //exit(1); //} else { //printf("accept ok\n"); //} // //pthread_cancle(); //等待线程结束,也不能阻塞。 } sqlite_close(sock_data.db); }
int main(int argc, char* argv[]) { sqlite3 *db; char *zErrMsg = 0; int rc; LPCWSTR dbName = _T(DBNAME); char dbNameChar[] = DBNAME; sqlite_init(); //Delete the db if it exists if (FileExists(dbName)) { fwprintf(fstream,L"DB File exists so deleting it.\r\n"); DeleteFile(dbName); } rc = sqlite3_open(dbNameChar, &db); if (rc) { fwprintf(fstream, L"Error: Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 1; } fwprintf(fstream,L"Opened the DB\r\n"); rc = sqlite3_exec(db, "CREATE TABLE tblStuff ( id INTEGER PRIMARY KEY , name, value INTEGER )", callback, 0, &zErrMsg); fwprintf(fstream,L"Created a table\r\n"); //Hint: Insert NULL for id to autoincrement as its INTEGER PRIMARY KEY rc = sqlite3_exec(db, "INSERT INTO tblStuff Values (NULL,'Temperature1',23)", callback, 0, &zErrMsg); rc = sqlite3_exec(db, "INSERT INTO tblStuff Values (NULL,'Temperature2',67)", callback, 0, &zErrMsg); rc = sqlite3_exec(db, "INSERT INTO tblStuff Values (NULL,'Humidity1',98)", callback, 0, &zErrMsg); fwprintf(fstream,L"Added some records:\r\n"); rc = sqlite3_exec(db, "SELECT * FROM tblStuff", callback, 0, &zErrMsg); if (rc != SQLITE_OK) { fwprintf(fstream, L"Error: SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } sqlite3_close(db); fwprintf(fstream,L"Closed the DB\r\n"); //Reopen the db rc = sqlite3_open(dbNameChar, &db); if (rc) { fwprintf(fstream, L"Error: Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 1; } fwprintf(fstream,L"Reopenned the DB\r\n"); fwprintf(fstream,L"Records are still there?:\r\n"); rc = sqlite3_exec(db, "SELECT * FROM tblStuff", callback, 0, &zErrMsg); rc = sqlite3_exec(db, "INSERT INTO tblStuff Values (NULL,'Temperature1',79)", callback, 0, &zErrMsg); rc = sqlite3_exec(db, "INSERT INTO tblStuff Values (NULL,'Temperature2',88)", callback, 0, &zErrMsg); rc = sqlite3_exec(db, "INSERT INTO tblStuff Values (NULL,'Humidity1',70)", callback, 0, &zErrMsg); fwprintf(fstream,L"Added some more records:\r\n"); rc = sqlite3_exec(db, "SELECT * FROM tblStuff", callback, 0, &zErrMsg); rc = sqlite3_exec(db, "UPDATE tblStuff SET name = 'Temperature10' WHERE name = 'Temperature1'", callback, 0, &zErrMsg); rc = sqlite3_exec(db, "DELETE FROM tblStuff WHERE name = 'Humidity1'", callback, 0, &zErrMsg); fwprintf(fstream, L"Updated and deleted some records:\r\n"); rc = sqlite3_exec(db, "SELECT * FROM tblStuff", callback, 0, &zErrMsg); rc = sqlite3_exec(db, "DROP TABLE tblStuff", callback, 0, &zErrMsg); fwprintf(fstream,L"Dropped the table\r\n"); if (rc != SQLITE_OK) { fwprintf(fstream, L"Error: SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } sqlite3_close(db); fwprintf(fstream,L"Closed the DB\r\n"); return 0; }
/* 考虑到升级是个非常重要但又较少依赖其他模块的功能,因此即使大部分模块初始化失败,也一样要继续运行,只要组播功能正常即可。 */ void *main_thread() { DEBUG("main thread start...\n"); compile_timeprint(); _wLBM_zyzdmb(13578642); smarthome_gw_sn_init(); if(-1==setting_init()){ DEBUG("setting init failed\n"); //return NULL; } if(-1==push_decoder_buf_init()){ DEBUG("push decoder buf init failed\n"); //return NULL; } chanFilterInit(); smc_init(); if(-1==sqlite_init()){ DEBUG("sqlite init failed\n"); //return NULL; } setting_init_with_database(); maintenance_thread_init(); if(-1==xmlparser_init()){ DEBUG("xmlparser init failed\n"); //return NULL; } // return parse_xml("pushroot/pushinfo/1/ProductDesc.xml", PRODUCTDESC_XML, NULL); if(0!=drm_init()){ DEBUG("drm init failed\n"); //return NULL; } upgrade_info_init(); // 根据首次开机标记"/data/data/com.dbstar/files/flag"决定是否要重置国电网关序列号 smarthome_sn_init_when_network_init(); #if 0 /* 慎用:只有在需要清理已有授权、重新接收授权时使用,正式版本不能调用。 */ DEBUG("\n\nWarning: you call function CDCASTB_FormatBuffer, it is an unnormal action\n\n\n"); CDCASTB_FormatBuffer(); #endif if(-1==mid_push_init(PUSH_CONF)){ DEBUG("push model init with \"%s\" failed\n", PUSH_CONF); //return NULL; } if(-1==igmp_init()){ DEBUG("igmp init failed\n"); //return NULL; } if(-1==softdvb_init()){ DEBUG("dvb init with failed\n"); //return NULL; } smartlife_connect_init(); DEBUG("OK ================================ OK\n"); msg_send2_UI(STATUS_DVBPUSH_INIT_SUCCESS, NULL, 0); int main_running = 1; while(1==main_running) { pthread_mutex_lock(&mtx_main); /* 需要本线程先运行到这里,再在其他非父线程中执行pthread_cond_signal(&cond_push_monitor)才能生效。 */ pthread_cond_wait(&cond_main,&mtx_main); DEBUG("main thread is closed by external call\n"); main_running = 0; pthread_mutex_unlock(&mtx_main); } DEBUG("exit from main thread\n"); return NULL; }
/* 功能: 程序开始时初始化各个模块,启动相关的线程,instruction线程用于处理具体指令,timing线程管理定时器 返回: 0——成功;-1——失败 */ int global_init() { int ret = -1; if(-1==dir_exist_ensure(WORKSPACE_SURFIX)) return -1; setting_init(); if(-1==dir_exist_ensure(FIFO_DIR)) return -1; if(-1==socket_init()){ DEBUG("socket module init failed\n"); return -1; } if(-1==sqlite_init()){ DEBUG("sqlite module init failed\n"); return -1; } if(-1==timing_init()){ DEBUG("timer module init failed\n"); return -1; } if(-1==equipment_init()){ DEBUG("equipment array init failed\n"); return -1; } if(-1==instruction_init()){ DEBUG("instruction module init failed\n"); return -1; } if(-1==serial_int()){ DEBUG("serial module init failed\n"); return -1; } // only for database testing //DEBUG("getGlobalPara(version)=%d\n", getGlobalPara("version")); //~~~~~~~~~~create thread~~~~~~~~~~// typedef void*(*format)(void *); ///define function pointer of "void*(name)(void*)" pthread_t l_socketlinkthread; /// the file description of thread #if 0 pthread_attr_t l_attrthread; ///the attribute of thread size_t l_stacksize=1048576*1; ///set thread stack size ///start thread. pthread_attr_init(&l_attrthread); ///initialize attribute pthread_attr_setscope(&l_attrthread,PTHREAD_SCOPE_SYSTEM); ///set attribute binding pthread_attr_setdetachstate(&l_attrthread,PTHREAD_CREATE_DETACHED); ///set attribute detached pthread_attr_setstacksize(&l_attrthread,l_stacksize); ret=pthread_create(&l_socketlinkthread,&l_attrthread,(format)socketHandler,NULL); #else /* use default attr */ ret = pthread_create(&l_socketlinkthread,NULL,(format)instruction_mainloop,NULL); #endif if(0!=ret) { ERROROUT("thread instruction_mainloop create failed!"); return -1; } pthread_t timing_thread_id; ret = pthread_create(&timing_thread_id, NULL, (void *)timing_mainloop, NULL); if(0!=ret){ ERROROUT("thread timing_mainloop create failed\n"); } return 0; }