Develop and Download Open Source Software

Browse Subversion Repository

Contents of /connection/TcpipAccept.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 191 - (show annotations) (download) (as text)
Fri Jan 4 02:49:36 2008 UTC (16 years, 3 months ago) by satofumi
File MIME type: text/x-c++src
File size: 2879 byte(s)
using namespace beego

1 /*!
2 \file
3 \brief TCP/IP 接続用ポート
4
5 \author Satofumi KAMIMURA
6
7 $Id$
8 */
9
10 #include "TcpipAccept.h"
11 #include "TcpipCtrl.h"
12 #include "NetInit.h"
13 #include "LogManager.h"
14 #include <string>
15
16 using namespace beego;
17
18
19 /*!
20 \brief TcpipAccept の内部クラス
21 */
22 struct TcpipAccept::pImpl : private NetInit {
23 std::string error_message;
24 IPaddress ip;
25 TCPsocket accept_socket;
26 SDLNet_SocketSet accept_set;
27 bool isActive;
28
29 pImpl(void)
30 : error_message("no error"),
31 accept_socket(NULL), accept_set(NULL), isActive(false) {
32 }
33
34 bool errorReturn(const char* base_message) {
35
36 LogManager* log = LogManager::getObject();
37 std::string message = base_message + std::string(SDLNet_GetError());
38 log->write(LogManager::Error, message.c_str());
39
40 return false;
41 }
42 };
43
44
45 TcpipAccept::TcpipAccept(void) : pimpl(new pImpl) {
46 }
47
48
49 TcpipAccept::~TcpipAccept(void) {
50 deactivate();
51 }
52
53
54 const char* TcpipAccept::what(void) {
55 return pimpl->error_message.c_str();
56 }
57
58
59 bool TcpipAccept::activate(unsigned short port) {
60 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
61 if (isActivated()) {
62 return true;
63 }
64
65 if (SDLNet_ResolveHost(&pimpl->ip, NULL, port) < 0) {
66 return pimpl->errorReturn("TcpipAccept::activate(): ");
67 }
68 pimpl->accept_socket = SDLNet_TCP_Open(&pimpl->ip);
69 if (pimpl->accept_socket == NULL) {
70 return pimpl->errorReturn("TcpipAccept::activate(): ");
71 }
72 pimpl->accept_set = SDLNet_AllocSocketSet(1);
73 if (pimpl->accept_set == NULL) {
74 return pimpl->errorReturn("TcpipAccept::activate(): ");
75 }
76 if (SDLNet_TCP_AddSocket(pimpl->accept_set, pimpl->accept_socket) < 0) {
77 return pimpl->errorReturn("TcpipAccept::activate(): ");
78 }
79 pimpl->isActive = true;
80 return true;
81 #else
82 return false;
83 #endif
84 }
85
86
87 void TcpipAccept::deactivate(void) {
88 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
89 if (! isActivated()) {
90 return;
91 }
92
93 if (SDLNet_TCP_DelSocket(pimpl->accept_set, pimpl->accept_socket) < 0) {
94 pimpl->errorReturn("TcpipAccept::deactivate(): ");
95 return;
96 }
97
98 SDLNet_FreeSocketSet(pimpl->accept_set);
99 pimpl->accept_set = NULL;
100
101 SDLNet_TCP_Close(pimpl->accept_socket);
102 pimpl->accept_socket = NULL;
103
104 pimpl->isActive = false;
105 #endif
106 }
107
108
109 bool TcpipAccept::isActivated(void) {
110 return pimpl->isActive;
111 }
112
113
114 /*!
115 \todo エラーメッセージをログ更新するものに置き換える
116 */
117 TcpipCtrl* TcpipAccept::accept(int timeout, SocketSet* socketSet) {
118 #if !HAVE_CONFIG_H || HAVE_LIBSDL_NET
119 if (isActivated() == false) {
120 pimpl->error_message = "TcpipAccept::accept() is invalid.";
121 return NULL;
122 }
123 int n = SDLNet_CheckSockets(pimpl->accept_set, timeout);
124 if ((n <= 0) || (SDLNet_SocketReady(pimpl->accept_socket) < 0)) {
125 return NULL;
126 }
127 TCPsocket socket = SDLNet_TCP_Accept(pimpl->accept_socket);
128 if (socket == NULL) {
129 return NULL;
130 }
131 return new TcpipCtrl(socketSet, socket);
132 #else
133 return NULL;
134 #endif
135 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26