C K Kashyap | 28 Feb 07:14 2013
Picon

How to return a network connection to C

Hi,
I am using Network.Connection to connect to gmail in my Haskell module - that's compiled to DLL and invoked from C.

I need a mechanism to return the connection handle to C so that it can pass it in the subsequent calls. How can I achieve this?

Regards,
Kashyap
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Niklas Hambüchen | 28 Feb 16:34 2013

Re: How to return a network connection to C

What data type are you dealing with exactly?

If you have a socket, I guess you can just use it from C (via FFI).

PS:
By "Network.Connection", do you mean
http://hackage.haskell.org/package/network-connection-0.1.1 ? Seems
deprecated.

On 28/02/13 06:14, C K Kashyap wrote:
> Hi,
> I am using Network.Connection to connect to gmail in my Haskell module -
> that's compiled to DLL and invoked from C.
> 
> I need a mechanism to return the connection handle to C so that it can
> pass it in the subsequent calls. How can I achieve this?
> 
> Regards,
> Kashyap
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
C K Kashyap | 28 Feb 18:31 2013
Picon

Re: How to return a network connection to C

I am using http://hackage.haskell.org/package/connection.
So I create network connection in Haskell 

getConnection :: IO Connection

I'd like this connection to be returned to C so that subsequent calls from C can send in the connection handle.

Regards,
Kashyap


On Thu, Feb 28, 2013 at 9:04 PM, Niklas Hambüchen <mail <at> nh2.me> wrote:
What data type are you dealing with exactly?

If you have a socket, I guess you can just use it from C (via FFI).

PS:
By "Network.Connection", do you mean
http://hackage.haskell.org/package/network-connection-0.1.1 ? Seems
deprecated.

On 28/02/13 06:14, C K Kashyap wrote:
> Hi,
> I am using Network.Connection to connect to gmail in my Haskell module -
> that's compiled to DLL and invoked from C.
>
> I need a mechanism to return the connection handle to C so that it can
> pass it in the subsequent calls. How can I achieve this?
>
> Regards,
> Kashyap
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Krzysztof Skrzętnicki | 28 Feb 19:28 2013
Picon

Re: How to return a network connection to C

If I understand you correctly you need a StablePtr to your Connection value. Please see: http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.6.0.1/Foreign-StablePtr.html

Best regards,
Krzysztof Skrzętnicki


On Thu, Feb 28, 2013 at 6:31 PM, C K Kashyap <ckkashyap <at> gmail.com> wrote:
I am using http://hackage.haskell.org/package/connection.
So I create network connection in Haskell 

getConnection :: IO Connection

I'd like this connection to be returned to C so that subsequent calls from C can send in the connection handle.

Regards,
Kashyap


On Thu, Feb 28, 2013 at 9:04 PM, Niklas Hambüchen <mail <at> nh2.me> wrote:
What data type are you dealing with exactly?

If you have a socket, I guess you can just use it from C (via FFI).

PS:
By "Network.Connection", do you mean
http://hackage.haskell.org/package/network-connection-0.1.1 ? Seems
deprecated.

On 28/02/13 06:14, C K Kashyap wrote:
> Hi,
> I am using Network.Connection to connect to gmail in my Haskell module -
> that's compiled to DLL and invoked from C.
>
> I need a mechanism to return the connection handle to C so that it can
> pass it in the subsequent calls. How can I achieve this?
>
> Regards,
> Kashyap
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Donn Cave | 28 Feb 19:37 2013

Re: How to return a network connection to C

Quoth C K Kashyap <ckkashyap <at> gmail.com>,

> I am using http://hackage.haskell.org/package/connection.
> So I create network connection in Haskell
>
> getConnection :: IO Connection
>
> I'd like this connection to be returned to C so that subsequent calls from
> C can send in the connection handle.

According to the documentation, he doesn't export enough of the
Connection type to access the handle inside, and there appears to
be no function provided to do that either.  So it looks to me like
you'd have to 1) make the connection "by hand" with Network.Socket.connect
etc., 2) get the Socket fd, 3) make a Handle from the Socket,
4) pass that to connectFromHandle, and 5) use the fd with your
C function.

Note that the socket connection itself, represented by the fd that
you return to C, will simply transmit data back and forth without
modification.  Specifically without SSL encryption.  If you need
SSL encryption in both Haskell and C, it would probably be simpler
to implement the I/O in C and call it from Haskell.

	Donn
C K Kashyap | 28 Feb 19:59 2013
Picon

Re: How to return a network connection to C

Okay, for now I have figured out a working solution to the original problem I was facing .....

I just forkIO another IO action that does the actual network work and in addition, it opens a server socket and waits for commands.

The calls from C reach Haskell, from where a connection is made to the server and command is sent. A little convoluted but works. I have the whole implementation here - https://github.com/ckkashyap/gmail
So now I am able to create a standalone, self sufficient DLL that I can just hand off to the folks in my org and they can write a C program and connect to gmail!!!!

Hey Donn ... when you say, implement the IO in C, you also imply implementing the SSL stuff too right? ... that's one thing I wanted to avoid - and I was also reluctant to use something like openSSL because that would come in the way of making standalone, self - sufficient DLL.

I really have to see what kind of performance hit my DLL has ...

Regards,
Kashyap


On Fri, Mar 1, 2013 at 12:07 AM, Donn Cave <donn <at> avvanta.com> wrote:
Quoth C K Kashyap <ckkashyap <at> gmail.com>,

> I am using http://hackage.haskell.org/package/connection.
> So I create network connection in Haskell
>
> getConnection :: IO Connection
>
> I'd like this connection to be returned to C so that subsequent calls from
> C can send in the connection handle.

According to the documentation, he doesn't export enough of the
Connection type to access the handle inside, and there appears to
be no function provided to do that either.  So it looks to me like
you'd have to 1) make the connection "by hand" with Network.Socket.connect
etc., 2) get the Socket fd, 3) make a Handle from the Socket,
4) pass that to connectFromHandle, and 5) use the fd with your
C function.

Note that the socket connection itself, represented by the fd that
you return to C, will simply transmit data back and forth without
modification.  Specifically without SSL encryption.  If you need
SSL encryption in both Haskell and C, it would probably be simpler
to implement the I/O in C and call it from Haskell.

        Donn

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Donn Cave | 28 Feb 20:08 2013

Re: How to return a network connection to C

Quoth C K Kashyap <ckkashyap <at> gmail.com>,

> Hey Donn ... when you say, implement the IO in C, you also imply
> implementing the SSL stuff too right?

Yes, if you need encrypted I/O from C.

	Donn

Gmane