private async Task AcceptPeers() { while (true) { Socket socket; try { socket = await _listener.AcceptSocketAsync(); _logger.LogInformation($"\"{(socket.RemoteEndPoint as IPEndPoint)?.Address}\" is connected"); } catch (ObjectDisposedException) { break; } catch (SocketException) { continue; } var peer = _peerFactory.CreateFrom(socket); OnPeerConnected?.Invoke(this, peer); } }
void OnNewConnection(int connectionId) { var connection = AddConnection(connectionId); connection.ConnectTask.TrySetResult(new object()); OnPeerConnected?.Invoke(connection); }
/// <summary> /// Processes all events appeared since last call /// </summary> public override void ProcessEvents() { while (peerConnectionAttemptMessages.TryDequeue(out ENetPeerConnectionAttemptMessage connection_attempt_message)) { ENetPeer peer = new ENetPeer(Guid.NewGuid(), connection_attempt_message.Peer, this); OnPeerConnectionAttempted?.Invoke(peer); if (IsConnectionAllowed(peer)) { peers.Add(peer.GUID.ToString(), peer); peerIDToPeerLookup.Add(peer.Peer.ID, peer); OnPeerConnected?.Invoke(peer); } else { connection_attempt_message.Peer.Disconnect((uint)EDisconnectionReason.Banned); } } while (peerDisconnectionMessages.TryDequeue(out ENetPeerDisconnectionMessage disconnection_message)) { RemovePeer(disconnection_message.Peer); } while (peerTimeOutMessages.TryDequeue(out ENetPeerTimeOutMessage time_out_message)) { RemovePeer(time_out_message.Peer); } while (peerReceiveMessages.TryDequeue(out ENetPeerReceiveMessage receive_message)) { if (peerIDToPeerLookup.ContainsKey(receive_message.Peer.ID)) { OnPeerMessageReceived?.Invoke(peerIDToPeerLookup[receive_message.Peer.ID], receive_message.Message); } } }
protected virtual NetworkConnection OnNewConnection(int connectionId) { var connection = AddConnection(connectionId); if (connection.ConnectInternal()) { OnPeerConnected?.Invoke(connection); } return(connection); }
private void AddPeer(EndPoint address, Tracker tracker = null, bool withTracker = false) { if (!(Peers.Any(peer => peer.Address.Equals(address))) && !(address.Equals(CommonHelpers.GetLocalEndPoint(CommonHelpers.PeerPort, true)))) { var peer = new Peer(address, Peers, tracker); Peers.Add(peer); //перенаправляем события пира peer.OnRequestBlocksMessage += (s, e) => { OnRequestBlocksMessage?.Invoke(s, e); }; peer.OnRequestTransactionsMessage += (s, e) => { OnRequestTransactionsMessage?.Invoke(s, e); }; peer.OnBlocksMessage += (s, e) => { OnBlocksMessage?.Invoke(s, e); }; peer.OnTransactionsMessage += (s, e) => { OnTransactionsMessage?.Invoke(s, e); }; peer.OnPeersMessage += (s, e) => OnPeersMessage(s, e); peer.Connect(withTracker); //вызов события подключения пира Task.Run(() => { System.Threading.Thread.Sleep(CommonHelpers.MessagesInterval * 5); OnPeerConnected?.Invoke(this, new MessageEventArgs(new Message(), peer.Hash, peer.Address)); }); } }
public Server(string key, int port, int pollDifference, int maxClient, int disconnectTimeOut) { handlers = new Dictionary <string, OnEventRecieved>(); dataWriter = new NetDataWriter(); listener = new EventBasedNetListener(); server = new NetManager(listener, maxClient, key); server.Start(port); server.DisconnectTimeout = disconnectTimeOut; this.pollDifference = pollDifference; listener.PeerConnectedEvent += peer => { Console.WriteLine("peer connectd ===>", peer.EndPoint); onPeerConnected?.Invoke(peer); }; listener.NetworkReceiveEvent += (peer, dataReader) => { string x = dataReader.GetString(); Console.WriteLine(x); JsonData jsonData = JsonMapper.ToObject(x); if (handlers.ContainsKey(jsonData[0].ToString())) { if (jsonData.Count > 1) { handlers[jsonData[0].ToString()](peer, jsonData[1]); } else { handlers[jsonData[0].ToString()](peer, null); } } }; listener.PeerDisconnectedEvent += (peer, disconnectInfo) => { Console.WriteLine("peer disconnectd ===>", peer.EndPoint); // Show peer ip onPeerDisconnected?.Invoke(peer); }; pollEventThread = new Thread(pollEvent); pollEventThread.Start(); }
private void Listener_PeerConnectedEvent(NetPeer peer) { int id = peer.Id + 1; jitterBuffersByPeerId[id] = new JitterBuffer(settings.JitterBufferDelayFrames); NetDataWriter writer = new NetDataWriter(); writer.Put((byte)PacketType.Join); writer.Put(id); writer.Put(StatefulWorld.Instance.Frame); peer.Send(writer, DeliveryMethod.ReliableOrdered); writer.Reset(); Remote.SynchronizeAll(); NetDebug.Log($"Peer index <b>{peer.Id}</b> connected. Assigned global ID {id}.", LogType.Connection); OnPeerConnected?.Invoke(id); }
/// <summary> /// Processes all events /// </summary> public override void ProcessEvents() { while (localPeerConnectionAttemptMessages.TryDequeue(out LocalPeerConnectionAttemptMessage local_peer_connection_attempt_message)) { IPeer peer = local_peer_connection_attempt_message.Peer; OnPeerConnectionAttempted?.Invoke(peer); if (IsConnectionAllowed(peer)) { peers.Add(peer.GUID.ToString(), peer); OnPeerConnected?.Invoke(peer); } else { local_peer_connection_attempt_message.Peer.Disconnect(EDisconnectionReason.Banned); } } while (localPeerDisconnectionMessages.TryDequeue(out LocalPeerDisconnectionMessage local_peer_disconnection_message)) { IPeer peer = local_peer_disconnection_message.Peer; string key = peer.GUID.ToString(); if (peers.ContainsKey(key)) { OnPeerDisconnected?.Invoke(peer); peers.Remove(key); } foreach (KeyValuePair <string, ILocalPeer> peer_peer in peerPeerLookup) { if (peer.GUID == peer_peer.Value.GUID) { peerPeerLookup.Remove(peer_peer.Key); break; } } } while (localPeerReceiveMessages.TryDequeue(out LocalPeerReceiveMessage local_peer_receive_message)) { OnPeerMessageReceived?.Invoke(local_peer_receive_message.Peer, Compression.Decompress(local_peer_receive_message.Message, local_peer_receive_message.Index, local_peer_receive_message.Length)); } }