Пример #1
0
 /// <summary>
 /// Send data to all connected peers
 /// </summary>
 /// <param name="data">Data</param>
 /// <param name="options">Send options (reliable, unreliable, etc.)</param>
 /// <param name="excludePeer">Excluded peer</param>
 public void SendToAll(byte[] data, SendOptions options, NetPeer excludePeer)
 {
     SendToAll(data, 0, data.Length, options, excludePeer);
 }
Пример #2
0
 /// <summary>
 /// Send data to all connected peers
 /// </summary>
 /// <param name="data">Data</param>
 /// <param name="start">Start of data</param>
 /// <param name="length">Length of data</param>
 /// <param name="options">Send options (reliable, unreliable, etc.)</param>
 /// <param name="excludePeer">Excluded peer</param>
 public void SendToAll(byte[] data, int start, int length, SendOptions options, NetPeer excludePeer)
 {
     lock (_peers)
     {
         for (int i = 0; i < _peers.Count; i++)
         {
             var netPeer = _peers[i];
             if (netPeer != excludePeer)
             {
                 netPeer.Send(data, start, length, options);
             }
         }
     }
 }
Пример #3
0
 /// <summary>
 /// Send data to all connected peers
 /// </summary>
 /// <param name="writer">DataWriter with data</param>
 /// <param name="options">Send options (reliable, unreliable, etc.)</param>
 /// <param name="excludePeer">Excluded peer</param>
 public void SendToAll(NetDataWriter writer, SendOptions options, NetPeer excludePeer)
 {
     SendToAll(writer.Data, 0, writer.Length, options, excludePeer);
 }
Пример #4
0
        private void DataReceived(byte[] reusableBuffer, int count, NetEndPoint remoteEndPoint)
        {
#if STATS_ENABLED
            PacketsReceived++;
            BytesReceived += (uint)count;
#endif

            //Try read packet
            NetPacket packet = _netPacketPool.GetAndRead(reusableBuffer, 0, count);
            if (packet == null)
            {
                NetUtils.DebugWriteError("[NM] DataReceived: bad!");
                return;
            }

            //Check unconnected
            switch (packet.Property)
            {
            case PacketProperty.DiscoveryRequest:
                if (DiscoveryEnabled)
                {
                    var netEvent = CreateEvent(NetEventType.DiscoveryRequest);
                    netEvent.RemoteEndPoint = remoteEndPoint;
                    netEvent.DataReader.SetSource(packet.RawData, NetConstants.HeaderSize);
                    EnqueueEvent(netEvent);
                }
                return;

            case PacketProperty.DiscoveryResponse:
            {
                var netEvent = CreateEvent(NetEventType.DiscoveryResponse);
                netEvent.RemoteEndPoint = remoteEndPoint;
                netEvent.DataReader.SetSource(packet.RawData, NetConstants.HeaderSize);
                EnqueueEvent(netEvent);
            }
                return;

            case PacketProperty.UnconnectedMessage:
                if (UnconnectedMessagesEnabled)
                {
                    var netEvent = CreateEvent(NetEventType.ReceiveUnconnected);
                    netEvent.RemoteEndPoint = remoteEndPoint;
                    netEvent.DataReader.SetSource(packet.RawData, NetConstants.HeaderSize);
                    EnqueueEvent(netEvent);
                }
                return;

            case PacketProperty.NatIntroduction:
            case PacketProperty.NatIntroductionRequest:
            case PacketProperty.NatPunchMessage:
            {
                if (NatPunchEnabled)
                {
                    NatPunchModule.ProcessMessage(remoteEndPoint, packet);
                }
                return;
            }
            }

            //Check normal packets
            NetPeer netPeer;

            //Check peers
            Monitor.Enter(_peers);
            int peersCount = _peers.Count;

            if (_peers.TryGetValue(remoteEndPoint, out netPeer))
            {
                Monitor.Exit(_peers);
                //Send
                if (packet.Property == PacketProperty.Disconnect)
                {
                    if (BitConverter.ToInt64(packet.RawData, 1) != netPeer.ConnectId)
                    {
                        //Old or incorrect disconnect
                        _netPacketPool.Recycle(packet);
                        return;
                    }

                    var netEvent = CreateEvent(NetEventType.Disconnect);
                    netEvent.Peer = netPeer;
                    netEvent.DataReader.SetSource(packet.RawData, 5, packet.Size - 5);
                    netEvent.DisconnectReason = DisconnectReason.RemoteConnectionClose;
                    EnqueueEvent(netEvent);

                    _peers.Remove(netPeer.EndPoint);
                    //do not recycle because no sense)
                }
                else if (packet.Property == PacketProperty.ConnectAccept)
                {
                    if (netPeer.ProcessConnectAccept(packet))
                    {
                        var connectEvent = CreateEvent(NetEventType.Connect);
                        connectEvent.Peer = netPeer;
                        EnqueueEvent(connectEvent);
                    }
                    _netPacketPool.Recycle(packet);
                }
                else
                {
                    netPeer.ProcessPacket(packet);
                }
                return;
            }

            try
            {
                if (peersCount < _maxConnections && packet.Property == PacketProperty.ConnectRequest)
                {
                    int protoId = BitConverter.ToInt32(packet.RawData, 1);
                    if (protoId != NetConstants.ProtocolId)
                    {
                        NetUtils.DebugWrite(ConsoleColor.Cyan,
                                            "[NM] Peer connect reject. Invalid protocol ID: " + protoId);
                        return;
                    }

                    string peerKey = Encoding.UTF8.GetString(packet.RawData, 13, packet.Size - 13);
                    if (peerKey != _connectKey)
                    {
                        NetUtils.DebugWrite(ConsoleColor.Cyan, "[NM] Peer connect reject. Invalid key: " + peerKey);
                        return;
                    }

                    //Getting new id for peer
                    long connectionId = BitConverter.ToInt64(packet.RawData, 5);
                    //response with id
                    netPeer = new NetPeer(this, remoteEndPoint, connectionId);
                    NetUtils.DebugWrite(ConsoleColor.Cyan, "[NM] Received peer connect request Id: {0}, EP: {1}",
                                        netPeer.ConnectId, remoteEndPoint);

                    //clean incoming packet
                    _netPacketPool.Recycle(packet);

                    _peers.Add(remoteEndPoint, netPeer);

                    var netEvent = CreateEvent(NetEventType.Connect);
                    netEvent.Peer = netPeer;
                    EnqueueEvent(netEvent);
                }
            }
            finally
            {
                Monitor.Exit(_peers);
            }
        }
Пример #5
0
 /// <summary>
 /// Disconnect peer from server and send additional data (Size must be less or equal MTU - 8)
 /// </summary>
 /// <param name="peer">peer to disconnect</param>
 /// <param name="writer">additional data</param>
 public void DisconnectPeer(NetPeer peer, NetDataWriter writer)
 {
     DisconnectPeer(peer, writer.Data, 0, writer.Length);
 }
Пример #6
0
 /// <summary>
 /// Disconnect peer from server and send additional data (Size must be less or equal MTU - 8)
 /// </summary>
 /// <param name="peer">peer to disconnect</param>
 /// <param name="data">additional data</param>
 public void DisconnectPeer(NetPeer peer, byte[] data)
 {
     DisconnectPeer(peer, data, 0, data.Length);
 }
Пример #7
0
 /// <summary>
 /// Disconnect peer from server
 /// </summary>
 /// <param name="peer">peer to disconnect</param>
 public void DisconnectPeer(NetPeer peer)
 {
     DisconnectPeer(peer, null, 0, 0);
 }