A new object oriented network api that can be used to replace the one provided by the EthernetInterface library.
Dependents: NetRelais TCP_Client_Example TCP_Server_Example UDP_Server_Example ... more
socket.cpp
00001 /** 00002 * Copyright (c) 2012, Roy van Dam <roy@vandam-innovations.com> 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright notice, this 00009 * list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright notice, 00011 * this list of conditions and the following disclaimer in the documentation 00012 * and/or other materials provided with the distribution. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00015 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00016 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00017 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 00018 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00019 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00020 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00021 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00022 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00023 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00024 */ 00025 00026 #include "socket.hpp" 00027 using namespace network; 00028 00029 Socket::Socket(): 00030 _status(Socket::Closed), 00031 _socket(-1) 00032 {} 00033 00034 Socket::~Socket() 00035 { 00036 if (this->_status != Socket::Closed) { 00037 this->close(); 00038 } 00039 } 00040 00041 int 00042 Socket::open() 00043 { 00044 return -1; 00045 } 00046 00047 int 00048 Socket::close() 00049 { 00050 if (this->_status == Socket::Closed) { 00051 return -1; 00052 } 00053 00054 // Close the socket 00055 int result = lwip_close(this->_socket); 00056 this->_socket = -1; 00057 00058 // Update status and return 00059 this->_status = Socket::Closed; 00060 return result; 00061 } 00062 00063 int 00064 Socket::bind(int port) 00065 { 00066 // Check socket status 00067 if (this->_status != Socket::Open) { 00068 return -1; 00069 } 00070 00071 // Update local endpoint and create native 00072 struct sockaddr_in native_endpoint; 00073 this->_local_endpoint.setAddress(ip::Address(ip::Address::Any)); 00074 this->_local_endpoint.setPort(port); 00075 this->_local_endpoint.toNative(&native_endpoint); 00076 00077 // Bind socket to endpoint 00078 if (lwip_bind(this->_socket, 00079 (const struct sockaddr *)&native_endpoint, 00080 sizeof(native_endpoint)) < 0) 00081 { 00082 return -1; 00083 } 00084 00085 return 0; 00086 } 00087 00088 ip::Endpoint & 00089 Socket::getRemoteEndpoint() 00090 { 00091 return this->_remote_endpoint; 00092 } 00093 00094 ip::Endpoint & 00095 Socket::getLocalEndpoint() 00096 { 00097 return this->_local_endpoint; 00098 } 00099 00100 int 00101 Socket::getHandle() 00102 { 00103 return this->_socket; 00104 } 00105 00106 enum Socket::Status 00107 Socket::getStatus() 00108 { 00109 return this->_status; 00110 }
Generated on Tue Jul 12 2022 12:58:20 by 1.7.2