summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorpixel <pixel>2007-03-05 21:40:44 +0000
committerpixel <pixel>2007-03-05 21:40:44 +0000
commit03aff7d93cf57507da3483601ef692b39330ac33 (patch)
treea4103c9234ed6df3ff6201edb152907ff5bc5357 /lib
parent657c7d68d3fde9ff7042149d51f55ca36e5c8385 (diff)
Socket now have non-blocking connect() support.
Diffstat (limited to 'lib')
-rw-r--r--lib/Socket.cc33
1 files changed, 29 insertions, 4 deletions
diff --git a/lib/Socket.cc b/lib/Socket.cc
index f417bf9..97371a5 100644
--- a/lib/Socket.cc
+++ b/lib/Socket.cc
@@ -35,19 +35,19 @@ class dummy_win32_socket_class_t : public Base {
typedef int socklen_t;
#endif
-Socket::Socket() throw (GeneralException) : Handle(socket(AF_INET, SOCK_STREAM, 0)), connected(false), listening(false), writeclosed(false), readclosed(false) {
+Socket::Socket() throw (GeneralException) : Handle(socket(AF_INET, SOCK_STREAM, 0)), connected(false), listening(false), writeclosed(false), readclosed(false), connecting(false) {
// cerr << "Socket(): connected = " << connected << "; readclosed = " << readclosed << "; writeclosed = " << writeclosed << endl;
if (GetHandle() < 0) {
throw GeneralException(_("Error creating socket."));
}
}
-Socket::Socket(const Socket & s) : Handle(s), connected(s.connected), listening(s.listening), writeclosed(s.writeclosed), readclosed(s.readclosed) {
+Socket::Socket(const Socket & s) : Handle(s), connected(s.connected), listening(s.listening), writeclosed(s.writeclosed), readclosed(s.readclosed), connecting(s.connecting) {
// cerr << "Constructing a socket by copy...\n";
// cerr << "Socket(const Socket &): connected = " << connected << "; readclosed = " << readclosed << "; writeclosed = " << writeclosed << endl;
}
-Socket::Socket(int h) : Handle(h), connected(true), listening(false), writeclosed(false), readclosed(false) { }
+Socket::Socket(int h) : Handle(h), connected(true), listening(false), writeclosed(false), readclosed(false), connecting(false) { }
String Socket::GetName(void) {
return String("socket");
@@ -57,6 +57,10 @@ bool Socket::IsConnected(void) {
return connected;
}
+bool Socket::IsConnecting(void) {
+ return connecting;
+}
+
bool Socket::IsListening(void) {
return listening;
}
@@ -139,7 +143,14 @@ bool Socket::Connect(const String & host, int port) {
// cerr << " - Connected." << endl;
connected = true;
} else {
-// cerr << " - Error connecting: " << strerror(errno) << endl;
+// std::cerr << " - Error connecting: " << strerror(errno) << std::endl;
+#ifdef _WIN32
+ if (WSAGetLastError() == WSAEWOULDBLOCK) {
+#else
+ if (errno == EINPROGRESS) {
+#endif
+ connecting = true;
+ }
}
}
return connected;
@@ -180,6 +191,20 @@ int Socket::GetPort() {
return r;
}
+bool Socket::FinalizeConnect() {
+ int r;
+ socklen_t l = sizeof(r);
+
+ if (getsockopt(GetHandle(), SOL_SOCKET, SO_ERROR, (char *)&r, &l) == 0) {
+ if (r == 0) {
+ connected = true;
+ }
+ }
+ connecting = false;
+
+ return connected;
+}
+
ssize_t Socket::nwrite(const void * buf, size_t count) throw (GeneralException) {
return ::send(GetHandle(), (const char *) buf, count, 0);
}