public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo) { int peerID = _peerIDs[peer]; var writer = new NetBitPackedDataWriter(); { writer.WriteRangedInt(0, _packets.Count, 0); writer.WriteRangedInt(0, _initialPacketBits, (int)INITIAL_PACKET.PEER_QUIT); writer.WriteRangedInt(0, _maxPlayersIndex, peerID); _manager.SendToAll(writer.Data, DeliveryMethod.ReliableOrdered, peer); } OnPeerQuit?.Invoke(peerID); _peerIDs.Remove(peer); _peers1.Remove(peerID); _peers2.Remove(peerID); _expectedPacketsWithFiles.Remove(peer); }
public void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod) { var peerID = _peerIDs[peer]; var data = new NetBitPackedDataReader(reader); var packetID = data.ReadRangedInt(0, NetClient._packets.Count); if (packetID == 0) { Console.WriteLine($"Server received initial data from ID: {peerID}"); var packetID2 = data.ReadRangedInt(0, _initialDataPackets.Count); if (packetID2 == 0) { var writer = new NetBitPackedDataWriter(); { writer.WriteRangedInt(0, _packets.Count, 0); writer.WriteRangedInt(0, _initialPacketBits, (int)INITIAL_PACKET.PEER_JOIN); writer.WriteRangedInt(0, _maxPlayersIndex, peerID); } if (IsListen) { NetClient.InvokeOnPeerJoin(peerID); foreach (var kv in _peerIDs) { if (kv.Value != NetClient._selfID && kv.Value != peerID) { kv.Key.Send(writer.Data, DeliveryMethod.ReliableOrdered); } } } else { OnPeerJoin?.Invoke(peerID); _manager.SendToAll(writer.Data, DeliveryMethod.ReliableOrdered, peer); } _peers1.Add(peerID, peer); _peers2.Add(peerID, peer); _expectedPacketsWithFiles.Add(peer, new Dictionary <Type, ISet <int> >()); } else { var(_, _, reads, _) = NetClient._packetsWithFilesInfo[NetClient._initialDataPackets[packetID2]]; var i = data.ReadInt(); var packet = InitialData[i]; for (var j = 0; j < reads.Length; j++) { reads[j](data, packet); } var writer = new NetBitPackedDataWriter(); { writer.WriteRangedInt(0, _packets.Count, 0); writer.WriteRangedInt(0, _initialPacketBits, (int)INITIAL_PACKET.INITIAL_DATA); writer.WriteRangedInt(0, _initialDataIDBits, (int)INITIAL_DATA_ID.DATA); var(_, writes2, _, _) = _initialDataPacketInfo[packet.GetType()]; writer.WriteRangedInt(0, _initialDataPackets.Count, packetID2); writer.Write(i == InitialData.Count - 1); writer.Write(i); writer.Write(false); for (var j = 0; j < writes2.Length; j++) { writes2[j](writer, packet); } peer.Send(writer.Data, DeliveryMethod.ReliableOrdered); } } return; } //Console.WriteLine($"Server received data: Packet ID: {id}"); try { var t = NetClient._packets[packetID]; if (_expectedPacketsWithFiles[peer].ContainsKey(t)) { var(_, _, reads2, _) = NetClient._packetsWithFilesInfo[t]; var(_, writes2, _, _) = _packetInfo[t]; var i = data.ReadInt(); var packet = _netFilePackets[i]; for (var j = 0; j < reads2.Length; j++) { reads2[j](data, packet); } var writer = new NetBitPackedDataWriter(); { writer.WriteRangedInt(0, _packets.Count, packetID); writer.WriteRangedInt(0, _netFilePackets.Length - 1, i); writer.Write(false); for (var j = 0; j < writes2.Length; j++) { writes2[j](writer, packet); } peer.Send(writer.Data, DeliveryMethod.ReliableOrdered); } _netFilePackets[i] = null; _expectedPacketsWithFiles[peer][t].Remove(i); if (_expectedPacketsWithFiles[peer][t].Count == 0) { _expectedPacketsWithFiles[peer].Remove(t); } } else { var(_, _, reads, instance, shouldRelay) = NetClient._packetInfo[t]; for (var i = 0; i < reads.Length; i++) { reads[i](data, instance); } instance.SenderID = peerID; if (!IsListen || (peerID != NetClient._selfID)) { try { instance.Process(); } catch (Exception e) { throw e; } } if (_packets.ContainsKey(packetID) && shouldRelay) { SendToAll(instance, deliveryMethod, peer); } } } catch { Console.WriteLine($"Server failed to read data: Packet ID: {packetID}"); } }
public void OnPeerConnected(NetPeer peer) { if (IsListen && _peerIDs.Count == 0) { _peerIDs.Add(peer, NetClient._selfID = 0); Console.WriteLine($"Server initialized self with ID: {NetClient._selfID}"); NetClient.InvokeOnJoin(NetClient._selfID); _peers2.Add(NetClient._selfID, peer); return; } var peerID = -1; for (int i = 0; i < _maxPlayers; i++) { if (!_peerIDs.Values.Contains(i)) { peerID = i; break; } } _peerIDs.Add(peer, peerID); var writer = new NetBitPackedDataWriter(); { writer.WriteRangedInt(0, _packets.Count, 0); writer.WriteRangedInt(0, _initialPacketBits, (int)INITIAL_PACKET.INITIAL_DATA); writer.WriteRangedInt(0, _initialDataIDBits, (int)INITIAL_DATA_ID.DATA); writer.WriteRangedInt(0, _initialDataPackets.Count, 0); writer.Write(InitialData.Count == 0); writer.Write((byte)_maxPlayersIndex); writer.WriteRangedInt(0, _maxPlayersIndex, peerID); for (int j = 0; j < _maxPlayers; j++) { if (j != peerID) { writer.Write(_peerIDs.Values.Contains(j)); } } peer.Send(writer.Data, DeliveryMethod.ReliableOrdered); for (int i = 0, InitialDataCount = InitialData.Count; i < InitialDataCount; i++) { var packet = InitialData[i]; writer = new NetBitPackedDataWriter(); { writer.WriteRangedInt(0, _packets.Count, 0); writer.WriteRangedInt(0, _initialPacketBits, (int)INITIAL_PACKET.INITIAL_DATA); writer.WriteRangedInt(0, _initialDataIDBits, (int)INITIAL_DATA_ID.DATA); if (NetClient._packetsWithFilesInfo.ContainsKey(packet.GetType())) { var(packetID, writes, _, _) = NetClient._packetsWithFilesInfo[packet.GetType()]; writer.WriteRangedInt(0, _initialDataPackets.Count, packetID); writer.Write(i == InitialDataCount - 1); writer.Write(i); writer.Write(true); for (var j = 0; j < writes.Length; j++) { writes[j](writer, packet); } } else { var(packetID, writes, _, _) = _initialDataPacketInfo[packet.GetType()]; writer.WriteRangedInt(0, _initialDataPackets.Count, packetID); writer.Write(i == InitialDataCount - 1); for (var j = 0; j < writes.Length; j++) { writes[j](writer, packet); } } peer.Send(writer.Data, DeliveryMethod.ReliableOrdered); } } } }