本文整理汇总了C#中ITcpConnection.EnqueueSend方法的典型用法代码示例。如果您正苦于以下问题:C# ITcpConnection.EnqueueSend方法的具体用法?C# ITcpConnection.EnqueueSend怎么用?C# ITcpConnection.EnqueueSend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ITcpConnection
的用法示例。
在下文中一共展示了ITcpConnection.EnqueueSend方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendRandomData
private void SendRandomData(ITcpConnection tcpConnection, bool small)
{
var rnd = new Random ();
WaitCallback callBack = state =>
{
lock (tcpConnection) // to make sure ordered data generation -- even if we internally lock in enqueue
// the better test would be to generate consistent packages and verify them without this lock, but
// due to internal lock this lock just reduces probability of our problems not eliminating them
{
var outData = new List<ArraySegment<byte>>();
var sent = _totalSent[tcpConnection];
var index = (int) (sent%_data.Length);
int size = (4 + ((!small && rnd.Next(2) == 0) ? _data.Length/2 + rnd.Next(_data.Length/2) : rnd.Next(32)))/4
*4;
if (index + size <= _data.Length)
{
outData.Add(new ArraySegment<byte>(_data, index, size));
}
else
{
outData.Add(new ArraySegment<byte>(_data, index, _data.Length - index));
outData.Add(new ArraySegment<byte>(_data, 0, size - _data.Length + index));
}
_totalSent[tcpConnection] += size;
Interlocked.Add(ref _sent, size);
tcpConnection.EnqueueSend(outData);
}
};
if (rnd.Next (3) != 0) {
small = true;
var loopCount = rnd.Next(100);
var nextRnd = rnd.Next (4);
for (var k = 0; k < 10; k++)
ThreadPool.QueueUserWorkItem (v =>
{
long h = 1;
// random cpu busy delay
for (long i = 0; i < loopCount; i++)
h = h + i;
// randon cpu free delay
callBack (null);
if (!small && nextRnd == 0) {
//Thread.Sleep (rnd.Next (5));
ThreadPool.QueueUserWorkItem (callBack);
}
}
);
}
else
{
callBack (null);
// callBack (null);
// callBack (null);
}
}
示例2: SendRandomData
private void SendRandomData(ITcpConnection tcpConnection, bool small)
{
var rnd = new Random ();
WaitCallback callBack = state =>
{
var outData = new List<ArraySegment<byte>> ();
int index = 0;
int size = 1 + ((!small && rnd.Next (2) == 0) ? _data.Length/2 + rnd.Next (_data.Length/2) : rnd.Next (32));
int totalSize = 0;
outData.Add (new ArraySegment<byte> (_data, index, size));
totalSize += size;
Interlocked.Add (ref _sent, size);
_totalSent [tcpConnection] += totalSize;
tcpConnection.EnqueueSend (outData);
};
if (rnd.Next (5) != 0) {
small = true;
var loopCount = rnd.Next(10000);
var nextRnd = rnd.Next (4);
for (var k = 0; k < 10; k++)
ThreadPool.QueueUserWorkItem (v =>
{
long h = 1;
// random cpu busy delay
for (long i = 0; i < loopCount; i++)
h = h + i;
// randon cpu free delay
callBack (null);
if (!small && nextRnd == 0) {
//Thread.Sleep (rnd.Next (5));
ThreadPool.QueueUserWorkItem (callBack);
}
}
);
}
else
{
callBack (null);
// callBack (null);
// callBack (null);
}
}
示例3: OnDataReceived
private void OnDataReceived(ITcpConnection conn, IEnumerable<ArraySegment<byte>> data)
{
conn.EnqueueSend(data);
conn.ReceiveAsync(OnDataReceived);
}