/* * Generic mux interface callback functions * _lms_socket_default_read() = read condition from mux * * fd = file descriptor * e = event type * msoptr = pointer to the MSocket object on which the mux found something for us * */ void _lms_mux_default_read(int fd, short e, void *msoptr) { MSocket *mso; if (msoptr) { mso = (MSocket *)msoptr; } else if (fd >= 0) { mso = lms_socket_findbyfd(fd); } else { /* mux is going a lot batty */ return; } if (!mso) { /* mux is going a little batty */ return; } if ((mso->type == LMSTYPE_LISTEN4) || (mso->type == LMSTYPE_LISTEN6)) { lms_conn_accept(mso); } else if ((mso->type == LMSTYPE_STREAM4) || (mso->type == LMSTYPE_STREAM6)) { /* Allows us to differentiate between an SSL connection and a non-SSL connection... */ mso->func_r(mso); } /* This isn't supported in libmsocket yet else if (mso->type == LMSTYPE_LOCALLISTEN) { interface_local_accept(mso); } else if (mso->type == LMSTYPE_LOCALCLIENT) { interface_local_read(mso); } */ else { /* This should be used for datagram sockets and such */ } return; }
int make_request(MSocket & ws, TinyTLSContext * ctx, TTlsLink * link) { ttlsReset(ctx); int res = ws.connect(hostname, 443); printf("Connect returned %d\n", res); if (res != 0) { return -1; } ttlsSetLink(ctx, link); do { int result = ttlsHandshake(ctx); if (result > 0) break; if (result < 0) { printf("Handshake failed with error %d\n", result); return -1; } } while (true); printf("\nREQUEST:\n==================\n%s", request); ttlsSend(ctx, (const uint8_t*)request, strlen(request)); printf("\nRESPONSE:\n==================\n"); for (;;) { uint8_t buf[60]; int res = ttlsRecv(ctx, buf, 60); if (res <= 0) { printf("\n\n::res = %d\n", res); break; } fwrite(buf, 1, res, stdout); } ws.disconnect(); return 0; }
/* * Generic mux interface callback functions, set when creating a new MSocket object. * I don't think most of these should be used. * _lms_socket_default_write() = write condition from mux * * fd = file descriptor * e = event type * msoptr = pointer to the MSocket object on which the mux found something for us * */ void _lms_mux_default_write(int fd, short e, void *msoptr) { MSocket *mso; if (msoptr) { mso = (MSocket *)msoptr; } else if (fd >= 0) { mso = lms_socket_findbyfd(fd); } else { /* mux is going a lot batty */ return; } if (!mso) { /* mux is going a little batty */ return; } if (mso->flags & LMSFLG_WAITCONN) { mso->flags &= ~LMSFLG_WAITCONN; } if (!(mso->flags & LMSFLG_CONNECTED)) { mso->flags |= LMSFLG_CONNECTED; } mso->func_w(mso); return; }
int main(int argc, char** argv) { if(!DbInit()){ cout<<"DbInit() failed!"<<endl; exit(-1); } cout<<"BDB init finished!"<<endl; MSocket server; int port=PORT; if(server.Create()==false){ cout<<"socket create error"<<endl; return 0; } //server.SetNonblock(false); if(server.Bind(port) != 0){ cout<<"socket bind error!"<<endl; return 0; } if(server.Listen()!=0){ cout<<"socket listen error"<<endl; return 0; } MThread threadPool[THREAD_MAX]; for(int i=0;i<THREAD_MAX;i++){ threadPool[i].Create(handleClientThread,(void*)&threadPool[i]); } MSocket* pclient=NULL; while(true){ pclient=server.Accept(); //pclient need to be deleted if(pclient!=NULL){ //if blocking, NULL means accept ERROR cout<<"Server Accept connect socket-------------------------------"<<endl; MutexClient.Lock(); clientDeque.push_back(pclient); MutexClient.Unlock(); }else { cout<<"test: Blocking Socket Accept error! now sleep(100)"<<endl; MThread::Sleep(100); } } server.Close(); return 0; }
void NFSClient::RefreshTask::Invoke() { NFSClient* pClient = (NFSClient*)GetData(); if (pClient == NULL) { return; } NFS_HEAD head; head.m_nMsgCode = CODE_TEST; char szBuf[4096+512] = {0};//4096表示buffer区,512表示包头 if (pClient->m_pServerModule->m_pServer->m_nAllfileInfoLen > 0) { pClient->SendCmd(CODE_REFRESH_BEGIN); } else { pClient->SendCmd(CODE_REFRESH_FAILED); return; } head.m_iType = NFS_HEAD_DATA; head.m_nIndex = 0; head.m_nMsgCode = CODE_REFRESH_DOING; int iLeft = (pClient->m_pServerModule->m_pServer->m_nAllfileInfoLen)%MAX_DATA_LEN; int iCout = (pClient->m_pServerModule->m_pServer->m_nAllfileInfoLen)/MAX_DATA_LEN; head.m_nCount = iCout + 1; cout<<"刷新的传输次数:"<<head.m_nCount<<endl; MSocket* pClientDataSocket = pClient->m_pServerModule->m_pServer->m_DataSocket.Accept(); for (int i = 0; i < iCout; ++i) { head.m_nLength = MAX_DATA_LEN; head.m_nIndex = i; head.SetBuffer(pClient->m_pServerModule->m_pServer->m_pszAllFileInfo + MAX_DATA_LEN*i,MAX_DATA_LEN); unsigned long len = head.GetStream(szBuf); unsigned int nResult = 0; pClientDataSocket->Send(szBuf,len); } head.m_nLength = iLeft; head.m_nIndex = iCout; head.SetBuffer(pClient->m_pServerModule->m_pServer->m_pszAllFileInfo + MAX_DATA_LEN*iCout,iLeft); unsigned long len = head.GetStream(szBuf); pClientDataSocket->Send(szBuf,len); head.m_nLength = 0; head.m_nIndex = 0; head.m_nMsgCode = CODE_REFRESH_SUCCESS_DONE; len = head.GetStream(szBuf); pClientDataSocket->Send(szBuf,len); pClientDataSocket->Close(); pClient->SendCmd(CODE_REFRESH_FINISH); }
static int RecvFunction(void * ctx, uint8_t * data, size_t size) { MSocket * ws = (MSocket*)ctx; int res = ws->recv((uint8_t *)data, size); return res; }
static int SendFunction(void * ctx, const uint8_t * data, size_t size) { MSocket * ws = (MSocket*)ctx; int res = ws->send(data, size); return res; }
//--------------------------------------------------------------------------- void TcpReceiveMessage(void) { int rl; TSyslogdTcpConn * c; MSocket * p; for(int i=0; i<tcp_cons->Count; i++) { c = (TSyslogdTcpConn *)tcp_cons->Items[i]; p = c->Socket; if( p->Poll(true, false, false, 0) ) // wait 0 msec { rl = p->ReadLength(); if( rl > 0 ) { // Ready to receive rl bytes if( p->Read(c->GetBufferSize(rl), rl) ) { c->DataSize += rl; } else { if( p->bytes > 0 && p->bytes < rl ) { c->DataSize += p->bytes; WriteToLogError("WARNING\tTCP received %ld, wait %ld, error=%ld", p->bytes, rl, p->errcode); } else { if( p->errcode==WSAESHUTDOWN ) { WriteToLogError("WARNING\tTCP shutdown from %s", p->GetRemoteAddrPort().c_str()); TcpDeleteConnection(i--); continue; } else { WriteToLogError("ERROR\tTCP read(rl=%ld) from %s\tbytes=%ld error=%ld", p->GetRemoteAddrPort().c_str(), rl, p->bytes, p->errcode); } } } } else // Readed 0 bytes ! { BYTE b; if( ! p->Read(&b, 1) ) { if( p->errcode==0 && p->bytes==0 ) { // Good disconnect TcpDeleteConnection(i--); continue; } else { WriteToLogError("ERROR\tTCP error from %s: %s [%d]", p->GetRemoteAddrPort().c_str(), p->GetErrorMessageEng().c_str(), p->errcode); TcpDeleteConnection(i--); continue; } } } } // Poll if( c->DataSize > 0 ) { int i, start; for(i=start=0; i<c->DataSize; i++) { // find end of message if( c->Data[i]==0 || c->Data[i]=='\n' || c->Data[i]=='\r' ) { if( start < i ) { // process messages // NULL terminate syslog message c->Data[i] = 0; WriteToLogRawMessage((char *)(c->Data + start)); TSyslogMessage sm; sm.FromStringSyslogd((char *)(c->Data + start), i - start, &c->Socket->destAddr); if( ProcessMessageRules(&sm) ) { // option is not set: "Ignore (do not save to the default file "syslog")" TStorageFile * sf = fdb->Get(0); if( sf ) if( ! sf->Save( sm.ToString() ) ) WriteToLogError("ERROR\tSave message to file: %s", sf->GetFileName().c_str()); } } start = i + 1; } } // half of syslog message in c->Data ? if( start < i ) { // Save to buffer start memmove(c->Data, c->Data + start, i - start); c->DataSize = i - start; } else { c->DataSize = 0; } } } // End for }