public virtual bool SendData(bool aWriteFinal, bool aRes1, bool aRes2, bool aRes3, int aWriteCode, MemoryStream aStream)
{
bool result = !Closed && ((aWriteCode == WebSocketFrame.Close) || !fClosedByMe);
int bt = 0;
int sendLen = 0;
int i;
long len = 0;
Stream stream;
byte[] bytes;
byte[] masks = new byte[4];
byte[] send = new byte[65536];
Random rand = new Random();
if (result)
{
lock (writeLock)
{
try
{
stream = getStream(fClient);
//send basics
bt = (aWriteFinal ? 1 : 0) * 0x80;
bt += (aRes1 ? 1 : 0) * 0x40;
bt += (aRes2 ? 1 : 0) * 0x20;
bt += (aRes3 ? 1 : 0) * 0x10;
bt += aWriteCode;
stream.WriteByte((byte)bt);
//length & mask
len = (fMasking ? 1 : 0) * 0x80;
if (aStream.Length < 126) len += aStream.Length;
else if (aStream.Length < 65536) len += 126;
else len += 127;
stream.WriteByte((byte)len);
if (aStream.Length >= 126)
{
if (aStream.Length < 65536)
{
bytes = System.BitConverter.GetBytes((ushort)aStream.Length);
}
else
{
bytes = System.BitConverter.GetBytes((ulong)aStream.Length);
}
if (BitConverter.IsLittleEndian) bytes = ReverseBytes(bytes);
stream.Write(bytes, 0, bytes.Length);
}
//masking
if (fMasking)
{
masks[0] = (byte)rand.Next(256);
masks[1] = (byte)rand.Next(256);
masks[2] = (byte)rand.Next(256);
masks[3] = (byte)rand.Next(256);
stream.Write(masks, 0, masks.Length);
}
//send data
aStream.Position = 0;
while ((sendLen = aStream.Read(send, 0, send.Length)) > 0)
{
if (fMasking)
{
for (i = 0; i < send.Length; i++)
{
send[i] = (byte)(send[i] ^ masks[i % 4]);
}
}
stream.Write(send, 0, sendLen);
}
aStream.Position = 0;
if (ConnectionWrite != null)
ConnectionWrite(this, aWriteFinal, aRes1, aRes2, aRes3, aWriteCode, aStream);
}
catch
{
result = false;
}
}
}
return result;
}