⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendun.cpp

📁 SP平台联通短信发送程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//此程序是中国联通9020发送程序,实现功能:(号段的判断)
//1、单条发送一般的文本信息
//2、发送免提短信
//3、铃声图片的发送:从MSGCONTENT中取出3位十进制转换为二进制后发送出去
//4、支持群发功能:当MsgFormat=99时,从指定文件中取出手机号码,连接到目的手机号上,发送出去
//5、有号段的判断,但号段表中的数据只是本省的,故只能判断本省。
//  但在号段的判断时考虑比较多,当接收方付费时,判断接收方的省份,将接收方不是本省的插入到COM_SEND_OTHER中,
//  且考虑了第三方付费的情况,将根据不同情况将信息发送出去。可能一条信息会被拆分成两条:一条接收方,一条付费方
//  ***所有的信息都会作一次尝试性发送

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pthread.h>

#include "../include/define.h"
#include "mysql/mysql.h"

#include "../include/sm_tools.h"
#include "../include/sm_db.h"
#include "../include/sgip.h"
#include "send.h"
#define MAX_COMMAND	10
#define SELF_TYPE	"send_9020"

void send_sm();
int gbtounicode(char *msg,char *result);
static int Send_Demo(SEND_STRUC sendstruc,char *demo);

pthread_mutex_t mutex;

//********************************************************
SENDSTRUC *waitheader;
SENDSTRUC *waittail;
int waitnum;

SENDSTRUC *okheader;
SENDSTRUC *oktail;
//********************************************************

//初始化链表
int initlist()
{
	waitheader = NULL;
	waittail = NULL;
	okheader = NULL;
	oktail = NULL;
	waitnum = 0;
	return 0;
}

//从已处理的链表中取信息,以便将COM_SEND表中的FLAG置为相应的值
int getoklist(SENDSTRUC **sendstruc)
{
	SENDSTRUC *point;

	pthread_mutex_lock(&mutex);
	
	if(okheader == NULL)
	{
		pthread_mutex_unlock(&mutex);
		return 0;
	}

	point = okheader;
	okheader = okheader->next;
	
	if(okheader == NULL)
	{
		oktail = NULL;
	}

	*sendstruc = point;
	
	pthread_mutex_unlock(&mutex);
	return 1;
}

//从等待链表中取信息后发送出去
int getwaitlist(SENDSTRUC **sendstruc)
{
	SENDSTRUC *point;
	
	pthread_mutex_lock(&mutex);

	if(waitheader == NULL)
	{
		pthread_mutex_unlock(&mutex);
		return 0;
	}

	point = waitheader;
	waitheader = waitheader->next;
	
	if(waitheader == NULL)
	{
		waittail = NULL;
	}
	
	*sendstruc = point;
	if(waitnum > 0)
	{
		waitnum --;
	}
	
	pthread_mutex_unlock(&mutex);
	return 1;
}

//从COM_SEND表中取出信息添加到等待链表中,等待发送
int addwaitlist(SENDSTRUC *sendstruc)
{
	pthread_mutex_lock(&mutex);
	if(waitheader == NULL)
	{
		waitheader = sendstruc;
		waittail = sendstruc;
	}
	else
	{
		waittail->next = sendstruc;
		waittail = waittail->next;
	}
	waittail -> next = NULL;
	
	waitnum ++;

	pthread_mutex_unlock(&mutex);
	return 1;
}

//将发送完的记录加入到OK链表中
int addoklist(SENDSTRUC *sendstruc)
{
	pthread_mutex_lock(&mutex);
	if(okheader == NULL)
	{
		okheader = sendstruc;
		oktail = sendstruc;
	}
	else
	{
		oktail->next = sendstruc;
		oktail = oktail->next;
	}
	oktail -> next = NULL;

	pthread_mutex_unlock(&mutex);
	return 1;
}

//***********************************************************

//***********************************************************

#define INIFILE "../config/sm.ini"
#define INIFILE_UN "../config/sms_130.ini"

static int MAX_WAITLIST;
static int THREAD_NUM;
static char PROVINCE[10];
static char FFSTRING[2000][500];
static int FFSTR_NUM;

char db_host[100],db_user[100],db_passwd[100],db_dbname[100];
MYSQL *myconn;

//变量的初始化
int InitPara()
{
	myconn = NULL;
	MAX_WAITLIST = GetProfileInt(INIFILE,"SEND","MAX_WAITLIST",100);
	THREAD_NUM = GetProfileInt(INIFILE,"SEND","THREAD_NUM",6);
	GetProfileString(INIFILE,"DB_LOCAL","HOST","localhost",db_host);
	GetProfileString(INIFILE,"DB_LOCAL","LOGNAME","root",db_user);
	GetProfileString(INIFILE,"DB_LOCAL","LOGPASS","",db_passwd);
	GetProfileString(INIFILE,"DB_LOCAL","DBNAME","NW_SM_TEST",db_dbname);
	GetProfileString(INIFILE,"HD","PROVINCE","",PROVINCE);
	
	return 0;
}


int main(int argc,char * argv[])
{
	char buf[100];
  int i,retval1;
  pthread_t thread_id;
  char sql[500];
  MYSQL_ROW row;
  MYSQL_RES *res = NULL;

	InitPara();
	
	//连接数据库
	myconn = dbconnect(db_host,db_user,db_passwd,db_dbname);

	if(myconn == NULL)
	{
		puts("连接数据库失败,请查看日志文件!");
		return 0;
	}
	
	puts(db_dbname);         
	
	//初始化数据库,将FLAG为90的置为0
	if(DoSQL("UPDATE COM_SEND SET FLAG='0' WHERE ICPID='UN' AND FLAG='90'",&myconn)!=0)
	{
		writelog(SELF_TYPE,"初始化Send表出错!");
	}

	//初始化链表
	initlist();
	
	//初始化API函数
	if(InitSGIPAPI(INIFILE_UN) != 0)
	{
		puts("初始化CMPPAPI失败!");
		writelog(SELF_TYPE,"初始化CMPPAPI失败!");
		return 0;
	}
	
	//将非法字符调入内存
	i=0;
	sprintf(sql,"SELECT STR_FIND FROM WEB_WORDS");
  retval1 = QuerySQL(sql,&myconn,&res);
  if(retval1 != 0)
  {
  	  writelog(SELF_TYPE,"查询指令出错");
  	  return 0;
  }
  if(res == NULL)
  {
  	return 0;
  }	
  while(row = mysql_fetch_row(res))
  {
  			
     strcpy(FFSTRING[i],trim(row[0],' '));
     //puts(FFSTRING[i]);
     i=i+1;		
  }
  mysql_free_result(res);
  FFSTR_NUM=i;
  printf("非法字符数量%d",FFSTR_NUM);
  //非法字符调入内存 end;

	pthread_mutex_init(&mutex,NULL);

	//创建发送进程
	for(i=0;i<THREAD_NUM;i++)
	{
		pthread_create(&thread_id, NULL, (void* (*)(void*))send_sm, NULL);
	}

	if(DoThread() != 0)
	{
		puts("查询信息出错!");
		return 0;
	}
	
	return 0;
}

//从COM_SEND表中取出记录加入到等待列表中等待发送
int DoThread()
{
	int retval;
	
	while(1)
	{
		retval = ScanMsg();
		if(retval <= 0)
		{
			sleep(2);
			continue;
		}
		usleep(10);
	}
}

int ScanMsg()
{
	char sql[500],day[20];
	MYSQL_ROW row,row2;
	MYSQL_RES *res = NULL,*res2 = NULL;
	
	SENDSTRUC *com_buf;
	SEND_STRUC sendstruc;
	
	int retval,retval2,i,slen,j,flag1,flag2,retval1,pos,x,y,z,ffstrlen,ffstrtemplen,retval3;
	char result[1001],temp[10],errmsg[100];
	float feecode;
	char msg[1001],ffstr[500],ffstrtemp[500],ffbuf[500];

	memset(sql,0,sizeof(sql));

	//处理发送过的数据,从发送完的链表中取出信息,将FLAG的值置为其发送后的返回值,并释放所占空间
	while(getoklist(&com_buf))
	{
		sprintf(sql,
		        "UPDATE COM_SEND SET FLAG='%d',DEAL_DATE='%s' WHERE ID=%s", com_buf->flag,getnowdate(day),com_buf->id);
		if(DoSQL(sql,&myconn) != 0)
		{
			free(com_buf);
			writelog(SELF_TYPE,"更新数据库出错!");
			return 0;
		}
		free(com_buf);
	}

	if(waitnum > MAX_WAITLIST)
	{
		return 0;
	}
	
	//从COM_SEND表中要发送的联通用户的记录FLAG=0:未处理,FLAG=89:已处理,但发送时队列满,要重发
	sprintf(sql,
	       "SELECT ID,NEEDREPLY,MSGLEVEL,SERVICEID,MSGFORMAT,FEETYPE,FEECODE,VALIDTIME,ATTIME,SRCTERMID, DESTTERMID,MSGCONTENT,FEEUSERTYPE,FEETERMINALID, TPPID,TPUDHI,LINKID  FROM COM_SEND   WHERE ICPID='UN' AND (FLAG='0') ORDER BY STEP,ID  LIMIT 0,%d",MAX_COMMAND);
	retval = QuerySQL(sql,&myconn,&res);
	if(retval != 0)
	{
		writelog(SELF_TYPE,"查询指令出错COM_SEND,FLAG=0");
		puts("com_send失败");
		return 0;
		
	}
	if(res == NULL)
	{
		return 0;
	}
	retval1 = 0;
	
	while(row = mysql_fetch_row(res))
	{
		retval1 ++; 
		com_buf = (SENDSTRUC *) malloc(sizeof(SENDSTRUC));
		if(com_buf == NULL)
		{
			writelog(SELF_TYPE,"申请内存失败!");
			mysql_free_result(res);
			return 0;
		}
		
		//对链表变量赋值
		puts(com_buf->id);
		strcpy(com_buf->id,row[0]);
		com_buf->nNeedReply = atoi(row[1]);
		com_buf->nMsgLevel = atoi(row[2]);
		strcpy(com_buf->sServiceID,row[3]);
		com_buf->nMsgFormat = atoi(row[4]);
		strcpy(com_buf->sFeeType,row[5]);
		feecode=atof(row[6])*100;
		sprintf(com_buf->sFeeCode,"%06d",(int)feecode);
		strcpy(com_buf->sValidTime,"");
		strcpy(com_buf->sAtTime,"");
		strcpy(com_buf->sSrcTermID,row[9]);
		strcpy(com_buf->sDesTermID,row[10]);
		strcpy(com_buf->sMsgContent,row[11]);
		com_buf->nMsgLen = strlen(com_buf->sMsgContent);
		strcpy(com_buf->sMsgID,com_buf->id);
		com_buf->cFeeUserType = atoi(row[12]);
		strcpy(com_buf->sFeeTerminalID,row[13]);
		com_buf->cTpPid=atoi(row[14]);
		com_buf->cTpUdhi=atoi(row[15]);
    strcpy(com_buf->linkid,row[16]);
    puts(com_buf->linkid);
    puts(com_buf->sDesTermID);

		//对特定用户实行免费发送  		begin		20031013	xcy
    sprintf(sql,"SELECT MSISDN FROM INFO_FREE_PHONE WHERE MSISDN='%s' AND FLAG='1'",com_buf->sDesTermID);
    printf("%s\n",sql);
	retval2 = QuerySQL(sql,&myconn,&res2);
    if(retval2 != 0)
    {
       writelog(SELF_TYPE,"查询指令出错INFO_FREE_PHONE");
       return 0;
    }
    if(res2 == NULL)
    {
       return 0;
    }
		
    if (row2 = mysql_fetch_row(res2))
    {
    	if (strcmp(com_buf->sFeeType,"02")==0)
    	{
         strcpy(com_buf->sFeeType,"01");
      }
      else if (strcmp(com_buf->sFeeType,"03")==0 && com_buf->nNeedReply==2)
      {
      	mysql_free_result(res2);
      	sprintf(sql,
            		"UPDATE COM_SEND SET FLAG='1' WHERE ID=%s",com_buf->id);
      	if(DoSQL(sql,&myconn) != 0)
      	{
      	   free(com_buf);
      	   writelog(SELF_TYPE,"更新数据库出错!");
      	   return 0;
      	}       
      	free(com_buf);         
      	continue;
      }
    }
    mysql_free_result(res2);
    
    //对特定用户实行免费发送  		end		20031013	xcy
                
		//对结构体变量赋值,用于插入COM_SEND_OTHER表
		
		sendstruc.nNeedReply = atoi(row[1]);					
		sendstruc.nMsgLevel = atoi(row[2]);                             	
		strcpy(sendstruc.sServiceID,row[3]);                            	
		sendstruc.nMsgFormat = atoi(row[4]);                            	
		strcpy(sendstruc.sFeeType,row[5]);                              	
		sendstruc.sFeeCode=atof(row[6]);                	
		strcpy(sendstruc.sValidTime,"");		                	
		strcpy(sendstruc.sAtTime,"");		                        	
		strcpy(sendstruc.sSrcTermID,row[9]);                            	
		strcpy(sendstruc.sDesTermID,row[10]);                           	
		strcpy(msg,row[11]);                                            	
		trim(msg,' ');                             

		//判断发送内容的长度是否超过500,若超过了500,则不发送,直接置为1,并写LOG	begin	2003.06.25	邢
		if (strlen(msg)>500)
		{
			sprintf(sql,"UPDATE COM_SEND SET FLAG='1',DEAL_DATE='%s' WHERE ID=%s",
				getnowdate(day),com_buf->id);
				
			if(DoSQL(sql,&myconn) != 0)
			{
				free(com_buf);
				writelog(SELF_TYPE,"更新数据库出错!");
				mysql_free_result(res);
				return 0;
			}

			sprintf(errmsg,"id:%s,srctermid:%s,serviceid:%s",com_buf->id,com_buf->sDesTermID,com_buf->sServiceID);
			writelog(SELF_TYPE,errmsg);
			free(com_buf);
			continue;
		}
		//判断发送内容的长度是否超过500,若超过了500,则不发送,直接置为1,并写LOG	end	2003.06.25	邢
		
                     	
		strcpy(sendstruc.sMsgContent,msg);                              	
		sendstruc.cFeeUserType = atoi(row[12]);                          	
		strcpy(sendstruc.sFeeTerminalID,row[13]);
		sendstruc.cTpPid=atoi(row[14]);		
		sendstruc.cTpUdhi=atoi(row[15]);	
    strcpy(sendstruc.sMsgContent,row[16]);
    
    
    /*当内容中含有非法字符时进行处理*/
    strcpy(ffstr,"");
    strcpy(ffbuf,"");
		strcpy(ffstr,com_buf->sMsgContent);
		ffstrlen=strlen(ffstr);
		puts(ffstr);
		printf("ffstr is %d",ffstrlen);
		pos=0;	
		x=0;
		while(pos<=ffstrlen)
		{
			//扫描字符串
			y=0;	
			z=0;	
      while(z<FFSTR_NUM)
      {
         strcpy(ffstrtemp,"");
               strcpy(ffstrtemp,FFSTRING[z]);
         ffstrtemplen=strlen(ffstrtemp);
         if(strncmp(ffstr + pos,ffstrtemp,ffstrtemplen) == 0)
			   {	
						ffbuf[x++]='*';
						pos+=ffstrtemplen;
						y=1;
						break;
			   }
			   z++;
      }
      if(y==0)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -