public BMSByte Receive(ref IPEndPoint remoteEP, ref string endpoint) { CheckDisposed(); recBuffer.Clear(); if (endPoint == null) { endPoint = new IPEndPoint(IPAddress.Any, 0); } int dataRead = socket.ReceiveFrom(recBuffer.byteArr, ref endPoint); if (!connections.ContainsKey(endPoint)) { connections.Add(endPoint, (((IPEndPoint)endPoint).Address.ToString() + HOST_PORT_CHARACTER_SEPARATOR + ((IPEndPoint)endPoint).Port.ToString())); BeardedManStudios.Forge.Logging.BMSLog.Log("endpoint added to connections: " + ((IPEndPoint)endPoint).Address.ToString()); } endpoint = connections[endPoint]; //if (dataRead < recBuffer.Size) // recBuffer = CutArray(recBuffer, dataRead); recBuffer.SetSize(dataRead); remoteEP = (IPEndPoint)endPoint; return(recBuffer); }
/*public CachedFacepunchP2PClient() * { * // Listen for clients wishing to start P2PConnections * SteamNetworking.OnP2PSessionRequest += OnP2PSessionRequest; * }*/ public CachedFacepunchP2PClient(SteamId endPoint, bool hosting = false) { isHost = hosting; // Listen for clients wishing to start P2PConnections if (isHost) { SteamNetworking.OnP2PSessionRequest += OnP2PSessionRequest; } SteamNetworking.OnP2PConnectionFailed += OnP2PConnectionFailed; steamEndPoint = endPoint; recBuffer.SetSize(65536); }
private byte PullPacketMetadata(BMSByte packet) { byte meta = packet.GetBasicType <byte>(packet.Size - sizeof(byte)); packet.SetSize(packet.Size - sizeof(byte)); return(meta); }
public BMSByte Receive(ref IPEndPoint remoteEP, ref string endpoint) { CheckDisposed(); recBuffer.Clear(); if (endPoint == null) { endPoint = new IPEndPoint(IPAddress.Any, 0); } // 将数据报接收到数据缓冲区并存储终结点。 // buffer Byte 类型的数组,它是存储接收到的数据的位置。 // remoteEP 按引用传递的 EndPoint,表示远程服务器。 // 返回值 接收到的字节数。 int dataRead = socket.ReceiveFrom(recBuffer.byteArr, ref endPoint); if (!connections.ContainsKey(endPoint)) { connections.Add(endPoint, (((IPEndPoint)endPoint).Address.ToString() + HOST_PORT_CHARACTER_SEPARATOR + ((IPEndPoint)endPoint).Port.ToString())); } endpoint = connections[endPoint]; //if (dataRead < recBuffer.Size) // recBuffer = CutArray(recBuffer, dataRead); recBuffer.SetSize(dataRead); remoteEP = (IPEndPoint)endPoint; return(recBuffer); }
private int PullPacketGroupId(BMSByte packet) { // This assumes that packet order id was pulled first int groupId = packet.GetBasicType <int>(packet.Size - sizeof(int)); packet.SetSize(packet.Size - sizeof(int)); return(groupId); }
private int PullPacketOrderId(BMSByte packet) { // This assumes that packet metadata was pulled first int orderId = packet.GetBasicType <int>(packet.Size - sizeof(int)); packet.SetSize(packet.Size - sizeof(int)); return(orderId); }
private ulong PullPacketUniqueId(BMSByte packet, bool endPacket) { // This assumes that packet group id was pulled first ulong uniqueId = packet.GetBasicType <ulong>(packet.Size - sizeof(ulong)); // Don't set the size like in the others unless it is the end packet // because the frame will consume this time step The frame expects // the last bytes in the message to be the time step if (!endPacket) { packet.SetSize(packet.Size - sizeof(ulong)); } return(uniqueId); }
/// <summary> /// Initialize the socket /// </summary> /// <param name="ep"></param> protected virtual void InitSocket(EndPoint ep) { if (socket != null) { socket.Close(); socket = null; } socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.Blocking = false; socket.Bind(ep); buffer.SetSize(MAX_PACKET_SIZE); }
public BMSByte Receive(uint msgSize, out CSteamID from) { uint dataRead = 0; CheckDisposed(); recBuffer.Clear(); recBuffer.SetSize((int)msgSize); if(SteamNetworking.ReadP2PPacket(recBuffer.byteArr, msgSize, out dataRead, out from)) { return recBuffer; } return null; }
public BMSByte Receive(ref IPEndPoint remoteEP) { CheckDisposed(); recBuffer.Clear(); if (endPoint == null) { endPoint = new IPEndPoint(IPAddress.Any, 0); } int dataRead = socket.ReceiveFrom(recBuffer.byteArr, ref endPoint); if (!connections.ContainsKey(endPoint)) { connections.Add(endPoint, (((IPEndPoint)endPoint).Address.ToString() + HOST_PORT_CHARACTER_SEPARATOR + ((IPEndPoint)endPoint).Port.ToString())); } recBuffer.SetSize(dataRead); remoteEP = (IPEndPoint)endPoint; return(recBuffer); }
public BMSByte Receive(ref IPEndPoint remoteEP, ref string endpoint) { try { CheckDisposed(); recBuffer.Clear(); if (endPoint == null) { endPoint = new IPEndPoint(IPAddress.Any, 0); } int dataRead = socket.ReceiveFrom(recBuffer.byteArr, ref endPoint); if (!connections.ContainsKey(endPoint)) { connections.Add(endPoint, (((IPEndPoint)endPoint).Address.ToString() + HOST_PORT_CHARACTER_SEPARATOR + ((IPEndPoint)endPoint).Port.ToString())); } endpoint = connections[endPoint]; //if (dataRead < recBuffer.Size) // recBuffer = CutArray(recBuffer, dataRead); recBuffer.SetSize(dataRead); remoteEP = (IPEndPoint)endPoint; return(recBuffer); } catch (SocketException) { // It would not be uncommon for a socket-exception to occur in the call to socket.ReceiveFrom. // This should not necessarily lead to concerns. //throw; return(recBuffer); } }
public CachedFacepunchP2PClient(SteamId endPoint) { steamEndPoint = endPoint; recBuffer.SetSize(65536); }
/// <summary> /// Creates the frame data using the passed in payload /// </summary> private void CreateFrame(bool useMask, ulong timestep, byte[] payload, Receivers receivers, int groupId, byte routerId, bool isStream) { // If we are to use a mask then generate a random mask if (useMask) { mask = new byte[4]; new Random().NextBytes(mask); } StreamData = new BMSByte(); TimeStep = timestep; GroupId = groupId; RouterId = routerId; Receivers = receivers; UniqueId = UniqueMessageIdCounter++; // Generate the frame identity byte[] frame = new byte[10]; // The first byte of the data is always the control byte, which dictates the message type frame[0] = ControlByte; int length = payload.Length; if (isStream) { length += 21; // Group id (4), receivers (1), time step (8), unique id (8) } else { length += 16; // time step (8), unique id (8) } if (frame[0] == Binary.CONTROL_BYTE) { length += 1; } // Determine the length of the payload int dataStartIndex = 0; if (length <= 125) { frame[1] = (byte)(useMask ? length | 128 : length); dataStartIndex = 2; } else if (length >= 126 && length <= 65535) { dataStartIndex = 4; frame[1] = (byte)(useMask ? 254 : 126); } else { dataStartIndex = 10; frame[1] = (byte)(useMask ? 255 : 127); } // If the payload is greater than a byte (255) then set the order of the bytes for the length if (dataStartIndex > 2) { int i = 0, j = 2, largestBitIndex = (dataStartIndex - 3) * 8; // Little endian / Big endian reversal based on mask //if (mask.Length == 0) //{ for (i = largestBitIndex; i >= 0; i -= 8) { frame[j++] = (byte)((((long)length) >> i) & 255); } //} else //{ // for (i = 0; i <= largestBitIndex; i += 8) // frame[j++] = (byte)((payload.Length >> i) & 255); //} } // Prepare the stream data with the size so that it doesn't have to keep resizing StreamData.SetSize(dataStartIndex + mask.Length + length); StreamData.Clear(); // Add the frame bytes StreamData.BlockCopy(frame, 0, dataStartIndex); // Add the mask bytes StreamData.BlockCopy(mask, 0, mask.Length); // Setup the int that tracks where the payload begins payloadStart = dataStartIndex + mask.Length; // If we are on a stream then use groupId if (isStream) { StreamData.BlockCopy(BitConverter.GetBytes(groupId), 0, sizeof(int)); payloadStart += sizeof(int); } // Copy the routerId if this is a binary frame if (frame[0] == Binary.CONTROL_BYTE) { StreamData.BlockCopy(new byte[1] { routerId }, 0, sizeof(byte)); payloadStart += 1; } // Add the initial payload bytes StreamData.BlockCopy(payload, 0, payload.Length); if (isStream) { StreamData.BlockCopy(new byte[1] { (byte)Receivers }, 0, sizeof(byte)); } // Add the time step to the end of the frame StreamData.BlockCopy <ulong>(TimeStep, sizeof(ulong)); // Add the unique message id for this frame just before the timestep frame StreamData.BlockCopy <ulong>(UniqueId, sizeof(ulong)); if (mask.Length > 0) { for (int i = dataStartIndex + mask.Length, j = 0; i < StreamData.Size; i++, j++) { StreamData.byteArr[i] = (byte)(StreamData.byteArr[i] ^ mask[j % 4]); } } }