summaryrefslogtreecommitdiff
path: root/lib/SocketClient.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/SocketClient.cc')
-rw-r--r--lib/SocketClient.cc88
1 files changed, 88 insertions, 0 deletions
diff --git a/lib/SocketClient.cc b/lib/SocketClient.cc
new file mode 100644
index 0000000..a86629a
--- /dev/null
+++ b/lib/SocketClient.cc
@@ -0,0 +1,88 @@
+/*
+ * Baltisot
+ * Copyright (C) 1999-2007 Nicolas "Pixel" Noble
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/* $Id: SocketClient.cc,v 1.1 2007-10-12 13:07:42 pixel Exp $ */
+
+#include <TaskMan.h>
+#include <SocketClient.h>
+#include <BRegex.h>
+#include <CopyJob.h>
+#include <ReadJob.h>
+#include <Variables.h>
+
+timeval default_socket_timeout = { 8, 0 };
+
+SocketClient::SocketClient(const String & host, int port, timeval _timeout) : timeout(_timeout) {
+ s.SetNonBlock();
+ s.Connect(host, 25);
+
+ if (s.IsConnected()) {
+ SetBurst();
+ } else {
+ WaitFor(timeout);
+ WaitFor(&s, W4_WRITING);
+ }
+}
+
+SocketClient::~SocketClient() {
+}
+
+String SocketClient::GetStatus() {
+ return status;
+}
+
+void SocketClient::SetStatus(const String & _status) {
+ status = _status;
+}
+
+void SocketClient::subDo() throw (GeneralException) {
+ switch (current) {
+ case 0:
+ if (s.IsConnecting() && (TaskMan::Event() == Task::EVT_HANDLE)) {
+ s.FinalizeConnect();
+ }
+ if (TaskMan::Event() == Task::EVT_TIMEOUT) {
+ status = "Connection timeout";
+ Suspend(TASK_DONE);
+ }
+ if (!s.IsConnected()) {
+ status = "Connection failed";
+ Suspend(TASK_DONE);
+ }
+ RemoveTimeout();
+
+ WaitFor(timeout);
+ return;
+
+ default:
+ if (c)
+ delete c;
+
+ c = 0;
+
+ if (TaskMan::Event() == Task::Task::EVT_TIMEOUT) {
+ status = "Connection timeout.";
+ Suspend(TASK_DONE);
+ }
+ RemoveTimeout();
+
+ WaitFor(timeout);
+ return;
+ }
+}