00001
00080 #ifndef INC_MI32_SOCKET_H
00081 #define INC_MI32_SOCKET_H
00082
00083 #ifndef INC_MI32_GENERICRW_H
00084 #include <mi32/genericrw.h>
00085 #endif
00086
00087 #ifndef INC_MI32_MILIST_H
00088 #include <mi32/milist.h>
00089 #endif
00090
00091 #if defined(DEBUG) && !defined(INC_STDIO_H)
00092 #include <stdio.h>
00093 #define INC_STDIO_H
00094 #endif
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 #ifdef MISYSTEMDLL
00105 #define CLASSLIBEXPORT MI_DLLCLASSEXPORT
00106 #else
00107 #define CLASSLIBEXPORT MI_DLLCLASSIMPORT
00108 #endif
00109
00110 #ifndef GENERATING_DOXYGEN_OUTPUT
00111 class STRUTF8;
00112 class MISTRING;
00113 #endif
00114
00120 class CLASSLIBEXPORT MISOCKET : public GENERICRW {
00121 public:
00122
00123 enum FAMILY {
00124 FAMILY_NONE = 0,
00125 FAMILY_LOCAL = 1,
00126 FAMILY_INET = 2
00127 };
00128
00129 enum TYPE {
00130 TYPE_NONE = 0,
00131 TYPE_STREAM = 1,
00132 TYPE_DGRAM = 2
00133 };
00134
00135 class IPADDRLIST;
00136
00141 struct CLASSLIBEXPORT IPADDR {
00142 union {
00143 struct { UINT8 s_b1, s_b2, s_b3, s_b4; } S_un_b;
00144 UINT32 S_addr;
00145 } S_un;
00146
00148 IPADDR (
00149 );
00150
00153 IPADDR (
00154 UINT32 addr
00155 );
00156
00159 int Compare (
00160 const MISOCKET::IPADDR& rhs
00161 ) const;
00162
00165 static void GetAddressList (
00166 IPADDRLIST& AddrList,
00167 bool OnlyRoutable = false
00168 );
00169
00170 void GetString (
00171 MISTRING& mistr,
00172 bool bDoReverseDNSLookup
00173 ) const;
00174
00177 bool IsLoopbackAddr (
00178 ) const;
00179
00185 bool IsRoutable (
00186 ) const;
00187
00190 bool SetByName (
00191 const char* name
00192 );
00193
00194 };
00195
00196
00206 struct CLASSLIBEXPORT ADDR {
00207
00208 unsigned short sa_family;
00209 char __ss_pad1[6];
00210 INT64 __ss_align;
00211 char __ss_pad2[112];
00212
00214 ADDR (
00215 );
00216
00218 ADDR (
00219 const MISOCKET::IPADDR& ip,
00220 int port
00221 );
00222
00228 void GetAddress (
00229 MISTRING& mistr,
00230 bool bDoReverseDNSLookup
00231 ) const;
00232
00234 MISOCKET::FAMILY GetFamily (
00235 ) const;
00236
00238 const MISOCKET::IPADDR& GetIPAddress (
00239 ) const;
00240
00242 const char* GetLocalName (
00243 ) const;
00244
00246 int GetPort (
00247 ) const;
00248
00250 void Set (
00251 const MISOCKET::IPADDR& ip,
00252 int port
00253 );
00254
00256 void SetFamily (
00257 MISOCKET::FAMILY family
00258 );
00259
00261 void SetLocalName (
00262 const char* name
00263 );
00264
00265 protected:
00266 const char* GetBytePtr (int offset) const;
00267 char* GetBytePtr (int offset);
00268 UINT16 GetUINT16(int offset) const;
00269 UINT32 GetUINT32(int offset) const;
00270 void SetUINT16(int offset, UINT16 val);
00271 void SetUINT32(int offset, UINT32 val);
00272
00273 };
00274
00275
00276
00277
00280 class CLASSLIBEXPORT CLIENT {
00281 public:
00282 CLIENT();
00283
00289 CLIENT (
00290 MISOCKET* sock,
00291 bool bTakeOwnership = true
00292 );
00293
00299 virtual ~CLIENT();
00300
00301 bool CallOnAccept (
00302 MISOCKET* socket,
00303 const MISOCKET::ADDR& addr
00304 );
00305
00310 void CallOnClose (
00311 );
00312
00315 void CallOnConnect (
00316 );
00317
00320 void CallOnReadReady (
00321 );
00322
00325 void CallOnTimeOut (
00326 );
00327
00329 void CallOnWriteReady (
00330 );
00331
00333 ERRVALUE Connect (
00334 const MISOCKET::ADDR& address
00335 );
00336
00339 MISOCKET* GetSocket (
00340 ) const;
00341
00342
00344 bool IsConnected (
00345 ) const;
00346
00357 void SetSocket (
00358 MISOCKET* sock,
00359 bool bTakeOwnership = true
00360 );
00361
00362 protected:
00363
00364
00365
00366 class LOCK {
00367 public:
00368 LOCK (
00369 CLIENT* client
00370 ) :
00371 m_client(client)
00372 {
00373 if (m_client != 0) m_client->Lock();
00374 }
00375 ~LOCK (
00376 ) {
00377 if (m_client != 0) m_client->Unlock();
00378 }
00379 private:
00380 CLIENT* m_client;
00381 };
00382
00385 void Destroy (
00386 );
00387
00388 private:
00389
00398 virtual bool v_OnAccept (
00399 MISOCKET* socket,
00400 const MISOCKET::ADDR& addr
00401 );
00402
00407 virtual void v_OnClose (
00408 );
00409
00412 virtual void v_OnConnect (
00413 );
00414
00417 virtual void v_OnReadReady (
00418 );
00419
00422 virtual void v_OnTimeOut (
00423 );
00424
00426 virtual void v_OnWriteReady (
00427 );
00428 #ifndef GENERATING_DOXYGEN_OUTPUT
00429 private:
00430 CLIENT(const CLIENT&);
00431 CLIENT& operator=(const CLIENT&);
00432 friend class LOCK;
00433
00434 MISOCKET* m_sock;
00435 bool m_bWeOwnSocket;
00436 bool m_bNeedToDestroy;
00437 int m_LockCount;
00438
00441 void Lock();
00442
00445 void Unlock();
00446 #endif // GENERATING_DOXYGEN_OUTPUT
00447 };
00448
00449
00451 MISOCKET (
00452 );
00453
00457 virtual ~MISOCKET (
00458 );
00459
00460
00469 MISOCKET* Accept (
00470 ADDR& address
00471 );
00472
00477 ERRVALUE Bind (
00478 const ADDR& address
00479 );
00480
00487 ERRVALUE Connect (
00488 const ADDR& address
00489 );
00490
00494 const ADDR& GetAddress (
00495 ) const;
00496
00502 void GetAddress (
00503 MISTRING& mistr,
00504 bool bDoReverseDNSLookup = false
00505 ) const;
00506
00511 bool GetSockName (
00512 ADDR& addr
00513 ) const;
00514
00516 int GetTimeOut (
00517 ) const;
00518
00520 bool IsNonBlocking (
00521 ) const;
00522
00525 ERRVALUE Listen (
00526 );
00527
00535 ERRVALUE ReadStr (
00536 STRUTF8& string
00537 );
00538
00541 void SetTimeOut (
00542 int seconds
00543 );
00544
00548 void SetNonBlocking (
00549 bool bNonBlocking = true
00550 );
00551
00553 ERRVALUE Socket (
00554 FAMILY family,
00555 TYPE type = TYPE_STREAM
00556 );
00557
00559 ERRVALUE WriteStr (
00560 const char* str
00561 );
00562
00563 #ifndef GENERATING_DOXYGEN_OUTPUT
00564
00565
00566 class FACTORY;
00567 class IMPL;
00568
00569 #ifdef DEBUG
00570
00571 void DumpDebugState (
00572 );
00573 void SetDebugFile (
00574 FILE*
00575 );
00576 void SetCloseDebug (
00577 );
00578 #endif
00579 #endif
00580
00581 private:
00582 #ifndef GENERATING_DOXYGEN_OUTPUT
00583 IMPL* m_impl;
00584
00585
00586 MISOCKET(const MISOCKET&);
00587 MISOCKET& operator=(const MISOCKET&);
00588
00589 MISOCKET(IMPL*);
00590
00591 friend class CLIENT;
00592 friend class IMPL;
00593
00601 void SetClient (
00602 CLIENT* client
00603 );
00604
00605
00606
00607
00608 GENERICRW::CAPABILITIES v_GetCapabilities (
00609 ) const;
00610
00611
00612
00613 INT64 v_GetSize (
00614 ) const;
00615
00616 INT64 v_GetSizeLimit (
00617 ) const;
00618
00619
00620
00621
00622
00623 INT32 v_Read (
00624 void* buffer,
00625 INT32 numbytes,
00626 bool AllowPartial = false
00627 );
00628
00629 ERRVALUE v_Resize (
00630 INT64 newsize
00631 );
00632
00633 INT64 v_Seek (
00634 INT64 offet,
00635 SEEKFROM from
00636 );
00637
00638 INT64 v_Tell (
00639 ) const;
00640
00641 ERRVALUE v_Write (
00642 const void* buffer,
00643 INT32 numbytes
00644 );
00645
00646
00647 #endif // GENERATING_DOXYGEN_OUTPUT
00648 };
00649
00651 inline bool operator== (
00652 const MISOCKET::IPADDR& lhs,
00653 const MISOCKET::IPADDR& rhs
00654 ) { return (lhs.Compare(rhs) == 0); }
00655
00656 inline bool operator!= (
00657 const MISOCKET::IPADDR& lhs,
00658 const MISOCKET::IPADDR& rhs
00659 ) { return (lhs.Compare(rhs) != 0); }
00660
00661 inline bool operator< (
00662 const MISOCKET::IPADDR& lhs,
00663 const MISOCKET::IPADDR& rhs
00664 ) { return (lhs.Compare(rhs) == -1); }
00665
00666 class CLASSLIBEXPORT MISOCKET::IPADDRLIST : public MILIST<MISOCKET::IPADDR> {};
00667
00668 #undef CLASSLIBEXPORT
00669
00670 #endif // INC_MI32_SOCKET_H
00671
00672
00673
00674