メインページ | ネームスペース一覧 | クラス階層 | 構成 | Directories | ファイル一覧 | ネームスペースメンバ | 構成メンバ | ファイルメンバ | 関連ページ

WBSockStr.h

00001 #ifndef WINCMP_SOCKSTR_H
00002 #define WINCMP_SOCKSTR_H
00003 #include "WBWin.h"
00004 #include <winsock2.h>
00005 #include <Base/StrBufBase.h>
00006 
00007 namespace Spr {;
00008 
00009 
00010 class WBSockStream;
00011 /// TCP/IPソケットを iostream の streambuffer に結びつけたもの.WBSockStream が使用する.
00012 class SPR_DLL WBSockStreambuf:public UTStreambufBase{
00013     friend WBSockStream;
00014 protected:
00015     SOCKET sock;                ///<    通信に使用するソケット
00016     SOCKET sockListen;          ///<    サーバになった場合,接続待ち(listen) に使用するソケット
00017     UINT port;                  ///<    接続・接続待ちに使用するポート番号
00018     SOCKADDR_IN oppAdr;         ///<    接続相手のアドレス
00019     int lastError;              ///<    エラー番号
00020     const char* lastErrorStr;   ///<    エラーメッセージ
00021     bool bConnected;            ///<    接続しているかどうか
00022     bool bListening;            ///<    listen しているかどうか
00023     bool bServer;               ///<    サーバかどうか
00024     WSAEVENT closeEvent;        ///<    相手がソケットを閉じたことを検出するためのイベント
00025 public:
00026     /// コンストラクタ
00027     WBSockStreambuf(char* gb, int gl, char* pb, int pl);
00028     /// デストラクタ
00029     BCC_CDECL ~WBSockStreambuf();
00030     /// 接続しているかどうか
00031     bool IsConnected();
00032     /// listenしているかどうか
00033     bool IsListening(){return bListening;}
00034     /// サーバかどうか
00035     bool IsServer(){return bServer;}
00036     /// サーバになってListen(接続要求待ち) する
00037     bool Listen(UINT port);
00038     /// 接続要求を受け入れる
00039     bool Accept(bool bBlock);
00040     /// クライアントになって接続要求をする.
00041     bool Connect(const char* adr, UINT port);
00042     /// 接続を閉じる
00043     void Close();
00044 protected:
00045     UINT read(void* buf, UINT bufLen);
00046     UINT write(void* buf, UINT bufLen);
00047     virtual int in_avail_stream();
00048     const char* ErrorMessage();
00049     const char* ErrorNumToMessage(int errorNum);
00050     void GetError();
00051     int AddSockRef();
00052     int ReleaseSockRef();
00053     static int sockRefCount;
00054 };
00055 
00056 /// TCP/IPソケットを iostream に結びつけたもの
00057 class SPR_DLL WBSockStream:public std::iostream{
00058 protected:
00059     WBSockStreambuf buf;        ///<    socket 用 streambuffer
00060     enum{BUFFERLEN = 1000};     ///<    iostreamのバッファの大きさ
00061     char getBuffer[BUFFERLEN];  ///<    iostreamの入力バッファ
00062     char putBuffer[BUFFERLEN];  ///<    iostreamの出力バッファ
00063 public:
00064     WBSockStream();             ///<    コンストラクタ
00065     BCC_CDECL ~WBSockStream();          ///<    デストラクタ
00066     /// エラーメッセージ
00067     const char* ErrorMessage(){ return buf.ErrorMessage(); }
00068     /// クライアントになって接続しに行く.
00069     bool connect(const char* addr, UINT port){ return Connect(addr, port); }
00070     bool Connect(const char* addr, UINT port){
00071         bool rv =  buf.Connect(addr, port);
00072         if (rv) clear();
00073         return rv;
00074     }
00075     /// サーバーとなって接続を待つ.
00076     bool listen(UINT port){ return Listen(port); }
00077     bool Listen(UINT port){ return buf.Listen(port); }
00078     /// 接続を受け付ける.
00079     bool accept(bool bBlock = false){ return Accept(bBlock); }
00080     bool Accept(bool bBlock = false){
00081         bool rv = buf.Accept(bBlock);
00082         if (rv) clear();
00083         return rv;
00084     }
00085     /// 接続しているかどうか
00086     bool IsConnected(){ return buf.IsConnected(); }
00087     /// listenしているかどうか
00088     bool IsListening(){ return buf.IsListening(); }
00089     /// すぐに入力可能なバイト数
00090     int avail(){ return Avail(); }
00091     int Avail(){ return buf.in_avail() + buf.in_avail_stream(); }
00092     ///  接続を切断する.
00093     void close(){ Close(); }
00094     void Close(){ buf.Close(); clear(std::ios::badbit); }
00095 };
00096 }   //  namespace Spr
00097 #endif

Springheadに対してSun Apr 16 01:57:58 2006に生成されました。  doxygen 1.4.1