public QClient(string name, int bufferSize = 100 * 1024, string ip = "127.0.0.1", int port = 39654) { _name = name; HeartSpan = 1000 * 1000; HeartAsync(); _batcher = new Batcher <byte[]>(3000, 10); //此参数用于控制产生或消费速读,过大会导致溢出异常 _batcher.OnBatched += _batcher_OnBatched; _qUnpacker = new QUnpacker(); _queueCoder = new QueueCoder(); ISocketOption socketOption = SocketOptionBuilder.Instance.SetSocket(Sockets.Model.SAEASocketType.Tcp) .UseIocp <QUnpacker>() .SetIP(ip) .SetPort(port) .SetWriteBufferSize(bufferSize) .SetReadBufferSize(bufferSize) .ReusePort(false) .Build(); _clientSocket = SocketFactory.CreateClientSocket(socketOption); _clientSocket.OnReceive += _clientSocket_OnReceive; _clientSocket.OnError += _clientSocket_OnError; _clientSocket.OnDisconnected += _clientSocket_OnDisconnected; }
public void GetSubscribeData(string sessionID, QueueResult sInfo, QUnpacker qcoder) { if (!_binding.Exists(sInfo)) { _binding.Set(sessionID, sInfo.Name, sInfo.Topic, false); _cNum = _binding.GetSubscriberCount(); ThreadPool.QueueUserWorkItem(new WaitCallback((o) => { while (_binding.Exists(sInfo)) { var msg = _messageQueue.Dequeue(sInfo.Topic); if (!string.IsNullOrEmpty(msg)) { Interlocked.Increment(ref _outNum); _classificationBatcher.Insert(sessionID, qcoder.QueueCoder.Data(sInfo.Name, sInfo.Topic, msg)); } else { Thread.Yield(); } } })); } }
/// <summary> /// 按指定格式编码 /// </summary> /// <param name="cmdType"></param> /// <param name="contents"></param> /// <returns></returns> public byte[] Encode(QueueSocketMsgType cmdType, string name, string topic, string data) { return(QUnpacker.Encode(new QueueSocketMsg(cmdType, name, topic, data))); }