void ParseTrackers() { string[] addresses = VotingsUser.Trackers.Split('\n'); foreach (var line in addresses) { var parts = line.Split(':'); IPAddress addr = new IPAddress(0); /*int port = 0;*/ if (parts.Length >= 1 && IPAddress.TryParse(parts[0], out addr) /*&& int.TryParse(parts[1], out port)*/) { EndPoint endPoint = new IPEndPoint(addr, CommonHelpers.TrackerPort); Tracker tracker = new Tracker(endPoint, Trackers); //перенаправляем события трекера вне tracker.OnRequestBlocksMessage += (s, e) => { OnRequestBlocksMessage?.Invoke(s, e); }; tracker.OnRequestTransactionsMessage += (s, e) => { OnRequestTransactionsMessage?.Invoke(s, e); }; tracker.OnBlocksMessage += (s, e) => { OnBlocksMessage?.Invoke(s, e); }; tracker.OnTransactionsMessage += (s, e) => { OnTransactionsMessage?.Invoke(s, e); }; //получение пиров tracker.OnPeersMessageFromPeer += OnPeersMessage; tracker.OnPeersMessageFromTracker += OnPeersMessage; //коннект через трекер tracker.OnConnectToPeerWithTrackerMessage += OnConnectToPeerWithTrackerMessage; Trackers.Add(tracker); } } }
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)); }); } }
private void OnToPeerMessage(PacketHeader packetHeader, Connection connection, ToPeerMessage incomingObject) { NetworkComms.Logger.Warn("Received message: " + incomingObject.Message.Type.ToString()); if (incomingObject.RecieverAddress.Equals(CommonHelpers.GetLocalEndPoint(CommonHelpers.PeerPort, true))) { var eventArgs = new MessageEventArgs(incomingObject.Message, null, incomingObject.SenderAddress); if (incomingObject.Message is PeerHashMessage) { OnPeerHashMessage?.Invoke(this, eventArgs); } else if (incomingObject.Message is RequestPeersMessage) { OnRequestPeersMessage?.Invoke(this, eventArgs); } else if (incomingObject.Message is PeersMessage) { OnPeersMessageFromPeer?.Invoke(this, eventArgs); } else if (incomingObject.Message is RequestBlocksMessage) { OnRequestBlocksMessage?.Invoke(this, eventArgs); } else if (incomingObject.Message is RequestTransactionsMessage) { OnRequestTransactionsMessage?.Invoke(this, eventArgs); } else if (incomingObject.Message is BlocksMessage) { OnBlocksMessage?.Invoke(this, eventArgs); } else if (incomingObject.Message is TransactionsMessage) { OnTransactionsMessage?.Invoke(this, eventArgs); } } }
public void Connect(bool withTracker = false) { if (Status == PeerStatus.NoHashRecieved) { RequestPeerHash(); return; } else if (Status == PeerStatus.Connected) { return; } //попытка подключения сразу через трекер (используется если поступил запрос от трекера) else if (withTracker || !VotingsUser.PeerDiscovery) { ConnectWithTracker(); } else { //пытаемся подключиться напрямую try { ConnectionInfo connInfo = new ConnectionInfo(Address); TCPConnection newTCPConn = TCPConnection.GetConnection(connInfo); ConnectionMode = ConnectionMode.Direct; Status = PeerStatus.NoHashRecieved; Connection = newTCPConn; //обработчики приходящих сообщений внутри пира //Connection.AppendShutdownHandler((c) => DisconnectDirect(false)); if (!Connection.IncomingPacketHandlerExists(typeof(PeerDisconnectMessage).Name)) { Connection.AppendIncomingPacketHandler <PeerDisconnectMessage>(typeof(PeerDisconnectMessage).Name, (p, c, m) => DisconnectAny(false)); } if (!Connection.IncomingPacketHandlerExists(typeof(PeerHashMessage).Name)) { Connection.AppendIncomingPacketHandler <PeerHashMessage>(typeof(PeerHashMessage).Name, (p, c, m) => OnPeerHashMessageDirect(m)); } if (!Connection.IncomingPacketHandlerExists(typeof(RequestPeersMessage).Name)) { Connection.AppendIncomingPacketHandler <RequestPeersMessage>(typeof(RequestPeersMessage).Name, (p, c, m) => OnRequestPeersMessageDirect(m)); } //вызов внешних событий if (!Connection.IncomingPacketHandlerExists(typeof(RequestBlocksMessage).Name)) { Connection.AppendIncomingPacketHandler <RequestBlocksMessage>(typeof(RequestBlocksMessage).Name, (p, c, m) => { OnRequestBlocksMessage?.Invoke(this, new MessageEventArgs(m, Hash, Address)); }); } if (!Connection.IncomingPacketHandlerExists(typeof(RequestTransactionsMessage).Name)) { Connection.AppendIncomingPacketHandler <RequestTransactionsMessage>(typeof(RequestTransactionsMessage).Name, (p, c, m) => { OnRequestTransactionsMessage?.Invoke(this, new MessageEventArgs(m, Hash, Address)); }); } if (!Connection.IncomingPacketHandlerExists(typeof(BlocksMessage).Name)) { Connection.AppendIncomingPacketHandler <BlocksMessage>(typeof(BlocksMessage).Name, (p, c, m) => { OnBlocksMessage?.Invoke(this, new MessageEventArgs(m, Hash, Address)); }); } if (!Connection.IncomingPacketHandlerExists(typeof(TransactionsMessage).Name)) { Connection.AppendIncomingPacketHandler <TransactionsMessage>(typeof(TransactionsMessage).Name, (p, c, m) => { OnTransactionsMessage?.Invoke(this, new MessageEventArgs(m, Hash, Address)); }); } if (!Connection.IncomingPacketHandlerExists(typeof(PeersMessage).Name)) { Connection.AppendIncomingPacketHandler <PeersMessage>(typeof(PeersMessage).Name, (p, c, m) => { OnPeersMessage?.Invoke(this, new MessageEventArgs(m, Hash, Address)); }); } Thread.Sleep(CommonHelpers.MessagesInterval); RequestPeerHash(); } catch { Connection = null; //если трекер не задан, то ошибка if (tracker == null) { ErrorsCount++; Status = PeerStatus.Disconnected; } //пытаемся подключиться через трекер else { ConnectWithTracker(); } } } //если более трех ошибок подключения, то удаляем пир из списка if (ErrorsCount >= 3 && Status == PeerStatus.Disconnected) { DisconnectAny(); } }