void OnSend(IAsyncResult result) { int num = 0; try { num = mSocket.EndSend(result); } catch (Exception exception) { num = 0; Close(); Debug.LogError(exception.ToString()); return; } lock (msgBuffer) { if (mSocket != null && mSocket.Connected) { var mb = msgBuffer [0]; MsgBuffer nextBuffer = null; if (mb.Size <= num) { msgBuffer.RemoveAt(0); if (msgBuffer.Count > 0) { nextBuffer = msgBuffer [0]; } } else if (mb.Size > num) { mb.position += num; nextBuffer = mb; } if (nextBuffer != null) { try { mSocket.BeginSend(nextBuffer.buffer, nextBuffer.position, nextBuffer.Size, SocketFlags.None, OnSend, null); } catch (Exception exception) { Debug.LogError(exception.ToString()); Close(); } } } } }
public void Send(byte[] data) { lock (msgBuffer) { var mb = new MsgBuffer() { position = 0, buffer = data }; msgBuffer.Add(mb); if (msgBuffer.Count == 1) { try { var asyncRet = mSocket.BeginSend(mb.buffer, mb.position, mb.Size, SocketFlags.None, OnSend, null); ThreadPool.QueueUserWorkItem(SendTimeOut, asyncRet); } catch (Exception exception) { Debug.LogError(exception.ToString()); Close(); } } } }