Hi Ariel,
     Thanks for sharing the information and the sample code. At the moment, I 
am just exploring the concepts, that will be required to build my application. 
Your inputs are certainly helping me make good progress on this.

Warm regards,
Mangesh


-----Original Message-----
From: Ariel Constenla-Haile [mailto:[email protected]] 
Sent: Thursday, February 21, 2013 3:59 PM
To: [email protected]
Subject: Re: communicating with a connected external application using OOBasic 
macro

Hi Mangesh,

On Wed, Feb 20, 2013 at 10:47:16AM +0000, Shukla, Mangesh wrote:
> Hi Ariel, Based on your suggestion below, I have implemented a Socket 
> server in my external application dll , and now listen to it on a 
> different port than the one that it connects with OOo.  I have been 
> able to connect with it from the OOBasic macro function. I am even 
> able to send across a string to the external application using the 
> socket connection. However  I am facing some issues which I have 
> posted on the openoffice forum.  Could you please have a look and let 
> me know if you have any suggestions to make it work.
> 
> http://forum.openoffice.org/en/forum/viewtopic.php?f=44&t=59806

Unless there is a typo, there is an error in the macro

nBytesRead =  oConnection.read()(aByteArray, 200) 

read() takes two arguments, you have read()(ByteArray, 200)


You should be aware that read() blocks until it reads the amount you specify or 
the connection in closed. If your socket server writes 100, the macro will wait 
for other 100.

On the socket code, the logic for reading looks wrong too: 

read(buffer, 256) will return after 256 bytes are read, or the connection is 
closed. With the exception thrown due to your Basic code, the connection gets 
closed by the clean-up performed by the OOo Basic engine, that's why it 
returns; but if the macro writes 200 bytes, read() will wait for the remaining 
6. You better use recv, that tries to read up to some bytes, and you should 
read in a loop (if your buffer is 256 and the peer writes 300, the first recv 
will return 256, the second recv will return 44, -1 on error, and
0 when the peer closed the connection).

Unfortunately, in AOO API the connector uses internally only read(), this means 
that unless you will read/write in a fixed size, you will need to use another 
language, and not AOO API, but the tools provided by that language.
http://opengrok.adfinis-sygroup.org/source/xref/aoo-trunk/main/io/source/connector/ctr_socket.cxx#127
(Side note, flush() does nothing, obviously it does not make sense when they 
use read/write)


Another point, your socket server should be accepting on its own thread, 
otherwise acceptConnection() will block your application.

Attached is a dummy, untested example. It accepts only one connection at the 
time, and after reading the first peer's write, it closes the connection (for 
something more realistic, you'll need a multi-threaded server - I'd use 
boost::asio instead of AOO C++ language binding).


Regards
--
Ariel Constenla-Haile
La Plata, Argentina

Reply via email to