473,241 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Async Sockets, Buffer Pinning

Ok so I think everyone can agree that creating buffers on the fly in
an async socket server is bad ... there is alot of literature
available on the problems this will cause with the heap. I am looking
at a few options to get around this.

1) Have a BufferPool class that hands out ArraySegment<byteportions
of a larger array (large enough that it would be in the LOH). If all
of the array is used create another big segment.

2) Create a bunch of smaller arrays for use by the bufferpool class
and have it hand them back

In both 1 & 2 I would probably have the connection use their buffer
for the duration of the connection. I would internally hold a list of
the free blocks. When a connection was done ith its buffer it would
have to release it back to this pool. My thought is that #2 might be
better for dealing with cases where I want to shrink the number of
buffers allocated from the previous maximum if needed.

In general I lean towards #1 ... but figured I would check if I might
be missing something.

Thanks in advance,

Greg Young
Aug 3 '06 #1
4 3529
As I understand it, we have two issue with async buffers. The .net buffers
and the system buffers used by the hardware for the io. IIRC, they did some
work in .Net 2.0 to help with clr side of things to make it much better -
are you still running into an issue here? However, we still have the issue
of the hardware using system read/write buffers and using up nonpaged memory
which limits the number of working sockets and also means a pinvoke buffer
copy I think (is this right?). It would be cool if they could somehow use
the actual .Net buffer and write directly into that from the hardware. Or
are they doing that already?

--
William Stacey [MVP]

"Greg Young" <dr*******************@hotmail.comwrote in message
news:u%***************@TK2MSFTNGP06.phx.gbl...
| Ok so I think everyone can agree that creating buffers on the fly in
| an async socket server is bad ... there is alot of literature
| available on the problems this will cause with the heap. I am looking
| at a few options to get around this.
|
| 1) Have a BufferPool class that hands out ArraySegment<byteportions
| of a larger array (large enough that it would be in the LOH). If all
| of the array is used create another big segment.
|
| 2) Create a bunch of smaller arrays for use by the bufferpool class
| and have it hand them back
|
| In both 1 & 2 I would probably have the connection use their buffer
| for the duration of the connection. I would internally hold a list of
| the free blocks. When a connection was done ith its buffer it would
| have to release it back to this pool. My thought is that #2 might be
| better for dealing with cases where I want to shrink the number of
| buffers allocated from the previous maximum if needed.
|
| In general I lean towards #1 ... but figured I would check if I might
| be missing something.
|
| Thanks in advance,
|
| Greg Young
|
|
Aug 4 '06 #2
That makes sense. Thanks. So the hardware is using buffers regardless out
of non-paged pool. When we post a buffer (from the heap), there is still a
copy from hw buffer to pinned buffer?

--
William Stacey [MVP]

"Vadym Stetsyak" <va*****@ukr.netwrote in message
news:Oz**************@TK2MSFTNGP03.phx.gbl...
| Hello, William!
|
| WSMAs I understand it, we have two issue with async buffers. The .net
| WSMbuffers and the system buffers used by the hardware for the io.
IIRC,
| WSMthey did some work in .Net 2.0 to help with clr side of things to
make
| WSMit much better - are you still running into an issue here? However,
| WSMwe still have the issue of the hardware using system read/write
| WSMbuffers and using up nonpaged memory which limits the number of
| WSMworking sockets and also means a pinvoke buffer copy I think (is this
| WSMright?). It would be cool if they could somehow use the actual .Net
| WSMbuffer and write directly into that from the hardware. Or are they
| WSMdoing that already?
|
| AFAIK hardware is using its own buffers for network I/O.
|
| In unmanaged sockets we specify user buffer in the sockets call ( e.g send
or WSASend ).
| Then when winsock call comes to kernel mode - data is copied from that
user buffer to
| internal driver buffer.
|
| The same is with recv ( WSAReceive ) - when data is received driver copies
data
| to user supplied buffer.
|
| In managed world using Reflector we can see that Socket.Send method has
| fixed (byte* numRef1 = buffer)
| {
| num1 =
UnsafeNclNativeMethods.OSSOCK.send(this.m_Handle.D angerousGetHandle(),
numRef1 + offset, size, socketFlags);
| }
|
| that is, unmanaged winsock function obtains direct pointer on our managed
buffer, which is pinned until call ends.
| The same is valid for async call, however, pinning occurs in more complex
way.
|
| --
| Regards, Vadym Stetsyak
| www: http://vadmyst.blogspot.com
Aug 4 '06 #3
Hello, William!
You wrote on Fri, 4 Aug 2006 12:53:44 -0400:

WSMThat makes sense. Thanks. So the hardware is using buffers
WSMregardless out of non-paged pool. When we post a buffer (from the
WSMheap), there is still a copy from hw buffer to pinned buffer?

When data is received - underlying driver copies it to user supplied buffer,
in
case of .NET it will be our pinned buffer.

There are drivers out there that can use user mode buffers, but
generally drivers do not trust user allocated buffers :8-)

--
Regards, Vadym Stetsyak.
Blog: http://vadmyst.blogspot.com
Aug 4 '06 #4
Basically you are correct, you have "user mode" buffers and "kernel mode"
buffers and somehow you need to copy the buffers across the user-kernel
boundary (exceptions are SAN network stacks who will use "Windows Socket
Direct"). However,the layered network stack (kernel) is much more complex
than that, each component in the stack can have it's own buffers allocated
to store it's data or some components can user shared cached memory buffers.
Anyway the kernel mode buffers are not shared with user mode (Winsock)
buffers, so there is at least one copy function needed to pass data across
the boundary.

Willy.

"Vadym Stetsyak" <va*****@ukr.netwrote in message
news:OF**************@TK2MSFTNGP06.phx.gbl...
| Hello, William!
| You wrote on Fri, 4 Aug 2006 12:53:44 -0400:
|
| WSMThat makes sense. Thanks. So the hardware is using buffers
| WSMregardless out of non-paged pool. When we post a buffer (from the
| WSMheap), there is still a copy from hw buffer to pinned buffer?
|
| When data is received - underlying driver copies it to user supplied
buffer,
| in
| case of .NET it will be our pinned buffer.
|
| There are drivers out there that can use user mode buffers, but
| generally drivers do not trust user allocated buffers :8-)
|
| --
| Regards, Vadym Stetsyak.
| Blog: http://vadmyst.blogspot.com
|
|
Aug 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joshua Coady | last post by:
I am writing a class that includes several async methods following the BeginXxx, EndXxx pattern. Some of these methods will call methods in the framework such as Stream.Read or Socket.Send. Do I...
1
by: Ben | last post by:
I've written a fair amount of sockets code using the Winsock2 API, but I am having some trouble converting to the .Net Sockets API, specifically asynchronous sockets. What I have is a form that is...
4
by: Roemer | last post by:
Hey all While working with Async Methods and Networking I found something very strange or better a bug? To test it yourself create a new Windows Application with just a Button on the Form. ...
6
by: Aero | last post by:
Hi, My window application written in C# is throwing following exception while connecting to one FTP location The type initializer for System.Net.Sockets.Socket threw an exception This exe is...
6
by: Shak | last post by:
Hi all, Three questions really: 1) The async call to the networkstream's endread() (or even endxxx() in general) blocks. Async calls are made on the threadpool - aren't we advised not to...
5
by: Arno | last post by:
reposted with the right microsoft managed newsgroup ID: Sorry for the inconvinience Hi, I've written a class for client-socket connection, but I get a lot of times the error message "Unable...
4
by: nyhetsgrupper | last post by:
I'm writing a server application connection to multiple clients using sockets. I want one socket for each client, and the comunication needs to be async. I could use the async sockets methods...
15
by: dennis.richardson | last post by:
Greetings all. Here's a problem that's been driving me nuts for the last 48 hours. I'm hoping that someone has come across this before. I have a C# Application that reads a UDP broadcast...
1
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.