//#include <server/mb/pg_wchar.h> //#pragma comment(lib,") bool SPostgres::Connect() { if(m_pConn != NULL) DisConnect(); SString sParam = GetParams();//hostaddr=127.0.0.1;port=5432;dbname=znl4;user=postgres;password=123456; SString sTemp,sTempNoPwd; if(SString::GetAttributeValue(sParam,"hostaddr").length() == 0) return false; sTemp.sprintf("hostaddr=%s port=%s dbname=%s user=%s password=%s connect_timeout=0", SString::GetAttributeValue(sParam,"hostaddr").data(), SString::GetAttributeValue(sParam,"port").data(), SString::GetAttributeValue(sParam,"dbname").data(), SString::GetAttributeValue(sParam,"user").data(), SString::GetAttributeValue(sParam,"password").data()); sTempNoPwd.sprintf("hostaddr=%s port=%s dbname=%s user=%s password=%s connect_timeout=0", SString::GetAttributeValue(sParam,"hostaddr").data(), SString::GetAttributeValue(sParam,"port").data(), SString::GetAttributeValue(sParam,"dbname").data(), SString::GetAttributeValue(sParam,"user").data(), "******"); LOGDEBUG("Begin to Connect Postgres:%s",sTempNoPwd.data()); m_pConn = PQconnectdb(sTemp.data()); if (PQstatus(m_pConn) != CONNECTION_OK) { SetStatus(DBERROR); SString err; err.sprintf("Connect to postgres ERROR:%s params:%s",PQerrorMessage(m_pConn),sTempNoPwd.data()); LOGERROR("%s",err.data()); DisConnect(); return false; } LOGDEBUG("Connect to postgres OK params:%s",sTempNoPwd.data()); int ret = PQsetClientEncoding(m_pConn,"GBK"); if(ret < 0) { LOGERROR("设置字符集为GBK时失败!"); } // encoding = PQclientEncoding(m_pConn); // ret = PQsetClientEncoding(m_pConn,"UNICODE"); // encoding = PQclientEncoding(m_pConn); // ret = PQsetClientEncoding(m_pConn,"SQL_ASCII"); // encoding = PQclientEncoding(m_pConn); // ret = PQsetClientEncoding(m_pConn,"GB18030"); // encoding = PQclientEncoding(m_pConn); // ret = PQsetClientEncoding(m_pConn,"UTF8"); // encoding = PQclientEncoding(m_pConn); SetStatus(IDLE); // Execute("RESET CLIENT_ENCODING;"); // SString sql = "insert into substation values('222',1,'新站','亲站','aa','aa','',1,0,0,'')"; // Execute(sql); return true; }
bool NetChannelBase::FlushStream() { bool bError = false; while (m_StreamOut.GetSize() > 0) { int32 nLen = (int32)m_StreamOut.GetSize(); int32 nRet = m_socket.Send((char*)m_StreamOut.GetBufferStart(), nLen); if( nRet < 0 ) { int32 nError = Socket::GetSysError(); if( nError == MY_EWOULDBLOCK || nError == MY_EAGAIN ) break; else if( nError == MY_ECONNRESET ) nRet = 0; else { SockAddr addr; m_socket.GetSockAddr(addr); MyLog::error("[%s:%u] IO Error [%d] when sending data !", addr.GetIP().c_str(), addr.GetPort(), GetID(), nError); DisConnect(); nError = true; break; } } if( nRet == 0 ) { SockAddr addr; m_socket.GetPeerAddr(addr); MyLog::error("NetChannelBase::flushStream() [%s:%u][%u] connection is closed!", addr.GetIP().c_str(), addr.GetPort(), GetID()); DisConnect(); bError = true; break; } m_pMgr->BytesSend().Add(nRet); m_StreamOut.Remove(nRet); m_nTotalSendByte += nRet; if( nRet < nLen ) break; } return !bError; }
bool CDataRedis::KeepConnect(uint32_t timeout) { if (context_!=NULL) return true; struct timeval st_timeout = {0, 800000}; // 1.5 seconds if(timeout) st_timeout.tv_usec = timeout; context_ = redisConnectWithTimeout(host_, port_, st_timeout); if (context_ == NULL || context_->err) { if (context_) { printf("Connection[%s:%u] error: %s.\n",host_, port_,context_->errstr); redisFree(context_); context_ = NULL; } else { printf("Connection error: can't allocate redis context.\n"); } return false; } if(idx_!=0 && !SelectDB(idx_)) { printf("Select db[%u] failed.\n",idx_); return false; } //test, connect redis_server redisReply *reply = (redisReply*)redisCommand(context_, "PING"); if (reply==NULL || reply->type == REDIS_REPLY_ERROR) { if(reply) { printf("PING redis_server, but error occur, errInfo: %s.\n",reply->str); freeReplyObject(reply); } DisConnect(); return false; } freeReplyObject(reply); reply = NULL; return true; }
bool CDataRedis::DeleteKeys(deque<string> &keys) { uint32_t size = keys.size(); assert(size>0); if(!KeepConnect()) return false; char cmd[4096] = {0}; snprintf(cmd,4096,"del"); for(uint32_t i = 0; i < size; i++) { strcat(cmd, " "); strcat(cmd, keys[i].c_str()); } redisReply *reply = (redisReply*)redisCommand(context_, cmd); if (reply==NULL || reply->type == REDIS_REPLY_ERROR) { if(reply) { printf("fail to delete keys, delCommand: %s, error: %s.\n", cmd,reply->str); freeReplyObject(reply); } DisConnect(); return false; } freeReplyObject(reply); reply = NULL; return true; }
bool psNetConnection::Connect(const char *servaddr, int port) { int err; if (IsReady() || server) DisConnect(); //@@@ DEBUG // Debug2(LOG_NET, 0,"psNetConnection::Connect this=%p",this); server = new Connection; server->addr.sin_family = AF_INET; server->addr.sin_port = htons(port); err = GetIPByName (&server->addr, servaddr); if (err) { delete server; server = NULL; return false; } shouldRun = true; thread.AttachNew( new CS::Threading::Thread(this, true) ); if (!thread->IsRunning()) { Error1("Couldn't Start Thread!"); return false; } return true; }
bool NetChannel::FetchBlockFromStreamAndProcess() { const uint32 nStreamSize = m_StreamIn.GetSize(); if(nStreamSize <= sizeof(BlockHeadT)) return false; BlockHeadT blockHead = *((BlockHeadT*)m_StreamIn.GetBufferStart()); uint32 nBlockSize = (blockHead & BLOCK_SIZE_MASK); if( nBlockSize > SOCKET_MY_MAX_DATA_BLOCK_SIZE ) { MyLog::message("NetChannel::fetchBlockFromStreamAndProcess bad blocksize = [%u]", nBlockSize); DisConnect(); return false; } if( nStreamSize < sizeof(blockHead) + nBlockSize ) return false; m_StreamIn.Remove(sizeof(blockHead)); char* pBuffer = (char*)m_StreamIn.GetBufferStart(); m_StreamIn.Remove(nBlockSize); return ParsePacketsInBlock( blockHead, pBuffer, nBlockSize); }
bool CDataRedis::Sremove(const char *key,const set<string> &mumbs) { uint32_t size = mumbs.size(); assert(size>0); if(!KeepConnect()) return false; char cmd[4096] = {0}; snprintf(cmd,4096,"srem %s",key); set<string>::const_iterator iter,end=mumbs.end(); for(iter=mumbs.begin(); iter!=end; ++iter) { strcat(cmd, " "); strcat(cmd, (*iter).c_str()); } redisReply *reply = (redisReply*)redisCommand(context_, cmd); if (reply==NULL || reply->type == REDIS_REPLY_ERROR) { if (reply) { printf("Srem command: %s, error: %s.\n", cmd,reply->str); freeReplyObject(reply); } DisConnect(); return false; } freeReplyObject(reply); reply = NULL; return true; }
CConnect::~CConnect() { if (m_ptrCon !=NULL) { DisConnect(); } }
CNetInstall::~CNetInstall() { DisConnect(); if (m_bInited) { WSACleanup(); } }
DBSpool::~DBSpool() { pthread_mutex_destroy(&m_thMutex); pthread_mutex_destroy(&m_thExecMutex); pthread_mutex_destroy(&m_thIdleMutex); pthread_cond_destroy(&m_thCond); DisConnect(); }
psNetConnection::~psNetConnection() { fflush(stdout); if (IsReady() || server) { DisConnect(); } }
int main(int argc, char **argv) { Connection con; PortConnection port; const char *name = TIMING_NAME; const char *other, *now; struct sigaction sa; sa.sa_handler= SIG_IGN; sa.sa_flags=SA_NOCLDWAIT; /* this old code produced a warning struct sigaction sa = { SIG_IGN, 0, 0, SA_NOCLDWAIT };*/ x = time(0); sigaction (SIGCHLD, &sa, 0); { char buf[1024]; FILE *x; x = fopen ("modulus.hex", "r"); if (! x) { perror ("modulus.hex"); exit (2); } fgets (buf, 1024, x); fclose (x); LHex2Long (buf, &n); } /*************** Globales Port eröffnen ***************/ if (!(port=OpenPort(name))) { fprintf(stderr,"TIMING_DAEMON: Kann das Dämon-Port \"%s\" nicht erzeugen: %s\n",name,NET_ErrorText()); exit(20); } /******************* Hauptschleife **********************/ while (1) { /************** Auf Verbindung auf dem Port warten ****************/ if (!(con=WaitAtPort(port))) { fprintf(stderr,"TIMING_DAEMON: WaitAtPort ging schief: %s\n",NET_ErrorText()); exit(20); } other = PeerName(port); now = Now(); fprintf (stderr, "Connect from %s: %s\n", other, now); fflush (stderr); x += time(0); handle_connection (con, other, now); DisConnect (con); } return 0; }
bool DBSpool::Connect() { if (/*m_pSQLConn || */m_pDBConnection){ DisConnect(); } /* m_pSQLConn = mysql_init((MYSQL*)NULL); if(m_pSQLConn == NULL){ gvLog(LOG_ERR_SYS, "mysql(DBSpool): unable to allocate database connection state!"); return false; } if (mysql_real_connect(m_pSQLConn, m_strHost.c_str(), m_strUser.c_str(), m_strPasswd.c_str(), m_strDBname.c_str(), m_shPort, NULL, 0) == NULL){ gvLog(LOG_ERR_SYS, "mysql(DBSpool): connection to database [%s:%d(%s)] failed: %s", m_strHost.c_str(), m_shPort, m_strDBname.c_str(), mysql_error(m_pSQLConn)); mysql_close(m_pSQLConn); m_pSQLConn = NULL; return false; } */ // MYSQL_RES* SQLRes = NULL; // int iRelt = 0; Lock(&m_thExecMutex); m_pDBConnection = new DBConnection[m_shCount]; if (m_pDBConnection){ for (int i=0; i<m_shCount; i++){ m_pDBConnection[i].SetIdentifier(i + 1); m_IdleList.push_back(i); m_pDBConnection[i].SetDBparm(m_strHost, m_shPort, m_strDBname, m_strUser, m_strPasswd); if (!m_pDBConnection[i].Connect()){ DisConnect(); Unlock(&m_thExecMutex); return false; } /* #ifdef CLN_UTF8 m_pDBConnection[i].ExecuteSQL("set names utf8", &SQLRes, iRelt);//���ÿͻ����ַ� #endif */ } } Unlock(&m_thExecMutex); return true; }
BOOL CNetInstall::StopConn() { BOOL bRet = FALSE; bRet = !shutdown(m_socket, SD_BOTH); if (bRet) { DisConnect(); } return bRet; }
//销毁接口层 long __stdcall DestoryChannels() { AFX_MANAGE_STATE(AfxGetStaticModuleState()); DisConnect(); CloseNotify(); DRTPUninitialize(); return RET_OK; }
BOOL ClientEngine::OnCmdPickCharAck( UINT16 wCommandID, UINT64 u64ConnID, CBufferHelper *pBufferHelper ) { StCharPickCharAck CharPickCharAck; pBufferHelper->Read(CharPickCharAck); CHECK_PAYER_ID(CharPickCharAck.u64CharID); if(CharPickCharAck.nRetCode == E_SUCCESSED) { DisConnect(); m_u64ClientID = CharPickCharAck.u64CharID; m_dwIdentifyCode = CharPickCharAck.dwIdentifyCode; ConnectToServer(CharPickCharAck.szIpAddr, CharPickCharAck.sPort); } return TRUE; }
bool CDataRedis::SelectDB(uint16_t db_index) { if(!KeepConnect()) return false; redisReply *reply = (redisReply*)redisCommand(context_,"select %d", db_index); if (reply==NULL || reply->type == REDIS_REPLY_ERROR) { if (reply) { printf("Fail to select db from redis, db_id: %u, error: %s.\n",db_index,reply->str); freeReplyObject(reply); } DisConnect(); return false; } freeReplyObject(reply); reply = NULL; return true; }
bool CDataRedis::Rpush(const char *key,const char *item) { assert(key); assert(item); if(!KeepConnect()) return false; redisReply *reply = (redisReply*)redisCommand(context_,"rpush %s %s", key, item); if (reply==NULL || reply->type == REDIS_REPLY_ERROR) { if(reply) { printf("Fail to set value, key: %s, value: %s, error: %s.\n",key,item,reply->str); freeReplyObject(reply); } DisConnect(); return false; } freeReplyObject(reply); reply = NULL; return true; }
//从list的头部删除元素,并返回删除元素 bool CDataRedis::Lpop(const char *key, string &item) { assert(NULL!=key); if(!KeepConnect()) return false; redisReply *reply = (redisReply*)redisCommand(context_, "lpop %s", key); if (reply==NULL || reply->type == REDIS_REPLY_ERROR) { if (reply) { printf("fail to get value, key: %s, error: %s.\n",key,reply->str); freeReplyObject(reply); } DisConnect(); return false; }else { item.assign(reply->str,reply->len); } freeReplyObject(reply); reply = NULL; return true; }
bool CDataRedis::Sadd(const char *key, const char *mumb) { if(!KeepConnect()) return false; char cmd[4096] = {0}; snprintf(cmd,4096,"sadd %s %s",key,mumb); redisReply *reply = (redisReply*)redisCommand(context_, cmd); if (reply==NULL || reply->type == REDIS_REPLY_ERROR) { if (reply) { printf("Sadd command: %s, error: %s.\n", cmd,reply->str); freeReplyObject(reply); } DisConnect(); return false; } freeReplyObject(reply); reply = NULL; return true; }
bool CDataRedis::Hset(const char *key, const char *field, const char *value) { if(!KeepConnect()) return false; char cmd[4096] = {0}; snprintf(cmd,4096,"hset %s %s %s",key,field,value); printf("%s.\n",cmd); redisReply *reply = (redisReply*)redisCommand(context_, cmd); if (reply==NULL || reply->type == REDIS_REPLY_ERROR) { if (reply) { printf("Hset command: %s, error: %s.\n", cmd,reply->str); freeReplyObject(reply); } DisConnect(); return false; } freeReplyObject(reply); reply = NULL; return true; }
bool CDataRedis::DeleteKey(const char *key) { assert(NULL!=key); if(!KeepConnect()) return false; char cmd[1024] = {0}; snprintf(cmd,1023,"del %s", key); redisReply *reply = (redisReply*)redisCommand(context_, cmd); if (reply==NULL || reply->type == REDIS_REPLY_ERROR) { if(reply) { printf("fail to delete key, delCommand: %s, error: %s.\n",cmd,reply->str); freeReplyObject(reply); } DisConnect(); return false; } freeReplyObject(reply); reply = NULL; return true; }
bool CDataRedis::GetValue(const char *key, string &value) { assert(NULL!=key); if(!KeepConnect()) return false; //printf("key=%s\n",key); redisReply *reply = (redisReply*)redisCommand(context_, "GET %s", key); if (reply==NULL || reply->type == REDIS_REPLY_ERROR||reply->len==0) { if(reply) { //printf("fail to get value, key: %s, error: %s.\n",key,reply->str); freeReplyObject(reply); } DisConnect();//chenyg add 20150714 return false; }else { //printf("reply->str=%s\n",reply->str); //printf("reply->len=%d\n",reply->len); value.assign(reply->str,reply->len); } freeReplyObject(reply); reply = NULL; return true; }
bool CDataRedis::Hvals(const char *key,deque<string> &values) { assert(NULL!=key); if(!KeepConnect()) return false; redisReply *reply = (redisReply*)redisCommand(context_, "hvals %s", key); if (reply==NULL || reply->type == REDIS_REPLY_ERROR) { if(reply) { printf("fail to get value, key: %s, error: %s.\n",key,reply->str); freeReplyObject(reply); } DisConnect(); return false; }else { size_t count = reply->elements; for(size_t i = 0; i < count; i++) { values.push_back(reply->element[i]->str); } } freeReplyObject(reply); reply = NULL; return true; }
bool NetChannelBase::Fetch2Stream() { assert( m_StreamIn.GetSpaceBeforeData() == 0 && m_StreamIn.GetSpaceAfterData() == m_StreamIn.GetSpace()); int32 nSpace = (int32)m_StreamIn.GetSpaceAfterData(); assert( nSpace >= 0 ); if( nSpace <= 0 ) return true; char* pBuffer = (char*)m_StreamIn.GetBuffer(); int32 nRet = RecvData( pBuffer, nSpace); if(nRet < 0) { DisConnect(); return false; } m_StreamIn.ReserveForWrite(nRet); return true; }
//返回key对应list的长度 bool CDataRedis::Llen(const char *key, uint32_t &len) { assert(NULL!=key); if(!KeepConnect()) return false; redisReply *reply = (redisReply*)redisCommand(context_, "llen %s", key); if (reply==NULL || reply->type == REDIS_REPLY_ERROR) { if(reply) { printf("fail to get value, key: %s, error: %s.\n",key,reply->str); freeReplyObject(reply); } DisConnect(); return false; }else { len = reply->integer; /* string value; value.assign(reply->str,reply->len); len = atoi(value.c_str());*/ } freeReplyObject(reply); reply = NULL; return true; }
bool CDataRedis::SetValue(const char* key, const char *value, int32_t expiration) { assert(key); assert(value); if(!KeepConnect()) return false; redisReply *reply = NULL; if (expiration > 0) { reply = (redisReply*)redisCommand(context_,"SET %s %s ex %d", key, value, expiration); } else { reply = (redisReply*)redisCommand(context_,"SET %s %s", key, value); } if (reply==NULL || reply->type == REDIS_REPLY_ERROR) { if (reply) { printf("Fail to set value, key: %s, value: %s, error: %s.\n",key,value,reply->str); freeReplyObject(reply); } DisConnect(); return false; } freeReplyObject(reply); reply = NULL; return true; }
bool CDataRedis::IncrByValue(const char* key,int32_t expiration) { assert(key); if(!KeepConnect()) return false; //printf("expiration=%d\n",expiration); redisReply *reply = NULL; if (expiration > 0) { reply = (redisReply*)redisCommand(context_,"incrby %s %d", key, expiration); } else { reply = (redisReply*)redisCommand(context_,"incr %s", key); } if (reply==NULL || reply->type == REDIS_REPLY_ERROR) { if (reply) { //printf("Fail to incr value, key: %s, error: %s.\n",key,reply->str); freeReplyObject(reply); } DisConnect(); //chenyg add 20150714 return false; } freeReplyObject(reply); reply = NULL; return true; }
void NetChannel::AsynSend(PBYTE pData, long lDataSize) { MYOVERLAPPED& olp = m_OLPSend; WSABUF wsaBuf; wsaBuf.buf = LPSTR(pData); wsaBuf.len = lDataSize; DWORD dwTransed = 0; DWORD dwFlags = 0; int32 nResult = WSASend( m_socket.GetSocket(), &wsaBuf, 1, &dwTransed, dwFlags, (LPWSAOVERLAPPED)&olp, NULL); if( nResult != 0) { if( WSAGetLastError() != WSA_IO_PENDING ) { OnExitSending(); DisConnect(); return; } } m_pMgr->BytesSend().Add(int64(lDataSize)); }
bool DBConnection::Connect() { if (m_pSQLConn){ DisConnect(); } m_pSQLConn = mysql_init((MYSQL*)NULL); if(m_pSQLConn == NULL) { return false; } if (mysql_real_connect(m_pSQLConn, m_strHost.c_str(), m_strUser.c_str(), m_strPasswd.c_str(), m_strDBname.c_str(), m_shPort, NULL, 0) == NULL){ mysql_close(m_pSQLConn); m_pSQLConn = NULL; return false; } #ifdef CLN_UTF8 if (m_pSQLConn) { mysql_query(m_pSQLConn, "set names utf8");//���ÿͻ����ַ� } #endif return true; }