public void Write(double value) { if (_autoResize) { ResizeIfNeed(_position + 8); } FastBitConverter.GetBytes(_data, _position, value); _position += 8; }
public void Write(float value) { if (_autoResize) { ResizeIfNeed(_position + 4); } FastBitConverter.GetBytes(_data, _position, value); _position += 4; }
public void Write(ushort value) { if (_autoResize) { ResizeIfNeed(_position + 2); } FastBitConverter.GetBytes(_data, _position, value); _position += 2; }
public void Write(byte[] data) { if (_autoResize) { ResizeIfNeed(_position + data.Length + 4); } FastBitConverter.GetBytes(_data, _position, data.Length); Buffer.BlockCopy(data, 0, _data, _position + 4, data.Length); _position += data.Length + 4; }
public void Write(byte[] data, int offset, int length) { if (_autoResize) { ResizeIfNeed(_position + length + 4); } FastBitConverter.GetBytes(_data, _position, length); Buffer.BlockCopy(data, offset, _data, _position + 4, length); _position += length + 4; }
private void Write(Array arr, int sz) { ushort length = arr == null ? (ushort)0 : (ushort)arr.Length; sz *= length; if (_autoResize) { ResizeIfNeed(_position + sz + 2); } FastBitConverter.GetBytes(_data, _position, length); if (arr != null) { Buffer.BlockCopy(arr, 0, _data, _position + 2, sz); } _position += sz + 2; }
internal async Task RunAsync() { // 첫 접속 패킷을 보내자 NetPacket sendPacket = NetPool.PacketPool.Alloc(PacketProperty.ResponseConnection, 8); sendPacket.SessionIdForConnection = SessionId; FastBitConverter.GetBytes(sendPacket.RawData, 5, _connectId); SendRawAsync(sendPacket, DeliveryMethod.Tcp); _isPossibleUpdate = true; try { await ProcessPacketAsync(); } finally { _isPossibleUpdate = false; Close(); } }
internal void OnReceivedRawUdpData(byte[] data, int size, NetPacket cachedPacket, SocketError error, IPEndPoint endPoint) { //Console.WriteLine($"buffer size : {size} packet size : {BitConverter.ToUInt16(data, 0)}"); if (size < NetPacket.HeaderSize) return; try { PacketProperty property = cachedPacket.Property; DeliveryMethod deliveryMethod = cachedPacket.DeliveryMethod; switch (property) { case PacketProperty.MtuCheck: { NetPacket packet = NetPool.PacketPool.Alloc(NetPacket.GetHeaderSize(PacketProperty.MtuOk)); try { byte getMtuId = data[3]; packet.Property = PacketProperty.MtuOk; packet.DeliveryMethod = DeliveryMethod.Unreliable; packet.RawData[3] = getMtuId; SendTo(packet.RawData, 0, packet.Size, SendMode.Immediately); } catch (Exception ex) { _logger.LogError(ex, "Exception happened in MtuCheck"); } finally { NetPool.PacketPool.Free(packet); } } break; case PacketProperty.MtuOk: { try { byte getMtuId = data[3]; if (getMtuId >= _mtuId) { _mtuId = getMtuId; _mtu = NetPacket.PossibleMtu[getMtuId]; if (_mtuId >= NetPacket.PossibleMtu.Length) _finishMtu = true; //Console.WriteLine($"SetMtu : {_mtu}"); } } catch (Exception ex) { _logger.LogError(ex, "Exception happened in MtuOk"); } } break; case PacketProperty.Ping: { ushort sequence = BitConverter.ToUInt16(data, 3); if (NetUtil.RelativeSequenceNumber(sequence, _pongPacket.Sequence) > 0) { FastBitConverter.GetBytes(_pongPacket.RawData, 5, DateTime.UtcNow.Ticks); _pongPacket.Sequence = sequence; SendTo(_pongPacket.RawData, 0, _pongPacket.Size, SendMode.Immediately); } } break; case PacketProperty.Pong: { ushort sequence = BitConverter.ToUInt16(data, 3); if (sequence == _pingPacket.Sequence) { _pingTimer.Stop(); int elapsedMs = (int)_pingTimer.ElapsedMilliseconds; _remoteTickDelta = BitConverter.ToInt64(data, 5) + (elapsedMs * TimeSpan.TicksPerMillisecond) / 2 - DateTime.UtcNow.Ticks; UpdateRoundTripTime(elapsedMs); //Console.WriteLine($"Pong sequence : {sequence} {elapsedMs} ms {_remoteTickDelta} microseconds"); } } break; default: { ReadPacket(data, size, _channelOption.PacketFilter); } break; } } catch (Exception e) { _logger.LogError(e, "Exception happened in OnReceivedRawUdpData"); Close(); } }
public static void SetP2pSessionId(byte[] data, ushort relaySessionId) { FastBitConverter.GetBytes(data, 5, relaySessionId); }