BeginSend()
とを使用して多くのクライアントを管理するサーバー側アプリケーションを作成しましたEndSend()
BeginSend()
過去数時間、同じクライアントに対して結果の呼び出しが呼び出されないように苦労しています。
次のコード スニペットは私の目標を達成していないようでSendAsyncData
、最初の呼び出しが完了する前に 2 番目の呼び出しをブロックしません。
私の間違いは何ですか?
struct SendBuffer
{
public const int BUFFER_SIZE = 1024 * 128;
public byte[] BUFFER;
public int sent;
public SendBuffer(byte[] data)
{
BUFFER = new byte[data.Length];
Buffer.BlockCopy(data, 0, BUFFER, 0, data.Length);
sent = 0;
}
public void Dispose()
{
BUFFER = null;
sent = 0;
}
}
private void SendAsyncDataWithHeader(byte[] data
{
int toSend;
byte[] dataWithHeader = Combine(BitConverter.GetBytes(data.Length), data);
SendAutoResetEvent.WaitOne();
sendBuffer = new SendBuffer(dataWithHeader);
if (sendBuffer.BUFFER.Length - sendBuffer.sent > SendBuffer.BUFFER_SIZE)
toSend = SendBuffer.BUFFER_SIZE;
else
toSend = sendBuffer.BUFFER.Length - sendBuffer.sent;
try
{
socket.BeginSend(sendBuffer.BUFFER, 0, toSend, SocketFlags.None, sendCallback, null);
}
catch (SocketException se)
{
switch (se.SocketErrorCode)
{
case SocketError.ConnectionAborted:
case SocketError.ConnectionReset:
if (Disconnected != null)
{
Disconnected(this);
}
break;
}
}
catch (ObjectDisposedException)
{
}
catch (NullReferenceException ex)
{
}
catch (Exception ex)
{
Server.logger.Error(ex);
Disconnected(this);
}
}
void sendCallback(IAsyncResult ar)
{
int bytesSent;
try
{
bytesSent = socket.EndSend(ar);
if (bytesSent == 0)
{
if (Disconnected != null)
{
Disconnected(this);
return;
}
}
sendBuffer.sent += bytesSent;
if (sendBuffer.sent == sendBuffer.BUFFER.Length)
{
sendBuffer.Dispose();
SendAutoResetEvent.Set();
Server.logger.Info("set {0}", userName);
}
else
{
int toSend;
if (sendBuffer.BUFFER.Length - sendBuffer.sent > SendBuffer.BUFFER_SIZE)
toSend = SendBuffer.BUFFER_SIZE;
else
toSend = sendBuffer.BUFFER.Length - sendBuffer.sent;
socket.BeginSend(sendBuffer.BUFFER, sendBuffer.sent, toSend, SocketFlags.None, sendCallback, null);
}
}
catch (SocketException se)
{
switch (se.SocketErrorCode)
{
case SocketError.ConnectionAborted:
case SocketError.ConnectionReset:
if (Disconnected != null)
{
Disconnected(this);
return;
}
break;
}
}
catch (ObjectDisposedException ex)
{
}
catch (Exception ex)
{
Server.logger.Error(ex);
Disconnected(this);
}
}