Пример #1
0
 /// <summary>
 /// Constructs a generalized connector
 /// </summary>
 /// <param name="onHandlePeerConnectionAttempt">Handles peer connection attempts</param>
 public AConnector(HandlePeerConnectionAttemptDelegate onHandlePeerConnectionAttempt) => this.onHandlePeerConnectionAttempt = onHandlePeerConnectionAttempt ?? throw new ArgumentNullException(nameof(onHandlePeerConnectionAttempt));
Пример #2
0
 /// <summary>
 /// Constructs a local connector
 /// </summary>
 /// <param name="onHandlePeerConnectionAttempt">Handles peer connection attempts</param>
 public LocalConnector(HandlePeerConnectionAttemptDelegate onHandlePeerConnectionAttempt) : base(onHandlePeerConnectionAttempt)
 {
     // ...
 }
Пример #3
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="Host">Host</param>
        /// <param name="port">Port</param>
        /// <param name="timeoutTime">Timeout time in seconds</param>
        /// <param name="onHandlePeerConnectionAttempt">Handles peer connection attempts</param>
        public ENetConnector(Host host, ushort port, uint timeoutTime, HandlePeerConnectionAttemptDelegate onHandlePeerConnectionAttempt) : base(onHandlePeerConnectionAttempt)
        {
            Host            = host ?? throw new ArgumentNullException(nameof(host));
            Port            = port;
            TimeoutTime     = timeoutTime;
            connectorThread = new Thread(() =>
            {
                HashSet <uint> available_peer_ids = new HashSet <uint>();
                while (isConnectorThreadRunning)
                {
                    bool has_network_event = true;
                    if (Host.CheckEvents(out Event network_event) <= 0)
                    {
                        if (Host.Service((int)TimeoutTime, out network_event) <= 0)
                        {
                            has_network_event = false;
                        }
                    }
                    if (has_network_event)
                    {
                        switch (network_event.Type)
                        {
                        case EventType.Connect:
                            available_peer_ids.Add(network_event.Peer.ID);
                            peerConnectionAttemptMessages.Enqueue(new ENetPeerConnectionAttemptMessage(network_event.Peer));
                            break;

                        case EventType.Disconnect:
                            available_peer_ids.Remove(network_event.Peer.ID);
                            peerDisconnectionMessages.Enqueue(new ENetPeerDisconnectionMessage(network_event.Peer));
                            break;

                        case EventType.Receive:
                            Packet packet = network_event.Packet;
                            if (buffer.Length < packet.Length)
                            {
                                buffer = new byte[packet.Length / buffer.Length * (((packet.Length % buffer.Length) == 0) ? 1 : 2) * buffer.Length];
                            }
                            Marshal.Copy(packet.Data, buffer, 0, packet.Length);
                            peerReceiveMessages.Enqueue(new ENetPeerReceiveMessage(network_event.Peer, network_event.ChannelID, Compression.Decompress(buffer, 0U, (uint)packet.Length)));
                            break;

                        case EventType.Timeout:
                            available_peer_ids.Remove(network_event.Peer.ID);
                            peerTimeOutMessages.Enqueue(new ENetPeerTimeOutMessage(network_event.Peer));
                            break;
                        }
                    }
                    while (peerSendMessages.TryDequeue(out ENetPeerSendMessage send_message))
                    {
                        if (available_peer_ids.Contains(send_message.Peer.ID))
                        {
                            Packet packet = default;
                            packet.Create(send_message.Message, (int)send_message.Index, (int)send_message.Length, PacketFlags.Reliable);
                            send_message.Peer.Send(0, ref packet);
                            disposePackets.Add(packet);
                        }
                    }
                    if (disposePackets.Count > 0)
                    {
                        Host.Flush();
                        foreach (Packet packet in disposePackets)
                        {
                            packet.Dispose();
                        }
                        disposePackets.Clear();
                    }
                }
                available_peer_ids.Clear();
            });
            connectorThread.Start();
        }