Roy van Dam / NetworkAPI

Dependents:   NetRelais TCP_Client_Example TCP_Server_Example UDP_Server_Example ... more

Revision:
4:d854fa394f85
Parent:
3:d30db8752485
Child:
8:cdee0f2b6ff0
diff -r d30db8752485 -r d854fa394f85 tcp/socket.cpp
--- a/tcp/socket.cpp	Wed Jul 18 11:22:37 2012 +0000
+++ b/tcp/socket.cpp	Wed Jul 18 13:20:32 2012 +0000
@@ -37,7 +37,7 @@
     // Open socket
     this->_socket = ::socket(AF_INET, SOCK_STREAM, 0);
     if (this->_socket < 0) {
-        return -1;
+        return -2;
     }
     
     // Update status and return
@@ -46,7 +46,29 @@
 }
 
 int
-Socket::connect(ip::Address &address, int port)
+Socket::connect(const char *hostname, int port)
+{
+    ip::Address address;
+    if (address.fromHostname(hostname) < 0) {
+        return -1;
+    }
+    
+    return this->connect(address, port);
+}
+
+int
+Socket::connect(const std::string hostname, int port)
+{
+    ip::Address address;
+    if (address.fromHostname(hostname) < 0) {
+        return -1;
+    }
+    
+    return this->connect(address, port);
+}
+
+int
+Socket::connect(const ip::Address &address, int port)
 {
     ip::Endpoint endpoint(address, port);
     return this->connect(endpoint);
@@ -56,7 +78,7 @@
 Socket::connect(ip::Endpoint &endpoint)
 {
     // Check socket status
-    if ((this->_status != Socket::Open) ||
+    if ((this->_status != Socket::Open) &&
         (this->_status != Socket::Disconnected))
     {
         return -1;
@@ -72,7 +94,7 @@
     
     // Check result
     if (result < 0) {
-        return -1;
+        return -2;
     }
     
     // Update remote endpoint information.
@@ -94,7 +116,7 @@
     // Attempt to shutdown the connection.
     int result = ::shutdown(this->_socket, SHUT_RDWR);
     if (result < 0) {
-        return -1;
+        return -2;
     }
     
     // Update status and return
@@ -113,7 +135,7 @@
     // Put socket into listening mode.
     int result = ::listen(this->_socket, max_pending);
     if (result < 0) {
-        return -1;
+        return -2;
     }
     
     // Update status and return
@@ -131,7 +153,7 @@
     
     // Check client socket status
     if (client._status != Socket::Closed) {
-        return -1;
+        return -2;
     }
     
     // Create native endpoint
@@ -145,7 +167,7 @@
     
     // Did we succeed?
     if (socket < 0) {
-        return -1;
+        return -3;
     }
     
     // Check if we received the endpoint information correctly.
@@ -162,6 +184,12 @@
 }
 
 int
+Socket::write(Buffer &buffer)
+{
+    return this->write(buffer.pointer(), buffer.length());
+}
+
+int
 Socket::write(void *data, size_t size)
 {
     // Check data buffer and size
@@ -171,7 +199,7 @@
 
     // Check socket status
     if (this->_status != Socket::Connected) {
-        return -1;
+        return -2;
     }
     
     // Update status
@@ -189,6 +217,16 @@
     return bytes_written;
 }
 
+int
+Socket::read(Buffer &buffer)
+{
+    int result = this->read(buffer.pointer(), buffer.size());
+    if (result >= 0) {
+        buffer.setLength(result);
+    }
+    
+    return result;
+}
 
 int
 Socket::read(void *data, size_t max_size)
@@ -200,7 +238,7 @@
 
     // Check socket status
     if (this->_status != Socket::Connected) {
-        return -1;
+        return -2;
     }
     
     // Update status