[Mono-dev] Non-TCP/IP socket access
Robert Jordan
robertj at gmx.net
Fri Jul 22 10:15:25 EDT 2011
On 22.07.2011 15:40, Robert Jordan wrote:
> On 22.07.2011 15:00, Andy Hume wrote:
>>> From: mono-devel-list-bounces at lists.ximian.com
>>> [mailto:mono-devel-list-bounces at lists.ximian.com] On Behalf
>>> Of Robert Jordan
>>> Sent: 22 July 2011 12:43
>>> To: mono-devel-list at lists.ximian.com
>>> Subject: Re: [Mono-dev] Non-TCP/IP socket access
>>>
>>> On 22.07.2011 13:08, Andy Hume wrote:
>>>> Presumably I can't convert my fd to a wapi handle somehow... So is
>>>> that plan not going to work? :-,(
>>>
>>> It won't work. The whole socket machinery assumes type
>>> AF_UNIX or AF_INET. If you set a fd of some other address
>>> family type, a lot of nasty things could happen if this check
>>> wasn't there.
>>>
>>> Get/SetSocketOptions, Local/RemoteEndPoint etc. simply don't
>>> cope with other AF types. They would return garbage at best.
>>>
>> I actually don't need to call any of those functions -- and if I were I
>> could just P/Invoke them...[1] :-,(
>>
>> The bit I obviously don't want to have to write myself is anything to do
>> with I/O. That's a huge amount of difficult work and its all there and
>> working in Mono and would work for my (SOCK_STREAM) socket if I could
>> just create the socket somehow. :-,(
>>
>> All I need is that wapi handle...[2] Do we think it's impossible to
>> access it.
>
> It looks like you could be able to create the socket with
> io-layers's function:
>
> [DllImport("__Internal")]
> static extern int _wapi_socket(int domain, int type,
> int protocol, IntPtr unused1, int unused2, int unused3);
Thank God this doesn't work because _wapi_* symbols are marked
as private.
But there is another way:
1) create an INET socket with Mono's System.Net.Sockets.Socket
class
2) get its FD via reflection
3) close it via p/invoke with close(2) (see "man 2 close")
4) create your socket via p/invoke with socket(2)
5) don't close the socket create in (4) yourself, because
WAPI will take care of it.
6) use the socket created in (1) throughout your application
Because Unix is reusing descriptors, the socket created in (4)
will have the same number, and WAPI will be happy.
Put this code inside a lock to prevent that some other
thread is stealing your fd.
Robert
More information about the Mono-devel-list
mailing list