68 CSocketUdp(
void) : _super(), m_isStopping(false), m_soData(INVALID_SOCKET), m_isBroadcast(-1), m_bufferSize(0), m_isAddressBroadcast(false)
98 return TNBVIW_KIND_SOCK_U + m_baseAddress.
GetPortNo();
119 WORD w = m_GetMaxUdpDg(m_soData);
139 m_baseAddress = address;
148 return m_baseAddress;
164 addr.sin_family = AF_INET;
165 m_baseAddress.
Get(addr);
167 SOCKET so = ::socket(AF_INET, SOCK_DGRAM, 0);
168 if ( so == SOCKET_ERROR )
170 TRACE1(
"::socket() error = %d\n", ::WSAGetLastError() );
173 if ( ::bind(so, addr, addr.GetSize()) == SOCKET_ERROR )
175 TRACE1(
"::bind() error = %d\n", ::WSAGetLastError() );
185 ASSERTLIB(m_bufferSize > 0);
187 m_lpcBuffer =
new char[m_bufferSize];
188 ASSERTLIB(m_lpcBuffer != NULL);
192 m_threadReceiver.
Start();
206 return m_soData != INVALID_SOCKET;
217 return m_soData != INVALID_SOCKET;
239 m_addressee = address;
240 m_isAddressBroadcast =
false;
241 return m_SetAddress(address);
254 m_isAddressBroadcast =
true;
266 virtual bool Send(
size_t dataSize, LPCVOID lpcvData)
268 _super::m_OnSend(dataSize, lpcvData);
269 return m_Send(m_soData, dataSize, lpcvData);
279 m_threadReceiver.
Stop();
284 m_isStopping =
false;
293 return m_threadReceiver;
303 return m_threadReceiver;
314 int lenlen =
sizeof(len);
315 int r = ::getsockopt(m_soData, SOL_SOCKET, SO_RCVBUF,
reinterpret_cast<char *
>(&len), &lenlen);
316 return static_cast<size_t>((r == SOCKET_ERROR) ? 0 : len);
327 if ( size < INT_MAX )
329 int len =
ToInt(size);
330 int r = ::setsockopt(m_soData, SOL_SOCKET, SO_RCVBUF,
reinterpret_cast<char *
>(&len),
sizeof(len));
331 if ( r != SOCKET_ERROR )
351 const int MACSIZE = 6;
352 const int MACLOOP = 16;
353 char sendbuf[MACLOOP * MACSIZE + MACSIZE];
355 ::FillMemory(&sendbuf[0], MACSIZE, 0xFF);
358 ::CopyMemory(&sendbuf[MACSIZE + i * MACSIZE], pbMacAddr, MACSIZE);
360 return udp.
Send(MACLOOP * MACSIZE + MACSIZE, sendbuf);
374 virtual bool m_GetPeerName(CSocketAddress& _address, SOCKET so)
376 _address.Set(m_addrFrom);
387 virtual bool m_Send(SOCKET so,
size_t dataSize, LPCVOID lpcvData)
389 BOOL isAddressBroadcast = m_isAddressBroadcast;
390 if ( m_isBroadcast != (!! isAddressBroadcast) )
392 ::setsockopt(so, SOL_SOCKET, SO_BROADCAST,
reinterpret_cast<char *
>(&isAddressBroadcast),
sizeof(isAddressBroadcast));
393 m_isBroadcast = !! isAddressBroadcast;
395 int iRc = ::sendto(so,
static_cast<const char*
>(lpcvData),
ToInt(dataSize), 0, m_sockaddrPartner, TSockAddr::GetSize());
396 TRACE3(
"::sendto(%u) result = %d error = %d\n", dataSize, iRc, ::WSAGetLastError() );
397 return iRc ==
ToInt(dataSize);
403 void m_CloseDataSocket(
void)
405 if ( m_soData != INVALID_SOCKET )
407 SOCKET so = m_soData;
408 m_soData = INVALID_SOCKET;
409 TRACE1(
"SocketUDP[%u]:Closed\n", m_baseAddress.
GetPortNo() );
417 virtual DWORD Run(
void)
424 int iInterval = _super::GetIdleInterval();
429 FD_SET(m_soData, &tFdSet);
430 int iSelTime = iInterval;
431 timeval tTimeVal = { iSelTime / 1000, iSelTime % 1000 };
432 ::select(
ToInt(m_soData + 1), &tFdSet, NULL, NULL, &tTimeVal);
433 if ( ! FD_ISSET(m_soData, &tFdSet) )
446 TTRACE1(
"SocketUDP[%u]:受信\n", m_baseAddress.
GetPortNo() );
447 int iFromLen = m_addrFrom.GetSize();
448 int iLen = ::recvfrom(m_soData, m_lpcBuffer,
ToInt(m_bufferSize), 0, m_addrFrom, &iFromLen);
449 if ( iLen == SOCKET_ERROR )
451 if ( ! m_isStopping )
453 int iError = ::WSAGetLastError();
454 TRACE2(
"::recv(%d) error = %d\n", m_soData, iError );
465 else if ( iLen != 0 )
468 s.SetData(iLen, m_lpcBuffer);
469 if ( ! m_OnSocketEvent(s) )
489 bool m_isAddressBroadcast;
490 TSockAddr m_addrFrom;
592 _super::m_OnSend(dataSize, lpcvData);
602 _super::SetBroadcastPort(h.
portNo);
608 const BYTE* b =
static_cast<const BYTE*
>(lpcvData);
621 virtual bool Send(
size_t dataSize, LPCVOID lpcvData)
623 _super::m_OnSend(dataSize, lpcvData);
634 _super::SetAddressee(sa);
635 const BYTE* b =
static_cast<const BYTE*
>(lpcvData);
650 dh.
ipAddress = _super::GetAddressee().GetAddress();;
654 dh.
portNo = _super::GetAddressee().GetPortNo();
668 virtual bool m_OnSocketEvent(CSocketEvent& e)
676 TDataHeader* h =
reinterpret_cast<TDataHeader*
>(b);
681 memcpy(&b[
sizeof(TDataHeader)], e.GetData(), e.GetDataSize());
684 return _super::m_OnSocketEvent(se);
#define loop(VAR, CNT)
loop構文.
static void StartUp(void)
[設定] SOCKET初期化.
friend class CSocketEvent
フレンドクラス定義
size_t GetDataSize(void) const
[取得] データ長取得
void SetData(size_t dataSize, LPCVOID lpcData)
[設定] データ設定.
bool HasData(void) const
[確認] Dataを持っているか
WORD GetPortNo(void) const
[取得] ポートNo取得.
DWORD GetAddress(void) const
[取得] IP アドレス取得.
void Get(IN_ADDR &_inaddr) const
[取得] IPアドレス取得.
bool Broadcast(size_t dataSize, LPCVOID lpcvData)
[設定] ブロードバンド送信指定
virtual bool Send(size_t dataSize, LPCVOID lpcvData)
[実行] 送信
TDataHeader AdjustDataHeader(const TDataHeader &h) const
[取得] データヘッダ調整.
CSocketUdpEx(void)
コンストラクタ
CThreadStatus & ReferThreadStatus(void)
[参照] 受信スレッド状態参照.
virtual void Stop(void)
[実行] 切断
const CSocketAddress & GetAddressee(void) const
[取得] 送信先アドレス取得.
virtual bool Send(size_t dataSize, LPCVOID lpcvData)
[実行] 送信
virtual CStr GetReportName(void) const
[取得] コミュニケーション名.
SOCKET GetSocket(void) const
[取得] ソケット取得
virtual DWORD GetReportID(void) const
[取得] コミュニケーションID.
virtual ~CSocketUdp(void)
デストラクタ
bool SetReceivedBufferSize(size_t size)
[設定] 受信バッファサイズ設定.
void SetBroadcastPort(WORD portNo, DWORD address=INADDR_BROADCAST)
[設定] ブロードバンド送信指定
bool SetAddressee(const CSocketAddress &address)
[設定] 送信先アドレス指定
static bool WakeupOnLan(const BYTE *pbMacAddr)
[送信] Wake up on LAN
void SetBaseAddress(const CSocketAddress &address)
[設定] ベースアドレス設定.
WORD GetMaxDatagram(void)
[取得] UDP送信最大サイズ取得.
CThreadStatus & GetReceiveThreadStatus(void)
[参照] 受信スレッド状態参照.
const CSocketAddress & GetBaseAddress(void) const
[取得] ベースアドレス取得.
virtual bool IsConnect(void) const
[確認] 接続チェック.
virtual bool IsAlive(void) const
[確認] Aliveチェック.
virtual bool Start(void)
[実行] 開始
size_t GetReceivedBufferSize(void) const
[取得] 受信バッファサイズ取得.
static CStrT Fmt(const TCHAR *lpszFormat,...)
[作成] 書式付き文字列作成
bool SetRunner(IRunner *pRunner)
[設定] ランナー、設定
bool Stop(DWORD dwWait=15000)
[設定] スレッド停止 スレッドに対して停止要求します。
bool Start(LPCTSTR lpszName=NULL)
[設定] スレッド開始
bool IsPassedAndReset(DWORD dwTime, bool isNow=true)
[確認] 経過確認&リセット.
void Reset(void)
[設定] リセット.
size_t GetSize(void) const
[取得] サイズ取得
void Resize(size_t l)
[設定] サイズ再設定
const TYP * Ref(void) const
[取得] ポインタ取得
int ToInt(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
#define EXCLUSIVE(CLS)
簡易排他制御マクロ.
@ ReportEvent_Disconnect
切断.
bool IsRunnable(void) const
[確認] 実行可能か否か