OSDN Git Service

2491fc26daa43c823f504ef8b62bc3a7015b76a7
[internetcity/prototype.git] / common / CSocket.h
1 #ifndef INC_CSocket
2 #define INC_CSocket
3
4 // \83C\83\93\83N\83\8b\81[\83h\82Í\95K\82¸\95K\97v\82È\95¨\82¾\82¯
5 // \93à\95\94\82Å\97\98\97p\82·\82é\82¾\82¯\82Ì\95¨\82Í\83\\81[\83X\82É\8bL\8fq
6 // winsock2.h\82Íwindows.h\82æ\82è\90æ\82É\83C\83\93\83N\83\8b\81[\83h\82µ\82È\82¯\82ê\82Î\82È\82ç\82È\82¢\82ç\82µ\82¢
7 //#include <winsock2.h>
8 #include "CThread.h"
9 #include "CRingBuffer.h"
10
11 // \8aÖ\90\94\96¼\82Í\83\8d\81[\83\8f\83L\83\83\83\81\83\8b\83P\81[\83X\82Æ\82·\82é
12
13 const int MAX_STRING_BUFFER_SIZE = 256;
14
15 struct CommunicationBuffer
16 {
17         char m_Buffer[MAX_STRING_BUFFER_SIZE];
18         // \8ai\94[\82³\82ê\82Ä\82¢\82é\95\8e\9a\90\94
19         size_t m_Length;
20
21         CommunicationBuffer()
22         {
23                 m_Length = 0;
24         }
25 };
26
27 ////////////////////////////////////////////////////////////////////////////////
28 // CSocket
29 ////////////////////////////////////////////////////////////////////////////////
30 class CSocket : public CThread
31 {
32         ////////////////////////////////////////////////////////////////////////////////
33         // \83\81\83\93\83o\95Ï\90\94
34         ////////////////////////////////////////////////////////////////////////////////
35 private:
36         WSADATA m_WsaData;
37         SOCKET m_Socket;
38         sockaddr_in m_SocketAddress;
39
40         bool m_isConnect;
41
42         CRingBuffer<CommunicationBuffer> m_Buffer;
43
44         ////////////////////////////////////////////////////////////////////////////////
45         // \83R\83\93\83X\83g\83\89\83N\83^
46         ////////////////////////////////////////////////////////////////////////////////
47 public:
48         CSocket();
49
50         ////////////////////////////////////////////////////////////////////////////////
51         // \83f\83X\83g\83\89\83N\83^
52         ////////////////////////////////////////////////////////////////////////////////
53 public:
54         virtual ~CSocket();
55
56         ////////////////////////////////////////////////////////////////////////////////
57         // static\8aÖ\90\94\81A\83R\81[\83\8b\83o\83b\83N\8aÖ\90\94
58         ////////////////////////////////////////////////////////////////////////////////
59         ////////////////////////////////////////////////////////////////////////////////
60         // \91\80\8dì\83\81\83\\83b\83h
61         ////////////////////////////////////////////////////////////////////////////////
62 public:
63         // \8f\89\8aú\89»
64         virtual bool initialize();
65         // \94ñ\8f\89\8aú\89»
66         virtual bool uninitialize();
67         // connection
68         virtual bool connection(SOCKET* io_pSocket = NULL, sockaddr_in* io_pSockAddress = NULL);
69         // disconnection
70         virtual bool disconnection();
71         // shutdown
72         virtual bool shutdown(unsigned int in_Kind);
73         // send
74         virtual bool send(const CommunicationBuffer* in_pCommunicationBuffer);
75         // receive
76         virtual bool receive(CommunicationBuffer** out_ppCommunicationBuffer);
77
78 protected:
79         // \83X\83\8c\83b\83h\83\8b\81[\83v
80         void run();
81
82
83         ////////////////////////////////////////////////////////////////////////////////
84         // \91®\90«\83\81\83\\83b\83h
85         ////////////////////////////////////////////////////////////////////////////////
86 public:
87         SOCKET* getSocket()
88         {
89                 return &m_Socket;
90         }
91
92         sockaddr* getSocketAddress()
93         {
94                 return reinterpret_cast<sockaddr*>(&m_SocketAddress);
95         }
96
97         ////////////////////////////////////////////////////////////////////////////////
98         // \83C\83e\83\8c\81[\83V\83\87\83\93
99         ////////////////////////////////////////////////////////////////////////////////
100         ////////////////////////////////////////////////////////////////////////////////
101         // \8fó\91Ô
102         ////////////////////////////////////////////////////////////////////////////////
103 public:
104         bool isConnect() const
105         {
106                 return m_isConnect;
107         }
108
109         ////////////////////////////////////////////////////////////////////////////////
110         // \89\89\8eZ\8eq
111         ////////////////////////////////////////////////////////////////////////////////
112         ////////////////////////////////////////////////////////////////////////////////
113         // \8f\83\90\88\89¼\91z\8aÖ\90\94\81A\92è\8b`\82Ì\82Ý\81i\94ñ\8eÀ\91\95\92ñ\8b\9f\81j
114         ////////////////////////////////////////////////////////////////////////////////
115         ////////////////////////////////////////////////////////////////////////////////
116         // \93à\95\94\83N\83\89\83X
117         ////////////////////////////////////////////////////////////////////////////////
118
119
120 };
121
122 #endif //INC_CSocket