// SimpleService.cpp : Implementation of WinMain
#include "stdafx.h"
#include "resource.h"
#include "SimpleService.h"
#define LOGFILE "C:\\Log.log"
#define LOG_FILE "C:\\LogOUT.log"
#include <ctime>
#include "uStruct.h"
#include <tchar.h>
#include <stdio.h>
#include <comdef.h>
#pragma comment(lib, "ws2_32.lib")
#include <string>
#include <iostream>
#include "parameters.h"
using namespace std;
class CSimpleServiceModule : public CAtlServiceModuleT< CSimpleServiceModule, IDS_SERVICENAME >
{
public :
DECLARE_REGISTRY_APPID_RESOURCEID(IDR_SIMPLESERVICE, "{01D73C58-D8F4-4A85-B27D-424443739977}")
HRESULT InitializeSecurity() throw()
{
return S_OK;
}
HRESULT PreMessageLoop (int nShowCmd);
void RunMessageLoop (void) throw();
void OnContinue (void) throw();
void OnPause (void) throw();
void OnShutdown (void) throw();
void OnStop (void) throw();
typedef CAtlServiceModuleT< CSimpleServiceModule , IDS_SERVICENAME > inherited;
virtual BOOL Install () throw();
virtual bool ParseCommandLine(LPCTSTR lpCmdLine, HRESULT* pnRetCode) throw();
virtual HRESULT RegisterAppId (bool bService = false) throw();
public :
string remoteIp;
char Hz [200];
char def [200];
char deff [1024];//="\0";
unsigned char buf [1024];
StructDatchik datchik [16];
StructDatchik g_datchik [256];
struct sockaddr_in sa;
struct sockaddr_in addr;
SOCKET sock;
SOCKET Sock;
char timee[10];
};
void log_message(char *filename, char *message)
{
char timee[10];
_strtime_s(timee);
FILE *logfile;
logfile = fopen(filename, "a");
if(!logfile)
return;
fprintf(logfile, "%s",message);
fprintf(logfile, "%s\n",timee);
fclose(logfile);
}
void log_sending(char *filename, char *message)
{
FILE *logfile;
logfile = fopen(filename, "a");
if(!logfile)
return;
fprintf(logfile, "%s",message);
fclose(logfile);
}
HRESULT CSimpleServiceModule::PreMessageLoop (int nShowCmd) throw()
{
log_message(LOGFILE, "PreMessageLoop");
return CAtlServiceModuleT< CSimpleServiceModule, IDS_SERVICENAME >::PreMessageLoop (nShowCmd);
}
void CSimpleServiceModule::RunMessageLoop () throw()
{
log_message(LOGFILE,"Start.");
int fromlen, bound, error;
char data [10];
int ddr = 1;
WSADATA ws;
WSAStartup (MAKEWORD( 2, 2 ), &ws);
_strtime_s(timee);
int iOptLen = sizeof(int);
WSADATA wsa;
if(WSAStartup(0x202, &wsa)!=0)
{
log_message(LOGFILE,"ERROR: WSAStartup failed\n");
return;
}
Sock = socket(AF_INET, SOCK_DGRAM, 0);
sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if (Sock < 0 && sock < 0)
{
log_message(LOGFILE,"ERROR: Create socket failed\n");
return;
}
u_long Port;
Parameters::GetParameters2(&Port);
log_message(LOGFILE,"Regedit read.");
setsockopt(Sock, SOL_SOCKET, SO_BROADCAST,(char*)&ddr, sizeof(int));
addr.sin_family = AF_INET;
addr.sin_port =htons(Port);
addr.sin_addr.s_addr = 0xFFFFFFFF;
u_long Port2;
Parameters::GetParameters(&Port2);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons(Port2);
//printf ("sock = %d\n", sock);
fromlen = sizeof (sa);
//printf ("fromlen = %d\n", fromlen);
bound = bind (sock,(struct sockaddr *)&sa, sizeof(struct sockaddr));
//printf ("bound = %d\n", bound);
//printf("Start server: %s\n", timee);
//printf ("Read...\n");
for (int k=0; k<256;k++)
g_datchik[k].status = 0;
for(;;)
{
char def8[1024]="\0";
char def10[1024]="\0";
char def6[1024]="\0";
_strtime_s(timee);
_strdate_s(data);
int recsize = recvfrom(sock, Hz, 100, 0, (sockaddr *)&sa, &fromlen);
int so = accept(sock, (sockaddr *)&sa, &fromlen);
remoteIp = inet_ntoa(sa.sin_addr);
memcpy(buf, Hz, recsize);
if (recsize >=13)
{
int k_vo;
k_vo=buf [1];
if (k_vo*9+4 == recsize)
.
.
.
.
//printf (def);
// std::cout << "client connected - ip " << remoteIp.c_str() << ", port " << sa.sin_port << std::endl;
// printf("recsize: %d\n",recsize);
//printf("Time: %s\n\n", timee);
}
}
try {
for (int i=0; i<256;i++)
if (g_datchik.status == 1)
{
char *z = deff;
z += sprintf(z,"%d",i+1 );
z += sprintf(z," %5.2f",g_datchik.t_ra );
z += sprintf(z," %s", g_datchik.n_unit);
z += sprintf(z," %s", g_datchik.IP);
z += sprintf(z," %s", datchik.time);
z += sprintf(z," %s", datchik.data);
z += sprintf(z," %d", g_datchik.period);
sprintf(z,"\n");
printf (deff,"\n");
sendto (Sock, deff,100,0,(sockaddr *)&addr, sizeof (addr));
log_message(LOGFILE, "Данные посланы");
log_sending(LOG_FILE,deff);
g_datchik.status = 0;
}
}catch (_com_error& er)
{}
u_long Period;
Parameters::GetParameters3(&Period);
Sleep (Period*1000);
continue;
}
log_message(LOGFILE, "RunMessageLoop");
CAtlServiceModuleT< CSimpleServiceModule, IDS_SERVICENAME >::RunMessageLoop ();
}
void CSimpleServiceModule::OnContinue () throw()
{
log_message(LOGFILE, "OnContinue");
CAtlServiceModuleT< CSimpleServiceModule, IDS_SERVICENAME >::OnContinue ();
}
void CSimpleServiceModule::OnPause () throw()
{
log_message(LOGFILE, "OnPause");
CAtlServiceModuleT< CSimpleServiceModule, IDS_SERVICENAME >::OnPause ();
}
void CSimpleServiceModule::OnShutdown () throw()
{
log_message(LOGFILE, "OnShutdown");
OnStop ();
}
void CSimpleServiceModule::OnStop () throw()
{
closesocket (Sock);
closesocket (sock);
WSACleanup ( );
log_message(LOGFILE, "OnStop");
CAtlServiceModuleT< CSimpleServiceModule, IDS_SERVICENAME >::OnStop ();
}
CSimpleServiceModule _AtlModule;
bool CSimpleServiceModule ::ParseCommandLine(LPCTSTR lpCmdLine, HRESULT* pnRetCode) throw()
{
if(!CAtlExeModuleT<CSimpleServiceModule >::ParseCommandLine(lpCmdLine, pnRetCode))
return false;
TCHAR szTokens[] = _T("-/");
*pnRetCode = S_OK;
LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens);
while (lpszToken != NULL)
{
if (WordCmpI(lpszToken, _T("Install"))==0)
{
*pnRetCode = RegisterAppId(true);
if (SUCCEEDED(*pnRetCode))
*pnRetCode = inherited::RegisterServer(TRUE);
return false;
}
else if(WordCmpI(lpszToken, _T("Del")) == 0)
{
*pnRetCode = UnregisterAppId();
if (SUCCEEDED(*pnRetCode))
inherited::UnregisterServer(TRUE);
return false;
}
lpszToken = FindOneOf(lpszToken, szTokens);
}
return true;
}
ATL UDP Service Помогите с потоками...
Код:
В принципе много лишнего, но может кому пригодится...
Мне нужно сделать как-то так:
Код:
unsigned __stdcall threadFunc ( LPVOID Data ) {
//тут уже создавать сокет, а не в RunMessageLoop
}
........
RunMessageLoop {
_beginthreadex ( NULL, 0, threadFunc , NULL, NULL, NULL);
}
//тут уже создавать сокет, а не в RunMessageLoop
}
........
RunMessageLoop {
_beginthreadex ( NULL, 0, threadFunc , NULL, NULL, NULL);
}