📄 kj4realdatajob.cpp
字号:
// KJ4RealDataJob.cpp: implementation of the KJ4RealDataJob class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "KJ4RealDataJob.h"
#include "KJ4Service.h"
#include "../KylinSystem/KMessageBinder.h"
#include "../KylinSystem/KMessageManager.h"
#include "../KylinSystem/KTimerManager.h"
#include "../KylinSystem/KStringExtractor.h"
#include "../KylinSystem/KThreadPool.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
KJ4RealDataJob::KJ4RealDataJob()
: mConnection(NULL)
{
mMessage = (KRealDataMessage*)KMessageManager::Instance()->CreateMessage(KRealDataMessage::TypeID());
mMessage->AddRef();
mConnMessage = (KInterfaceConnMessage*)KMessageManager::Instance()->CreateMessage(KInterfaceConnMessage::TypeID());
mConnMessage->AddRef();
mFailMessage = (KInterfaceFailMessage*)KMessageManager::Instance()->CreateMessage(KInterfaceFailMessage::TypeID());
mFailMessage->AddRef();
}
KJ4RealDataJob::~KJ4RealDataJob()
{
mMessage->Release();
mConnMessage->Release();
mFailMessage->Release();
}
const KGuid KJ4RealDataJob::GetTypeID()
{
return KGuid::NullGuid();
}
const CString KJ4RealDataJob::GetTypeName()
{
return _T("KJ4RealDataJob");
}
bool KJ4RealDataJob::Execute()
{
if(mStrData.IsEmpty())
return true;
KJ4Service* host = (KJ4Service*)mHostService;
RealDataItem item;
mMessage->SetChange(0);
SYSTEMTIME systime;
GetLocalTime(&systime);
SystemTimeToVariantTime(&systime, &item.Time);
KStringExtractor ext, extItem;
ext.SetValue(mStrData);
CString strItem, strToken;
while(!ext.IsEmpty())
{
ext.GetString(strItem, _T("\r"));
if(strItem.IsEmpty())
continue;
extItem.SetValue(strItem);
item.Num = extItem.GetInt();
extItem.Skip();
item.Value = extItem.GetDouble();
extItem.GetString(strToken);
host->GetRealDataStatus(item, strToken);
mMessage->SetRealData(item);
}
if(mMessage->IsDirty())
{
KMessageBinder::Instance()->ProcessMessage(mMessage);
}
return true;
}
bool KJ4RealDataJob::Init()
{
KJ4Service* host = (KJ4Service*)mHostService;
mMessage->SetMineID(host->mMineID);
mConnMessage->SetMineID(host->mMineID);
mFailMessage->SetMineID(host->mMineID);
return true;
}
void KJ4RealDataJob::Start()
{
KJ4Service* host = (KJ4Service*)mHostService;
mConnection = new KSocket(this);
mConnection->Connect(host->mAddr, host->mPort);
}
void KJ4RealDataJob::Stop()
{
mConnection->Close();
delete mConnection;
mConnection = NULL;
KTimerManager::Instance()->KillTimer(this, 0);
}
void KJ4RealDataJob::OnConnect(KSocket* sock, int error)
{
KTimerManager::Instance()->KillTimer(this, 0);
if(error != 0)
{
OnClose(mConnection, error);
}
else
{
KJ4Service* host = (KJ4Service*)mHostService;
if(host->mInterfaceFail)
{
host->mInterfaceFail = false;
SYSTEMTIME systime;
GetLocalTime(&systime);
mConnMessage->SetConnTime(systime);
KMessageBinder::Instance()->ProcessMessage(mConnMessage);
}
}
}
void KJ4RealDataJob::OnClose(KSocket* sock, int error)
{
mConnection->Close();
KTimerManager::Instance()->SetTimer(this, 0, 60000);
KJ4Service* host = (KJ4Service*)mHostService;
if(!host->mInterfaceFail)
{
host->mInterfaceFail = true;
CString reason;
reason.Format(_T("连接到实时数据服务器%s:%d失败!"), host->mAddr, host->mPort);
mFailMessage->SetReason(reason);
SYSTEMTIME systime;
GetLocalTime(&systime);
mFailMessage->SetFailTime(systime);
KMessageBinder::Instance()->ProcessMessage(mFailMessage);
}
}
void KJ4RealDataJob::OnReceive(KSocket* sock, int error)
{
const int BUFLEN = 4096;
BYTE buf[BUFLEN];
ZeroMemory(buf, BUFLEN);
sock->Receive(buf, BUFLEN);
mStrData += buf;
KThreadPool::Instance()->QueueWorkItem(this);
}
void KJ4RealDataJob::OnTimer(UINT timer, LPVOID param)
{
KJ4Service* host = (KJ4Service*)mHostService;
mConnection->Connect(host->mAddr, host->mPort);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -