public process ( byte datas, |
||
datas | byte | |
offset | ||
length | ||
Résultat | void |
public void recv() { if (socket_ == null || socket_.Connected == false) { throw new ArgumentException("invalid socket!"); } if (socket_.Poll(1000, SelectMode.SelectRead)) { if (socket_ == null || socket_.Connected == false) { Dbg.WARNING_MSG("invalid socket!"); return; } int successReceiveBytes = 0; try { successReceiveBytes = socket_.Receive(_datas, MemoryStream.BUFFER_MAX, 0); } catch (SocketException err) { if (err.ErrorCode == 10054 || err.ErrorCode == 10053) { Dbg.DEBUG_MSG(string.Format("NetworkInterface::recv(): disable connect!")); if (socket_ != null && socket_.Connected) { socket_.Close(); } socket_ = null; Event.fire("onDisableConnect", new object[] {}); } else { Dbg.ERROR_MSG(string.Format("NetworkInterface::recv(): socket error(" + err.ErrorCode + ")!")); } } if (successReceiveBytes > 0) { // Dbg.DEBUG_MSG(string.Format("NetworkInterface::recv(): size={0}!", successReceiveBytes)); } else if (successReceiveBytes == 0) { Dbg.DEBUG_MSG(string.Format("NetworkInterface::recv(): disable connect!")); if (socket_ != null && socket_.Connected) { socket_.Close(); } socket_ = null; } else { Dbg.ERROR_MSG(string.Format("NetworkInterface::recv(): socket error!")); } msgReader.process(_datas, (MessageLength)successReceiveBytes); } }
public void process() { int t_wpos = Interlocked.Add(ref _wpos, 0); if (_rpos < t_wpos) { messageReader.process(_buffer, (UInt32)_rpos, (UInt32)(t_wpos - _rpos)); Interlocked.Exchange(ref _rpos, t_wpos); } else if (t_wpos < _rpos) { messageReader.process(_buffer, (UInt32)_rpos, (UInt32)(_buffer.Length - _rpos)); messageReader.process(_buffer, (UInt32)0, (UInt32)t_wpos); Interlocked.Exchange(ref _rpos, t_wpos); } else { // 没有可读数据 } }
private void OnReceive2(int bytes) { //Debug.LogError("curThread: "+Thread.CurrentThread.ManagedThreadId); //var st = Util.GetTimeNow(); if (bytes <= 0 || client == null || !client.Connected) { Close(); return; } uint num = (uint)bytes; msgReader.process(mTemp, num, flowHandler); }
public void process() { int t_wpos = Interlocked.Add(ref _wpos, 0); if (_rpos < t_wpos) { if (_networkInterface.fileter() != null) { _networkInterface.fileter().recv(messageReader, _buffer, (UInt32)_rpos, (UInt32)(t_wpos - _rpos)); } else { messageReader.process(_buffer, (UInt32)_rpos, (UInt32)(t_wpos - _rpos)); } Interlocked.Exchange(ref _rpos, t_wpos); } else if (t_wpos < _rpos) { if (_networkInterface.fileter() != null) { _networkInterface.fileter().recv(messageReader, _buffer, (UInt32)_rpos, (UInt32)(_buffer.Length - _rpos)); _networkInterface.fileter().recv(messageReader, _buffer, (UInt32)0, (UInt32)t_wpos); } else { messageReader.process(_buffer, (UInt32)_rpos, (UInt32)(_buffer.Length - _rpos)); messageReader.process(_buffer, (UInt32)0, (UInt32)t_wpos); } Interlocked.Exchange(ref _rpos, t_wpos); } else { // 没有可读数据 } }
public override bool recv(MessageReader reader, byte[] buffer, UInt32 rpos, UInt32 len) { if (!_blowfish.isGood()) { Dbg.ERROR_MSG("BlowfishFilter::recv: Dropping packet, due to invalid filter"); return(false); } if (_packet.length() == 0 && len >= MIN_PACKET_SIZE && BitConverter.ToUInt16(buffer, (int)rpos) - 1 == len - 3) { int packLen = BitConverter.ToUInt16(buffer, (int)rpos) - 1; int padSize = buffer[rpos + 2]; decrypt(buffer, (int)(rpos + 3), (int)(len - 3)); int length = packLen - padSize; if (reader != null) { reader.process(buffer, rpos + 3, (UInt32)length); } return(true); } _packet.append(buffer, rpos, len); while (_packet.length() > 0) { UInt32 currLen = 0; int oldwpos = 0; if (_packLen <= 0) { // 如果满足一个最小包则尝试解包, 否则缓存这个包待与下一个包合并然后解包 if (_packet.length() >= MIN_PACKET_SIZE) { _packLen = _packet.readUint16(); _padSize = _packet.readUint8(); _packLen -= 1; if (_packet.length() > _packLen) { currLen = (UInt32)(_packet.rpos + _packLen); oldwpos = _packet.wpos; _packet.wpos = (int)currLen; } else if (_packet.length() < _packLen) { return(false); } } else { return(false); } } else { // 如果上一次有做过解包行为但包还没有完整则继续处理 // 如果包是完整的下面流程会解密, 如果有多余的内容需要将其剪裁出来待与下一个包合并 if (_packet.length() > _packLen) { currLen = (UInt32)(_packet.rpos + _packLen); oldwpos = _packet.wpos; _packet.wpos = (int)currLen; } else if (_packet.length() < _packLen) { return(false); } } decrypt(_packet); _packet.wpos -= _padSize; // 上面的流程能保证wpos之后不会有多余的包 // 如果有多余的包数据会放在_recvStream if (reader != null) { reader.process(_packet.data(), (UInt32)_packet.rpos, _packet.length()); } if (currLen > 0) { _packet.rpos = (int)currLen; _packet.wpos = oldwpos; } else { _packet.clear(); } _packLen = 0; _padSize = 0; } return(true); }