A Simple TCP & UDP Socket Library
Dependents: SimpleSocketExamples 1.0
ServerSocket.cpp@0:3eb1ce25eac4, 2012-06-05 (annotated)
- Committer:
- yamaguch
- Date:
- Tue Jun 05 05:56:49 2012 +0000
- Revision:
- 0:3eb1ce25eac4
1.1.3; fixed 0x00 reception bug
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yamaguch | 0:3eb1ce25eac4 | 1 | /* |
yamaguch | 0:3eb1ce25eac4 | 2 | Copyright (c) 2011, Senio Networks, Inc. |
yamaguch | 0:3eb1ce25eac4 | 3 | |
yamaguch | 0:3eb1ce25eac4 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
yamaguch | 0:3eb1ce25eac4 | 5 | of this software and associated documentation files (the "Software"), to deal |
yamaguch | 0:3eb1ce25eac4 | 6 | in the Software without restriction, including without limitation the rights |
yamaguch | 0:3eb1ce25eac4 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
yamaguch | 0:3eb1ce25eac4 | 8 | copies of the Software, and to permit persons to whom the Software is |
yamaguch | 0:3eb1ce25eac4 | 9 | furnished to do so, subject to the following conditions: |
yamaguch | 0:3eb1ce25eac4 | 10 | |
yamaguch | 0:3eb1ce25eac4 | 11 | The above copyright notice and this permission notice shall be included in |
yamaguch | 0:3eb1ce25eac4 | 12 | all copies or substantial portions of the Software. |
yamaguch | 0:3eb1ce25eac4 | 13 | |
yamaguch | 0:3eb1ce25eac4 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
yamaguch | 0:3eb1ce25eac4 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
yamaguch | 0:3eb1ce25eac4 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
yamaguch | 0:3eb1ce25eac4 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
yamaguch | 0:3eb1ce25eac4 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yamaguch | 0:3eb1ce25eac4 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
yamaguch | 0:3eb1ce25eac4 | 20 | THE SOFTWARE. |
yamaguch | 0:3eb1ce25eac4 | 21 | */ |
yamaguch | 0:3eb1ce25eac4 | 22 | |
yamaguch | 0:3eb1ce25eac4 | 23 | #include "SimpleSocket.h" |
yamaguch | 0:3eb1ce25eac4 | 24 | |
yamaguch | 0:3eb1ce25eac4 | 25 | ServerSocket::ServerSocket(int port, bool debug) |
yamaguch | 0:3eb1ce25eac4 | 26 | : accepted(false), debug(debug) { |
yamaguch | 0:3eb1ce25eac4 | 27 | ssocket.setOnEvent(this, &ServerSocket::onTCPSocketEvent); |
yamaguch | 0:3eb1ce25eac4 | 28 | |
yamaguch | 0:3eb1ce25eac4 | 29 | SocketError bindErr = ssocket.bind(Host(IpAddr(), port)); |
yamaguch | 0:3eb1ce25eac4 | 30 | DBG("Init bind... %s\n", bindErr.toString()); |
yamaguch | 0:3eb1ce25eac4 | 31 | |
yamaguch | 0:3eb1ce25eac4 | 32 | SocketError listenErr = ssocket.listen(); |
yamaguch | 0:3eb1ce25eac4 | 33 | DBG("Init listen... %s\n", listenErr.toString()); |
yamaguch | 0:3eb1ce25eac4 | 34 | } |
yamaguch | 0:3eb1ce25eac4 | 35 | |
yamaguch | 0:3eb1ce25eac4 | 36 | void ServerSocket::onTCPSocketEvent(TCPSocketEvent e) { |
yamaguch | 0:3eb1ce25eac4 | 37 | DBG(SocketEvent(e)); |
yamaguch | 0:3eb1ce25eac4 | 38 | |
yamaguch | 0:3eb1ce25eac4 | 39 | switch (e) { |
yamaguch | 0:3eb1ce25eac4 | 40 | case TCPSOCKET_ACCEPT: |
yamaguch | 0:3eb1ce25eac4 | 41 | accepted = true; |
yamaguch | 0:3eb1ce25eac4 | 42 | break; |
yamaguch | 0:3eb1ce25eac4 | 43 | case TCPSOCKET_DISCONNECTED: |
yamaguch | 0:3eb1ce25eac4 | 44 | ssocket.close(); |
yamaguch | 0:3eb1ce25eac4 | 45 | break; |
yamaguch | 0:3eb1ce25eac4 | 46 | } |
yamaguch | 0:3eb1ce25eac4 | 47 | } |
yamaguch | 0:3eb1ce25eac4 | 48 | |
yamaguch | 0:3eb1ce25eac4 | 49 | ClientSocket ServerSocket::accept(float timeout) { |
yamaguch | 0:3eb1ce25eac4 | 50 | TCPSocket* socket = 0; |
yamaguch | 0:3eb1ce25eac4 | 51 | Host host; |
yamaguch | 0:3eb1ce25eac4 | 52 | timer.reset(); |
yamaguch | 0:3eb1ce25eac4 | 53 | timer.start(); |
yamaguch | 0:3eb1ce25eac4 | 54 | |
yamaguch | 0:3eb1ce25eac4 | 55 | do { |
yamaguch | 0:3eb1ce25eac4 | 56 | Net::poll(); |
yamaguch | 0:3eb1ce25eac4 | 57 | if (accepted) { |
yamaguch | 0:3eb1ce25eac4 | 58 | accepted = false; |
yamaguch | 0:3eb1ce25eac4 | 59 | SocketError err = ssocket.accept(&host, &socket); |
yamaguch | 0:3eb1ce25eac4 | 60 | if (!err) { |
yamaguch | 0:3eb1ce25eac4 | 61 | IpAddr ip = host.getIp(); |
yamaguch | 0:3eb1ce25eac4 | 62 | DBG("TCP connection from %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]); |
yamaguch | 0:3eb1ce25eac4 | 63 | break; |
yamaguch | 0:3eb1ce25eac4 | 64 | } else { |
yamaguch | 0:3eb1ce25eac4 | 65 | DBG("accept error:%s\n", err.toString()); |
yamaguch | 0:3eb1ce25eac4 | 66 | } |
yamaguch | 0:3eb1ce25eac4 | 67 | } |
yamaguch | 0:3eb1ce25eac4 | 68 | } while (timer.read() < timeout); |
yamaguch | 0:3eb1ce25eac4 | 69 | |
yamaguch | 0:3eb1ce25eac4 | 70 | return ClientSocket(host.getIp(), socket, debug); |
yamaguch | 0:3eb1ce25eac4 | 71 | } |
yamaguch | 0:3eb1ce25eac4 | 72 | |
yamaguch | 0:3eb1ce25eac4 | 73 | void ServerSocket::setDebug(bool debug) { |
yamaguch | 0:3eb1ce25eac4 | 74 | this->debug = debug; |
yamaguch | 0:3eb1ce25eac4 | 75 | } |