📄 sidelight.cpp
字号:
#include "Sidelight.hpp"
#include "dbInterface.hpp"
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
static TimerEvent STime;
bool Exit;
void TerminateSignal(int sigcount)
{
Exit=1;
}
bool SendSmsBase::GetExit()
{
return !Exit;
}
SendSmsBase::SendSmsBase()
{
Exit=0;
signal(SIGTERM, TerminateSignal);
signal(SIGINT, TerminateSignal);
signal(SIGQUIT, TerminateSignal);
pthread_mutex_init(&pHreadMutex,0);
ThreadState=1;
}
int SendSmsBase::Init()
{
return 0;
}
unsigned long SendSmsBase::OnTime(TimerEvent *Event)
{
return Event->Event[Event->id];
}
void *OnTime(void *pThis)
{
TimerEvent *pSendSms=(TimerEvent*)pThis;
pthread_mutex_t *pHreadMutex=&pSendSms->pSendSms->pHreadMutex;
pthread_mutex_lock(pHreadMutex);
TimerEvent SendSmsEvent;
unsigned long id=SendSmsEvent.id=pSendSms->id;
SendSmsEvent.Second[id]=pSendSms->Second[id];
SendSmsEvent.Event[id]=pSendSms->Event[id];
SendSmsEvent.len=pSendSms->len;
SendSmsEvent.lpvoid=malloc(pSendSms->len);
if(SendSmsEvent.lpvoid)memcpy(SendSmsEvent.lpvoid,pSendSms->lpvoid,pSendSms->len);
pthread_mutex_unlock(pHreadMutex);
bool pEvent=1;
while(pEvent)
{
sleep(SendSmsEvent.Second[id]);
pEvent=pSendSms->pSendSms->OnTime(&SendSmsEvent);
}
pSendSms->pSendSms->ReleaseID(id);
return 0;
}
int SendSmsBase::CreateOnTime(unsigned long Second,unsigned long Event)
{
if(id>MAXEVENT-1)
return -1;
pthread_mutex_lock(&pHreadMutex);
STime.pSendSms=this;
STime.Event[id]=Event;
STime.Second[id]=Second;
STime.id=id;
pthread_mutex_unlock(&pHreadMutex);
pthread_t tid;
if(!pthread_create(&tid,0,::OnTime,&STime))
{
return id++;
}
return -2;
}
void *OnEventTime(void *pThis)
{
TimerEvent *pSendSms=(TimerEvent*)pThis;
pthread_mutex_t *pHreadMutex=&pSendSms->pSendSms->pHreadMutex;
pthread_mutex_lock(pHreadMutex);
TimerEvent SendSmsEvent;
memcpy(&SendSmsEvent,pThis,sizeof(TimerEvent));
SendSmsEvent.pSendSms=pSendSms->pSendSms;
SendSmsEvent.lpvoid=malloc(pSendSms->len);
if(SendSmsEvent.lpvoid)memcpy(SendSmsEvent.lpvoid,pSendSms->lpvoid,pSendSms->len);
pthread_mutex_unlock(pHreadMutex);
bool pEvent=1;
pSendSms->pSendSms->ThreadState=0;
if(SendSmsEvent.len&&!SendSmsEvent.lpvoid)
return 0;
while(pEvent)
{
sleep(SendSmsEvent.Second[0]);
pEvent=SendSmsEvent.pSendSms->OnTime(&SendSmsEvent);
}
free(SendSmsEvent.lpvoid);
return 0;
}
unsigned long CreateThread(
void *lpThreadAttributes, // pointer to security attributes
unsigned long dwStackSize, // initial thread stack size
void *(*lpStartAddress)(void *),// pointer to thread function
void *lpParameter, // argument for new thread
unsigned long dwCreationFlags, // creation flags
unsigned long *lpThreadId // pointer to receive thread ID
)
{
pthread_t tid;
if (! lpThreadId)
{
lpThreadId = &tid;
}
return pthread_create(lpThreadId ,NULL, lpStartAddress, lpParameter);
}
int SendSmsBase::CreateOnTime(TimerEvent *pEvent)
{
/* unsigned long id=pEvent->id;
pthread_mutex_lock(&pHreadMutex);
STime.len=pEvent->len;
STime.lpvoid=malloc(pEvent->len);
if(STime.lpvoid)memcpy(STime.lpvoid,pEvent->lpvoid,pEvent->len);
STime.pSendSms=pEvent->pSendSms?pEvent->pSendSms:this;
pthread_mutex_unlock(&pHreadMutex);
*/ ThreadState=1;
if(!CreateThread(0, 0, OnEventTime, pEvent, 0, 0))
{
WaitThread();
return 0;
}
return -1;
}
int SendSmsBase::ReleaseID(unsigned long id)
{
if(id>=MAXEVENT||!STime.Event[id])
return -1;
pthread_mutex_lock(&pHreadMutex);
STime.Event[id]=0;
STime.Second[id]=0;
this->id--;
for(unsigned long i=id;STime.Event[i+1];i++)
{
STime.Event[i]=STime.Event[i+1];
STime.Second[i]=STime.Second[i+1];
}
pthread_mutex_unlock(&pHreadMutex);
return 0;
}
int SendSmsBase::ReleaseEvent(unsigned long Event)
{
for(int i=0;i<MAXEVENT;i++)
{
if(STime.Event[i]==Event)
{
ReleaseID(i);
return 0;
}
}
return -1;
}
int SendSmsBase::WaitThread()
{
while(ThreadState);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -