KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eda_dde.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <condition_variable>
22#include <mutex>
23#include <thread>
24
25#include <eda_dde.h>
26#include <kiway_player.h>
27#include <id.h>
28
29#include <wx/crt.h>
30
31
32static const wxString HOSTNAME( wxT( "localhost" ) );
43{
44public:
46 m_messageReady( false ),
47 m_shutdown( false )
48 {
49 // Do a dummy Connect so that wx will set up the socket stuff on the main thread, which is
50 // required even if you later make socket connections on another thread.
51 wxSocketClient* client = new wxSocketClient;
52 wxIPV4address addr;
53
54 addr.Hostname( HOSTNAME );
55 addr.Service( KICAD_PCB_PORT_SERVICE_NUMBER );
56
57 client->Connect( addr, false );
58
59 client->Close();
60 client->Destroy();
61
62 m_thread = std::thread( &ASYNC_SOCKET_HOLDER::worker, this );
63 }
64
66 {
67 {
68 std::lock_guard<std::mutex> lock( m_mutex );
69 m_shutdown = true;
70 }
71
72 m_cv.notify_one();
73
74 try
75 {
76 if( m_thread.joinable() )
77 m_thread.join();
78 }
79 catch( ... )
80 {
81 }
82 }
83
91 bool Send( int aService, const std::string& aMessage )
92 {
93 if( m_messageReady )
94 return false;
95
96 std::lock_guard<std::mutex> lock( m_mutex );
97
98 m_message = std::make_pair( aService, aMessage );
99 m_messageReady = true;
100 m_cv.notify_one();
101
102 return true;
103 }
104
105private:
109 void worker()
110 {
111 int port;
112 std::string message;
113
114 std::unique_lock<std::mutex> lock( m_mutex );
115
116 while( !m_shutdown )
117 {
118 m_cv.wait( lock, [&]() { return m_messageReady || m_shutdown; } );
119
120 if( m_shutdown )
121 break;
122
123 port = m_message.first;
124 message = m_message.second;
125
126 lock.unlock();
127
128 wxSocketClient* sock_client;
129 wxIPV4address addr;
130
131 // Create a connection
132 addr.Hostname( HOSTNAME );
133 addr.Service( port );
134
135 // Mini-tutorial for Connect() :-)
136 // (JP CHARRAS Note: see wxWidgets: sockets/client.cpp sample)
137 // ---------------------------
138 //
139 // There are two ways to use Connect(): blocking and non-blocking,
140 // depending on the value passed as the 'wait' (2nd) parameter.
141 //
142 // Connect(addr, true) will wait until the connection completes,
143 // returning true on success and false on failure. This call blocks
144 // the GUI (this might be changed in future releases to honor the
145 // wxSOCKET_BLOCK flag).
146 //
147 // Connect(addr, false) will issue a nonblocking connection request
148 // and return immediately. If the return value is true, then the
149 // connection has been already successfully established. If it is
150 // false, you must wait for the request to complete, either with
151 // WaitOnConnect() or by watching wxSOCKET_CONNECTION / LOST
152 // events (please read the documentation).
153 //
154 // WaitOnConnect() itself never blocks the GUI (this might change
155 // in the future to honor the wxSOCKET_BLOCK flag). This call will
156 // return false on timeout, or true if the connection request
157 // completes, which in turn might mean:
158 //
159 // a) That the connection was successfully established
160 // b) That the connection request failed (for example, because
161 // it was refused by the peer.
162 //
163 // Use IsConnected() to distinguish between these two.
164 //
165 // So, in a brief, you should do one of the following things:
166 //
167 // For blocking Connect:
168 //
169 // bool success = client->Connect(addr, true);
170 //
171 // For nonblocking Connect:
172 //
173 // client->Connect(addr, false);
174 //
175 // bool waitmore = true;
176 // while (! client->WaitOnConnect(seconds, millis) && waitmore )
177 // {
178 // // possibly give some feedback to the user,
179 // // update waitmore if needed.
180 // }
181 // bool success = client->IsConnected();
182 //
183 // And that's all :-)
184
185 sock_client = new wxSocketClient( wxSOCKET_BLOCK );
186 sock_client->SetTimeout( 1 ); // Time out in Seconds
187 sock_client->Connect( addr, false );
188 sock_client->WaitOnConnect( 0, 250 );
189
190 if( sock_client->Ok() && sock_client->IsConnected() )
191 {
192 sock_client->SetFlags( wxSOCKET_NOWAIT /*wxSOCKET_WAITALL*/ );
193 sock_client->Write( message.c_str(), message.length() );
194 }
195
196 sock_client->Close();
197 sock_client->Destroy();
198
199 m_messageReady = false;
200
201 lock.lock();
202 }
203 }
204
205 std::thread m_thread;
206 std::pair<int, std::string> m_message;
208 mutable std::mutex m_mutex;
209 std::condition_variable m_cv;
211};
212
213
214std::unique_ptr<ASYNC_SOCKET_HOLDER> socketHolder = nullptr;
215
216
226bool SendCommand( int aService, const std::string& aMessage )
227{
228 if( !socketHolder )
229 socketHolder.reset( new ASYNC_SOCKET_HOLDER() );
230
231 return socketHolder->Send( aService, aMessage );
232}
233
234
236{
237 if( socketHolder )
238 socketHolder.reset();
239}
Spin up a thread to send messages via a socket.
Definition eda_dde.cpp:43
void worker()
Actual task that sends data to the socket server.
Definition eda_dde.cpp:109
std::condition_variable m_cv
Definition eda_dde.cpp:209
std::mutex m_mutex
Definition eda_dde.cpp:208
std::pair< int, std::string > m_message
Definition eda_dde.cpp:206
std::thread m_thread
Definition eda_dde.cpp:205
bool Send(int aService, const std::string &aMessage)
Attempt to send a message if the thread is available.
Definition eda_dde.cpp:91
std::unique_ptr< ASYNC_SOCKET_HOLDER > socketHolder
Definition eda_dde.cpp:214
bool SendCommand(int aService, const std::string &aMessage)
Used by a client to sent (by a socket connection) a data to a server.
Definition eda_dde.cpp:226
void SocketCleanup()
Must be called to clean up the socket thread used by SendCommand.
Definition eda_dde.cpp:235
static const wxString HOSTNAME(wxT("localhost"))
DDE server & client.
#define KICAD_PCB_PORT_SERVICE_NUMBER
Pcbnew listens on this port for commands from Eeschema.
Definition eda_dde.h:36