![](/media/cache/profiles/1f47cef5e38c952f94c5d61726027439.50x50_q85.jpg)
Based on PS3_BlueUSB with reference to http://blog.goo.ne.jp/roboz80/e/10e7bf38d3a63b996ca2894e9fb5e3b6
Socket.cpp@0:44619612f575, 2011-07-05 (annotated)
- Committer:
- kenbumono
- Date:
- Tue Jul 05 08:25:40 2011 +0000
- Revision:
- 0:44619612f575
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenbumono | 0:44619612f575 | 1 | /* |
kenbumono | 0:44619612f575 | 2 | Copyright (c) 2010 Peter Barrett |
kenbumono | 0:44619612f575 | 3 | |
kenbumono | 0:44619612f575 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
kenbumono | 0:44619612f575 | 5 | of this software and associated documentation files (the "Software"), to deal |
kenbumono | 0:44619612f575 | 6 | in the Software without restriction, including without limitation the rights |
kenbumono | 0:44619612f575 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kenbumono | 0:44619612f575 | 8 | copies of the Software, and to permit persons to whom the Software is |
kenbumono | 0:44619612f575 | 9 | furnished to do so, subject to the following conditions: |
kenbumono | 0:44619612f575 | 10 | |
kenbumono | 0:44619612f575 | 11 | The above copyright notice and this permission notice shall be included in |
kenbumono | 0:44619612f575 | 12 | all copies or substantial portions of the Software. |
kenbumono | 0:44619612f575 | 13 | |
kenbumono | 0:44619612f575 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kenbumono | 0:44619612f575 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kenbumono | 0:44619612f575 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kenbumono | 0:44619612f575 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kenbumono | 0:44619612f575 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenbumono | 0:44619612f575 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
kenbumono | 0:44619612f575 | 20 | THE SOFTWARE. |
kenbumono | 0:44619612f575 | 21 | */ |
kenbumono | 0:44619612f575 | 22 | |
kenbumono | 0:44619612f575 | 23 | /* |
kenbumono | 0:44619612f575 | 24 | Tue Apr 26 2011 Bart Janssens: added a socket listener |
kenbumono | 0:44619612f575 | 25 | */ |
kenbumono | 0:44619612f575 | 26 | |
kenbumono | 0:44619612f575 | 27 | #include <stdio.h> |
kenbumono | 0:44619612f575 | 28 | #include <stdlib.h> |
kenbumono | 0:44619612f575 | 29 | #include <stdio.h> |
kenbumono | 0:44619612f575 | 30 | #include <string.h> |
kenbumono | 0:44619612f575 | 31 | |
kenbumono | 0:44619612f575 | 32 | #include "Utils.h" |
kenbumono | 0:44619612f575 | 33 | #include "Socket.h" |
kenbumono | 0:44619612f575 | 34 | |
kenbumono | 0:44619612f575 | 35 | #define MAX_SOCKET_HANDLERS 3 |
kenbumono | 0:44619612f575 | 36 | #define MAX_SOCKETS 16 |
kenbumono | 0:44619612f575 | 37 | #define MAX_LISTEN 8 |
kenbumono | 0:44619612f575 | 38 | |
kenbumono | 0:44619612f575 | 39 | class SocketInternalPad |
kenbumono | 0:44619612f575 | 40 | { |
kenbumono | 0:44619612f575 | 41 | public: |
kenbumono | 0:44619612f575 | 42 | SocketInternal si; |
kenbumono | 0:44619612f575 | 43 | u8 pad[8]; |
kenbumono | 0:44619612f575 | 44 | }; |
kenbumono | 0:44619612f575 | 45 | |
kenbumono | 0:44619612f575 | 46 | |
kenbumono | 0:44619612f575 | 47 | |
kenbumono | 0:44619612f575 | 48 | |
kenbumono | 0:44619612f575 | 49 | class SocketManager |
kenbumono | 0:44619612f575 | 50 | { |
kenbumono | 0:44619612f575 | 51 | SocketHandler* _handlers[MAX_SOCKET_HANDLERS]; |
kenbumono | 0:44619612f575 | 52 | SocketInternalPad _sockets[MAX_SOCKETS]; |
kenbumono | 0:44619612f575 | 53 | SocketListener _listeners[MAX_LISTEN]; |
kenbumono | 0:44619612f575 | 54 | |
kenbumono | 0:44619612f575 | 55 | public: |
kenbumono | 0:44619612f575 | 56 | SocketManager() |
kenbumono | 0:44619612f575 | 57 | { |
kenbumono | 0:44619612f575 | 58 | memset(_handlers,0,sizeof(_handlers)); |
kenbumono | 0:44619612f575 | 59 | memset(_sockets,0,sizeof(_sockets)); |
kenbumono | 0:44619612f575 | 60 | memset(_listeners,0,sizeof(_listeners)); |
kenbumono | 0:44619612f575 | 61 | } |
kenbumono | 0:44619612f575 | 62 | |
kenbumono | 0:44619612f575 | 63 | SocketHandler* GetHandler(int type) |
kenbumono | 0:44619612f575 | 64 | { |
kenbumono | 0:44619612f575 | 65 | if (type < 1 || type > MAX_SOCKET_HANDLERS) |
kenbumono | 0:44619612f575 | 66 | return 0; |
kenbumono | 0:44619612f575 | 67 | return _handlers[type - 1]; |
kenbumono | 0:44619612f575 | 68 | } |
kenbumono | 0:44619612f575 | 69 | |
kenbumono | 0:44619612f575 | 70 | SocketInternal* GetInternal(int s) |
kenbumono | 0:44619612f575 | 71 | { |
kenbumono | 0:44619612f575 | 72 | if (s < 1 || s > MAX_SOCKETS) |
kenbumono | 0:44619612f575 | 73 | return 0; |
kenbumono | 0:44619612f575 | 74 | return &_sockets[s - 1].si; |
kenbumono | 0:44619612f575 | 75 | } |
kenbumono | 0:44619612f575 | 76 | |
kenbumono | 0:44619612f575 | 77 | int RegisterSocketHandler(int type, SocketHandler* handler) |
kenbumono | 0:44619612f575 | 78 | { |
kenbumono | 0:44619612f575 | 79 | if (type < 1 || type > MAX_SOCKET_HANDLERS) |
kenbumono | 0:44619612f575 | 80 | return ERR_SOCKET_TYPE_NOT_FOUND; |
kenbumono | 0:44619612f575 | 81 | _handlers[type - 1] = handler; |
kenbumono | 0:44619612f575 | 82 | return 0; |
kenbumono | 0:44619612f575 | 83 | } |
kenbumono | 0:44619612f575 | 84 | |
kenbumono | 0:44619612f575 | 85 | int Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData) |
kenbumono | 0:44619612f575 | 86 | { |
kenbumono | 0:44619612f575 | 87 | SocketHandler* h = GetHandler(type); |
kenbumono | 0:44619612f575 | 88 | if (!h) |
kenbumono | 0:44619612f575 | 89 | return ERR_SOCKET_TYPE_NOT_FOUND; |
kenbumono | 0:44619612f575 | 90 | |
kenbumono | 0:44619612f575 | 91 | for (int i = 0; i < MAX_SOCKETS; i++) |
kenbumono | 0:44619612f575 | 92 | { |
kenbumono | 0:44619612f575 | 93 | SocketInternal* si = (SocketInternal*)(_sockets+i); |
kenbumono | 0:44619612f575 | 94 | if (si->ID == 0) |
kenbumono | 0:44619612f575 | 95 | { |
kenbumono | 0:44619612f575 | 96 | //printf("Call to Socket Manager Open \r\n"); |
kenbumono | 0:44619612f575 | 97 | si->ID = i+1; |
kenbumono | 0:44619612f575 | 98 | si->Type = type; |
kenbumono | 0:44619612f575 | 99 | si->Callback = callback; |
kenbumono | 0:44619612f575 | 100 | si->userData = userData; |
kenbumono | 0:44619612f575 | 101 | return h->Open(si,addr); |
kenbumono | 0:44619612f575 | 102 | } |
kenbumono | 0:44619612f575 | 103 | } |
kenbumono | 0:44619612f575 | 104 | return ERR_SOCKET_NONE_LEFT; |
kenbumono | 0:44619612f575 | 105 | } |
kenbumono | 0:44619612f575 | 106 | |
kenbumono | 0:44619612f575 | 107 | SocketInternal* Create(int type, SocketAddrHdr* addr, int port) |
kenbumono | 0:44619612f575 | 108 | { |
kenbumono | 0:44619612f575 | 109 | SocketInternal* si; |
kenbumono | 0:44619612f575 | 110 | SocketListener* li; |
kenbumono | 0:44619612f575 | 111 | SocketHandler* h = GetHandler(type); |
kenbumono | 0:44619612f575 | 112 | if (!h) |
kenbumono | 0:44619612f575 | 113 | return 0; |
kenbumono | 0:44619612f575 | 114 | |
kenbumono | 0:44619612f575 | 115 | for (int i = 0; i < MAX_SOCKETS; i++) |
kenbumono | 0:44619612f575 | 116 | { |
kenbumono | 0:44619612f575 | 117 | si = (SocketInternal*)(_sockets+i); |
kenbumono | 0:44619612f575 | 118 | if (si->ID == 0) |
kenbumono | 0:44619612f575 | 119 | { |
kenbumono | 0:44619612f575 | 120 | si->ID = i+1; |
kenbumono | 0:44619612f575 | 121 | si->State = SocketState_Listen; |
kenbumono | 0:44619612f575 | 122 | si->Type = type; |
kenbumono | 0:44619612f575 | 123 | si->port = port; |
kenbumono | 0:44619612f575 | 124 | for (int i = 0; i < MAX_LISTEN; i++){ |
kenbumono | 0:44619612f575 | 125 | li = (SocketListener*)(_listeners+i); |
kenbumono | 0:44619612f575 | 126 | if (( li->Type == si->Type )&& (li->port == si->port)) { |
kenbumono | 0:44619612f575 | 127 | si->Callback = li->Callback; |
kenbumono | 0:44619612f575 | 128 | si->userData = li->userData; |
kenbumono | 0:44619612f575 | 129 | h->Create(si,addr); |
kenbumono | 0:44619612f575 | 130 | return si; |
kenbumono | 0:44619612f575 | 131 | } |
kenbumono | 0:44619612f575 | 132 | |
kenbumono | 0:44619612f575 | 133 | } |
kenbumono | 0:44619612f575 | 134 | } |
kenbumono | 0:44619612f575 | 135 | } |
kenbumono | 0:44619612f575 | 136 | |
kenbumono | 0:44619612f575 | 137 | } |
kenbumono | 0:44619612f575 | 138 | |
kenbumono | 0:44619612f575 | 139 | |
kenbumono | 0:44619612f575 | 140 | int Listen(int type, int port, SocketCallback callback,void* userData) |
kenbumono | 0:44619612f575 | 141 | { |
kenbumono | 0:44619612f575 | 142 | SocketListener* li; |
kenbumono | 0:44619612f575 | 143 | SocketHandler* h = GetHandler(type); |
kenbumono | 0:44619612f575 | 144 | if (!h) return ERR_SOCKET_TYPE_NOT_FOUND; |
kenbumono | 0:44619612f575 | 145 | |
kenbumono | 0:44619612f575 | 146 | //printf("Call to Socket Manager Listen \r\n"); |
kenbumono | 0:44619612f575 | 147 | for (int i = 0; i < MAX_LISTEN; i++) |
kenbumono | 0:44619612f575 | 148 | { |
kenbumono | 0:44619612f575 | 149 | li = (SocketListener*)(_listeners+i); |
kenbumono | 0:44619612f575 | 150 | if (( li->Type == type )&& (li->port == port)) { |
kenbumono | 0:44619612f575 | 151 | //printf("Port %d is already in use\r\n",port); |
kenbumono | 0:44619612f575 | 152 | return ERR_SOCKET_IN_USE; //in use |
kenbumono | 0:44619612f575 | 153 | } |
kenbumono | 0:44619612f575 | 154 | } |
kenbumono | 0:44619612f575 | 155 | |
kenbumono | 0:44619612f575 | 156 | for (int i = 0; i < MAX_LISTEN; i++) |
kenbumono | 0:44619612f575 | 157 | { |
kenbumono | 0:44619612f575 | 158 | li = (SocketListener*)(_listeners+i); |
kenbumono | 0:44619612f575 | 159 | if (( li->Type == 0 )&& (li->port == 0)) { |
kenbumono | 0:44619612f575 | 160 | li->ID = i+1; |
kenbumono | 0:44619612f575 | 161 | li->Type = type; |
kenbumono | 0:44619612f575 | 162 | li->port = port; |
kenbumono | 0:44619612f575 | 163 | li->Callback = callback; |
kenbumono | 0:44619612f575 | 164 | li->userData = userData; |
kenbumono | 0:44619612f575 | 165 | //printf("Listening on port %d \r\n",port); |
kenbumono | 0:44619612f575 | 166 | return 0; |
kenbumono | 0:44619612f575 | 167 | } |
kenbumono | 0:44619612f575 | 168 | } |
kenbumono | 0:44619612f575 | 169 | //printf("Max listen ports reached\r\n",port); |
kenbumono | 0:44619612f575 | 170 | return ERR_SOCKET_NONE_LEFT; |
kenbumono | 0:44619612f575 | 171 | } |
kenbumono | 0:44619612f575 | 172 | |
kenbumono | 0:44619612f575 | 173 | int InUse(int type, int port) |
kenbumono | 0:44619612f575 | 174 | { |
kenbumono | 0:44619612f575 | 175 | SocketListener* li; |
kenbumono | 0:44619612f575 | 176 | SocketHandler* h = GetHandler(type); |
kenbumono | 0:44619612f575 | 177 | if (!h) return ERR_SOCKET_TYPE_NOT_FOUND; |
kenbumono | 0:44619612f575 | 178 | for (int i = 0; i < MAX_LISTEN; i++) |
kenbumono | 0:44619612f575 | 179 | { |
kenbumono | 0:44619612f575 | 180 | li = (SocketListener*)(_listeners+i); |
kenbumono | 0:44619612f575 | 181 | if (( li->Type == type )&& (li->port == port)) { |
kenbumono | 0:44619612f575 | 182 | |
kenbumono | 0:44619612f575 | 183 | //printf("Listen check on port %d OK\r\n",port); |
kenbumono | 0:44619612f575 | 184 | return 0; |
kenbumono | 0:44619612f575 | 185 | } |
kenbumono | 0:44619612f575 | 186 | } |
kenbumono | 0:44619612f575 | 187 | //printf("Listen check on port %d NOK\r\n",port); |
kenbumono | 0:44619612f575 | 188 | return ERR_SOCKET_NONE_LEFT; |
kenbumono | 0:44619612f575 | 189 | } |
kenbumono | 0:44619612f575 | 190 | |
kenbumono | 0:44619612f575 | 191 | |
kenbumono | 0:44619612f575 | 192 | int Accept(int socket, SocketCallback callback, void* userData) |
kenbumono | 0:44619612f575 | 193 | { |
kenbumono | 0:44619612f575 | 194 | SocketInternal* si = GetInternal(socket); |
kenbumono | 0:44619612f575 | 195 | if (!si || si->ID != socket) |
kenbumono | 0:44619612f575 | 196 | return ERR_SOCKET_NOT_FOUND; |
kenbumono | 0:44619612f575 | 197 | |
kenbumono | 0:44619612f575 | 198 | si->Callback = callback; |
kenbumono | 0:44619612f575 | 199 | si->userData = userData; |
kenbumono | 0:44619612f575 | 200 | |
kenbumono | 0:44619612f575 | 201 | //printf("Call to Socket Manager Accept \r\n"); |
kenbumono | 0:44619612f575 | 202 | return 0; |
kenbumono | 0:44619612f575 | 203 | |
kenbumono | 0:44619612f575 | 204 | } |
kenbumono | 0:44619612f575 | 205 | |
kenbumono | 0:44619612f575 | 206 | int Send(int socket, const u8* data, int len) |
kenbumono | 0:44619612f575 | 207 | { |
kenbumono | 0:44619612f575 | 208 | //printf("Call to Socket Manager Send \r\n"); |
kenbumono | 0:44619612f575 | 209 | SocketInternal* si = GetInternal(socket); |
kenbumono | 0:44619612f575 | 210 | //printf("socket = %d si->ID = %d si->Type = %d \r\n", socket, si->ID, si->Type); |
kenbumono | 0:44619612f575 | 211 | if (!si || si->ID != socket){ |
kenbumono | 0:44619612f575 | 212 | //printf("send: socket not found \r\n"); |
kenbumono | 0:44619612f575 | 213 | return ERR_SOCKET_NOT_FOUND; |
kenbumono | 0:44619612f575 | 214 | } |
kenbumono | 0:44619612f575 | 215 | //printf("Calling l2cap send \r\n"); |
kenbumono | 0:44619612f575 | 216 | |
kenbumono | 0:44619612f575 | 217 | SocketHandler* h = GetHandler(si->Type); |
kenbumono | 0:44619612f575 | 218 | if (!h) { |
kenbumono | 0:44619612f575 | 219 | //printf("Send: no socket type found \r\n"); |
kenbumono | 0:44619612f575 | 220 | return ERR_SOCKET_TYPE_NOT_FOUND; |
kenbumono | 0:44619612f575 | 221 | } |
kenbumono | 0:44619612f575 | 222 | return h->Send(si,data,len); |
kenbumono | 0:44619612f575 | 223 | |
kenbumono | 0:44619612f575 | 224 | } |
kenbumono | 0:44619612f575 | 225 | |
kenbumono | 0:44619612f575 | 226 | int Close(int socket) |
kenbumono | 0:44619612f575 | 227 | { |
kenbumono | 0:44619612f575 | 228 | SocketInternal* si = GetInternal(socket); |
kenbumono | 0:44619612f575 | 229 | if (!si || si->ID != socket) |
kenbumono | 0:44619612f575 | 230 | return ERR_SOCKET_NOT_FOUND; |
kenbumono | 0:44619612f575 | 231 | si->ID = 0; |
kenbumono | 0:44619612f575 | 232 | return GetHandler(si->Type)->Close(si); |
kenbumono | 0:44619612f575 | 233 | } |
kenbumono | 0:44619612f575 | 234 | }; |
kenbumono | 0:44619612f575 | 235 | |
kenbumono | 0:44619612f575 | 236 | SocketManager gSocketManager; |
kenbumono | 0:44619612f575 | 237 | |
kenbumono | 0:44619612f575 | 238 | int Socket_Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData) |
kenbumono | 0:44619612f575 | 239 | { |
kenbumono | 0:44619612f575 | 240 | //printf("Call to Socket Open \r\n"); |
kenbumono | 0:44619612f575 | 241 | return gSocketManager.Open(type,addr,callback,userData); |
kenbumono | 0:44619612f575 | 242 | } |
kenbumono | 0:44619612f575 | 243 | |
kenbumono | 0:44619612f575 | 244 | SocketInternal* Socket_Create(int type, SocketAddrHdr* addr, int port) |
kenbumono | 0:44619612f575 | 245 | { |
kenbumono | 0:44619612f575 | 246 | return gSocketManager.Create(type, addr, port); |
kenbumono | 0:44619612f575 | 247 | } |
kenbumono | 0:44619612f575 | 248 | |
kenbumono | 0:44619612f575 | 249 | int Socket_Send(int socket, const u8* data, int len) |
kenbumono | 0:44619612f575 | 250 | { |
kenbumono | 0:44619612f575 | 251 | //printf("Call to Socket_Send \r\n"); |
kenbumono | 0:44619612f575 | 252 | return gSocketManager.Send(socket,data,len); |
kenbumono | 0:44619612f575 | 253 | } |
kenbumono | 0:44619612f575 | 254 | |
kenbumono | 0:44619612f575 | 255 | int Socket_Close(int socket) |
kenbumono | 0:44619612f575 | 256 | { |
kenbumono | 0:44619612f575 | 257 | return gSocketManager.Close(socket); |
kenbumono | 0:44619612f575 | 258 | } |
kenbumono | 0:44619612f575 | 259 | |
kenbumono | 0:44619612f575 | 260 | int Socket_Listen(int type, int port,SocketCallback callback, void* userData) |
kenbumono | 0:44619612f575 | 261 | { |
kenbumono | 0:44619612f575 | 262 | //printf("Call to Socket_Listen \r\n"); |
kenbumono | 0:44619612f575 | 263 | return gSocketManager.Listen(type, port,callback,userData); |
kenbumono | 0:44619612f575 | 264 | } |
kenbumono | 0:44619612f575 | 265 | |
kenbumono | 0:44619612f575 | 266 | int Socket_InUse(int type, int port) |
kenbumono | 0:44619612f575 | 267 | { |
kenbumono | 0:44619612f575 | 268 | //printf("Call to Socket_InUse \r\n"); |
kenbumono | 0:44619612f575 | 269 | return gSocketManager.InUse(type, port); |
kenbumono | 0:44619612f575 | 270 | } |
kenbumono | 0:44619612f575 | 271 | |
kenbumono | 0:44619612f575 | 272 | int Socket_Accept(int socket, SocketCallback callback, void* userData) |
kenbumono | 0:44619612f575 | 273 | { |
kenbumono | 0:44619612f575 | 274 | //printf("Call to Socket_Accept \r\n"); |
kenbumono | 0:44619612f575 | 275 | return gSocketManager.Accept(socket, callback, userData); |
kenbumono | 0:44619612f575 | 276 | } |
kenbumono | 0:44619612f575 | 277 | |
kenbumono | 0:44619612f575 | 278 | int RegisterSocketHandler(int type, SocketHandler* handler) |
kenbumono | 0:44619612f575 | 279 | { |
kenbumono | 0:44619612f575 | 280 | return gSocketManager.RegisterSocketHandler(type,handler); |
kenbumono | 0:44619612f575 | 281 | } |
kenbumono | 0:44619612f575 | 282 | |
kenbumono | 0:44619612f575 | 283 | SocketInternal* GetSocketInternal(int socket) |
kenbumono | 0:44619612f575 | 284 | { |
kenbumono | 0:44619612f575 | 285 | return gSocketManager.GetInternal(socket); |
kenbumono | 0:44619612f575 | 286 | } |
kenbumono | 0:44619612f575 | 287 |