Reputation: 4590
Is there a way to avoid creating new byte array instances with fixed lengths and calling Array.Copy
from the larger byte array Buffer
to the newly created reply array called reply
?
Or any other ideas?
// Buffer : byte[]
// host : a WCF host
while (code >= 0)
{
try
{
if (Socket.Available == 0)
{
Thread.Sleep(Wait);
continue;
}
var length = Socket.Receive(Buffer);
if (length > 0)
{
Log("Read", ServerPort, " ->", Client, length);
var reply = new byte[length];
Array.Copy(Buffer, reply, length);
try { code = host.Reply(ServerPort, Client, reply); }
catch (Exception ex)
{
code = -2;
Log("Exception", ServerPort, "<=>", Client, ex);
ConnectToHost();
}
}
}
catch (Exception ex)
{
code = -3;
Delist();
if (ex.GetType() != typeof(ThreadAbortException))
Log("Exception", ServerPort, "!!!", Client, ex);
Log("Disconnect", ServerPort, @"<\>", Client, Relay.Host.Address());
Tcp.Close();
}
Upvotes: 0
Views: 371
Reputation: 13849
You can use ArraySegment<T> as a way to pass around pieces of a larger byte array. I think the API is kinda crappy, but it certainly does the trick.
However, seems like you'd probably also benefit from using the BufferManager class provided for WCF for managing reusable buffers in your code.
Upvotes: 1