This commit is contained in:
2022-01-02 12:59:05 +01:00
parent 0975975c5e
commit 63790f1bb5
7 changed files with 168 additions and 1 deletions

13
.idea/.idea.BasicBlockChain/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,13 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/modules.xml
/contentModel.xml
/projectSettingsUpdater.xml
/.idea.BasicBlockChain.iml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@@ -33,11 +33,11 @@
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Net" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="VAR.Json, Version=1.2.0.1065, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="VAR.Json, Version=1.2.0.1065, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\VAR.Json.1.2.0.1065\lib\net461\VAR.Json.dll</HintPath> <HintPath>..\packages\VAR.Json.1.2.0.1065\lib\net461\VAR.Json.dll</HintPath>
@@ -46,6 +46,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="Block.cs" /> <Compile Include="Block.cs" />
<Compile Include="BlockChain.cs" /> <Compile Include="BlockChain.cs" />
<Compile Include="P2PNode.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Transaction.cs" /> <Compile Include="Transaction.cs" />
</ItemGroup> </ItemGroup>

View File

@@ -0,0 +1,131 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace BasicBlockChain.Core
{
public class NetNode
{
private PeerAddress _nodeAddress = null;
private List<NetPeer> _peers = new List<NetPeer>();
private Socket _listen = null;
private Thread _thread = null;
private bool _running = false;
public NetNode(PeerAddress nodeAddress)
{
_nodeAddress = nodeAddress;
StartListening();
_running = true;
_thread = new Thread(Run);
_thread.Start();
}
public void AddPeer(PeerAddress peerAddress)
{
NetPeer newPeer = new NetPeer(_nodeAddress, peerAddress);
lock (_peers)
{
_peers.Add(newPeer);
}
}
private void StartListening()
{
_listen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_listen.Bind(new IPEndPoint(IPAddress.Any, _nodeAddress.Port));
_listen.Listen(1000);
}
private void Run()
{
while (_running)
{
if (_listen.Poll(100, SelectMode.SelectRead) == false) { continue; }
Socket newSock = _listen.Accept();
NetPeer newPeer = new NetPeer(_nodeAddress, null, newSock);
lock (_peers)
{
_peers.Add(newPeer);
}
}
}
}
public class PeerAddress
{
public string Address { get; set; }
public int Port { get; set; }
}
public class NetPeer
{
private PeerAddress _nodeAddress = null;
private PeerAddress _peerAddress = null;
private Socket _socket = null;
private Thread _thread = null;
private bool _running = false;
private enum PeerStatus
{
None,
SendingId,
WaitingId,
Idle,
Ended,
};
private PeerStatus _status = PeerStatus.None;
public NetPeer(PeerAddress nodeAddress, PeerAddress peerAddress, Socket socket = null)
{
_nodeAddress = nodeAddress;
_peerAddress = peerAddress;
_socket = socket;
_status = PeerStatus.Idle;
if (_socket == null)
{
ConnectPeer();
_status = PeerStatus.SendingId;
}
if (peerAddress == null)
{
_status = PeerStatus.WaitingId;
}
_socket.Blocking = false;
_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
_running = true;
_thread = new Thread(Run);
_thread.Start();
}
private void ConnectPeer()
{
IPAddress ipAddress = IPAddress.Parse(_peerAddress.Address);
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.Connect(ipAddress, _peerAddress.Port);
}
private void Run()
{
while (_running)
{
_socket.Poll(1, SelectMode.SelectRead);
// TODO
}
}
}
}

View File

@@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=135c9ccb_002Da4bf_002D4bc5_002Dacf7_002De923283d2c6b/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>