Debian Codegraph @

CodeLite/SocketAPI/clSocketClient.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : clSocketClient.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25
26 #include "clConnectionString.h"
27 #include "clSocketClient.h"
28
29 #ifndef _WIN32
30 #include <arpa/inet.h>
31 #include <errno.h>
32 #include <netinet/in.h>
33 #include <netinet/ip.h> /* superset of previous */
34 #include <sys/socket.h>
35 #include <sys/un.h>
36 #include <unistd.h>
37 #endif
38
39 #ifdef __WXMSW__
40 #define RESET_ERRNO() WSASetLastError(0)
41 #else
42 #define RESET_ERRNO() errno = 0
43 #endif
44
45 clSocketClient::clSocketClient() {}
46
47 clSocketClient::~clSocketClient() {}
48
49 bool clSocketClient::ConnectLocal(const wxString& socketPath)
50 {
51 DestroySocket();
52 #ifndef __WXMSW__
53 struct sockaddr_un server;
54 m_socket = socket(AF_UNIX, SOCK_STREAM, 0);
55 server.sun_family = AF_UNIX;
56 strcpy(server.sun_path, socketPath.mb_str(wxConvUTF8).data());
57 if(::connect(m_socket, (struct sockaddr*)&server, sizeof(struct sockaddr_un)) < 0) { return false; }
58 return true;
59 #else
60 return false;
61 #endif
62 }
63
64 bool clSocketClient::ConnectRemote(const wxString& address, int port, bool& wouldBlock, bool nonBlockingMode)
65 {
66 wouldBlock = false;
67 DestroySocket();
68 m_socket = ::socket(AF_INET, SOCK_STREAM, 0);
69 if(nonBlockingMode) { MakeSocketBlocking(false); }
70
71 const char* ip_addr = address.mb_str(wxConvUTF8).data();
72 struct sockaddr_in serv_addr;
73 serv_addr.sin_family = AF_INET;
74 serv_addr.sin_port = htons(port);
75
76 #ifndef __WXMSW__
77 if(inet_pton(AF_INET, ip_addr, &serv_addr.sin_addr) <= 0) {
78 // restore socket to blocking mode
79 if(nonBlockingMode) { MakeSocketBlocking(true); }
80 return false;
81 }
82 #else
83 serv_addr.sin_addr.s_addr = inet_addr(ip_addr);
84 #endif
85
86 RESET_ERRNO();
87 int rc = ::connect(m_socket, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
88 #ifdef __WXMSW__
89 wouldBlock = (WSAGetLastError() == WSAEWOULDBLOCK);
90 #else
91 wouldBlock = (errno == EINPROGRESS);
92 #endif
93
94 // restore socket to blocking mode
95 if(nonBlockingMode) { MakeSocketBlocking(true); }
96 return rc == 0;
97 }
98
99 bool clSocketClient::Connect(const wxString& connectionString, bool nonBlockingMode)
100 {
101 clConnectionString cs(connectionString);
102 if(!cs.IsOK()) { return false; }
103 if(cs.GetProtocol() == clConnectionString::kUnixLocalSocket) {
104 return ConnectLocal(cs.GetPath());
105 } else {
106 // TCP
107 bool wouldBlock = false;
108 return ConnectRemote(cs.GetHost(), cs.GetPort(), wouldBlock, nonBlockingMode);
109 }
110 }
111
112 bool clSocketClient::ConnectNonBlocking(const wxString& connectionString, bool& wouldBlock)
113 {
114 wouldBlock = false;
115 clConnectionString cs(connectionString);
116 if(!cs.IsOK()) { return false; }
117 if(cs.GetProtocol() == clConnectionString::kUnixLocalSocket) {
118 return ConnectLocal(cs.GetPath());
119 } else {
120 // TCP
121 return ConnectRemote(cs.GetHost(), cs.GetPort(), wouldBlock, true);
122 }
123 }