Develop and Download Open Source Software

Browse Subversion Repository

Contents of /connection/UdpipCtrl.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 361 - (show annotations) (download) (as text)
Thu Apr 17 13:32:46 2008 UTC (15 years, 11 months ago) by satofumi
File MIME type: text/x-c++src
File size: 2931 byte(s)
adjust for windows
1 /*!
2 \file
3 \brief UDP/IP 接続
4
5 \author Satofumi KAMIMURA
6
7 $Id$
8
9 \todo いずれ、UDPpacket を管理した受信の仕組みに置き換えるべき
10 \todo つか、使い方の意味を書き出しておくべき。忘れてきた
11
12 \todo 必要があれば、SDLNet_UDP_RecvV, SDLNet_UDP_SendV を用いた実装に変更する
13 */
14
15 #include "UdpipCtrl.h"
16 #include "NetInit.h"
17 #include <SDL_net.h>
18 #include <string>
19
20 using namespace beego;
21
22
23 struct UdpipCtrl::pImpl : private NetInit {
24 std::string error_message;
25 long port;
26 UDPsocket socket;
27 int channel;
28 IPaddress from_ip;
29
30 pImpl(long port_)
31 : error_message("no error."), port(port_), socket(SDLNet_UDP_Open(port)),
32 channel(-1) {
33
34 if (! socket) {
35 // !!! エラー出力
36 return;
37 }
38 }
39
40 ~pImpl(void) {
41 SDLNet_UDP_Close(socket);
42 socket = NULL;
43 }
44 };
45
46
47 UdpipCtrl::UdpipCtrl(long port) : pimpl(new pImpl(port)) {
48 }
49
50
51 UdpipCtrl::~UdpipCtrl(void) {
52 }
53
54
55 const char* UdpipCtrl::what(void) {
56 return pimpl->error_message.c_str();
57 }
58
59
60 bool UdpipCtrl::connect(const char* host, long port) {
61
62 IPaddress ip;
63 int ret = SDLNet_ResolveHost(&ip, host, static_cast<short>(port));
64 if (ret < 0) {
65 // !!! エラー出力
66 return false;
67 }
68
69 ret = SDLNet_UDP_Bind(pimpl->socket, pimpl->channel, &ip);
70 if (ret < 0) {
71 // !!! エラー出力
72 return false;
73 }
74 pimpl->channel = ret;
75
76 return true;
77 }
78
79
80 void UdpipCtrl::disconnect(void) {
81
82 SDLNet_UDP_Unbind(pimpl->socket, 0);
83 }
84
85
86 bool UdpipCtrl::isConnected(void) {
87
88 // UDP/IP はコネクションレスなので、常に false を返す
89 return false;
90 }
91
92
93 bool UdpipCtrl::changeBaudrate(long baudrate) {
94
95 // 実装しない
96 return true;
97 }
98
99
100 int UdpipCtrl::send(const char* data, int size) {
101 if (! pimpl->socket) {
102 // !!! エラーメッセージの更新
103 return -1;
104 }
105
106 // !!! この型の意味は、UDPpacket をまとめて管理しろ、ってことなのかな?
107 UDPpacket packet;
108 packet.channel = pimpl->channel;
109
110 // !!! C++ のキャストに置き換えたいなぁ...
111 //packet.data = static_cast<Uint8*>(const_cast<char*>(data));
112 packet.data = (Uint8*)data;
113 packet.len = size;
114
115 return SDLNet_UDP_Send(pimpl->socket, pimpl->channel, &packet);
116 }
117
118
119 int UdpipCtrl::recv(char* data, int size, int timeout) {
120
121 UDPpacket packet;
122 packet.channel = pimpl->channel;
123 // !!! C++ のキャストに置き換えたいなぁ...
124 packet.data = (Uint8*)data;
125 packet.maxlen = size;
126
127 int ret = SDLNet_UDP_Recv(pimpl->socket, &packet);
128 if (ret <= 0) {
129 return ret;
130 }
131
132 pimpl->from_ip = packet.address;
133 return packet.len;
134 }
135
136
137 int UdpipCtrl::size(int timeout) {
138
139 // !!!
140 return -1;
141 }
142
143
144 void UdpipCtrl::clear(void) {
145
146 // !!!
147 }
148
149
150 void UdpipCtrl::skip(int total_timeout) {
151
152 // !!!
153 }
154
155
156 void UdpipCtrl::flush(void) {
157
158 // !!!
159 }
160
161
162 void UdpipCtrl::disconnect(const char* host, long port) {
163
164 IPaddress ip;
165 int ret = SDLNet_ResolveHost(&ip, host, static_cast<short>(port));
166 if (ret < 0) {
167 // !!! エラー出力
168 return;
169 }
170
171 SDLNet_UDP_Unbind(pimpl->socket, pimpl->channel);
172 }

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