Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
narrowed IServerContext
Browse files Browse the repository at this point in the history
  • Loading branch information
osmirnov committed Jun 11, 2018
1 parent 8a96f22 commit 4b838f0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 36 deletions.
2 changes: 1 addition & 1 deletion NeoSharp.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<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/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PublicFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String></wpf:ResourceDictionary>
5 changes: 0 additions & 5 deletions src/NeoSharp.Core/Network/IServerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,5 @@ public interface IServerContext
/// Version
/// </summary>
VersionPayload Version { get; }

/// <summary>
/// Update version payload
/// </summary>
void UpdateVersionPayload();
}
}
11 changes: 3 additions & 8 deletions src/NeoSharp.Core/Network/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class Server : IServer, IDisposable
{
#region Constants

private const int DefaultReceiveTimeout = 1000;
private static readonly TimeSpan DefaultMessagePollingInterval = TimeSpan.FromMilliseconds(100);
private static readonly TimeSpan DefaultReceiveTimeout = TimeSpan.FromMilliseconds(1_000);

#endregion

Expand Down Expand Up @@ -81,7 +82,6 @@ public class Server : IServer, IDisposable

// TODO: Change after port forwarding implementation
_peerEndPoints = config.PeerEndPoints;
_serverContext.UpdateVersionPayload();
}

/// <summary>
Expand Down Expand Up @@ -134,12 +134,7 @@ private void PeerConnected(object sender, IPeer peer)

ListenForMessages(peer, _messageListenerTokenSource.Token);

// Update version payload

_serverContext.UpdateVersionPayload();

// Initiate handshake

peer.Send(new VersionMessage(_serverContext.Version));
}
catch (Exception e)
Expand Down Expand Up @@ -216,7 +211,7 @@ private void ListenForMessages(IPeer peer, CancellationToken cancellationToken)
var message = await peer.Receive();
if (message == null)
{
await _asyncDelayer.Delay(ServerContext.MessagePollingInterval, cancellationToken);
await _asyncDelayer.Delay(DefaultMessagePollingInterval, cancellationToken);
continue;
}
Expand Down
37 changes: 15 additions & 22 deletions src/NeoSharp.Core/Network/ServerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ namespace NeoSharp.Core.Network
{
public class ServerContext : IServerContext
{
#region Constants

/// <summary>
/// Default interval for message polling
/// </summary>
public static readonly TimeSpan MessagePollingInterval = TimeSpan.FromMilliseconds(100);

#endregion

/// <summary>
/// Blockchain
/// </summary>
private readonly IBlockchain _blockchain;

/// <summary>
/// Version
/// </summary>
public VersionPayload Version { get; private set; }
private readonly VersionPayload _version;

/// <inheritdoc />
public VersionPayload Version
{
get
{
_version.Timestamp = DateTime.UtcNow.ToTimestamp();
_version.CurrentBlockIndex = _blockchain.CurrentBlock?.Index ?? 0;

return _version;
}

}

/// <summary>
/// Server context
Expand All @@ -37,7 +38,7 @@ public ServerContext(NetworkConfig config, IBlockchain blockchain)
if (config == null) throw new ArgumentNullException(nameof(config));
_blockchain = blockchain ?? throw new ArgumentNullException(nameof(blockchain));

Version = new VersionPayload
_version = new VersionPayload
{
Version = 2,
// TODO: What's it?
Expand All @@ -50,13 +51,5 @@ public ServerContext(NetworkConfig config, IBlockchain blockchain)
Relay = true
};
}
/// <summary>
/// Update version payload
/// </summary>
public void UpdateVersionPayload()
{
Version.Timestamp = DateTime.UtcNow.ToTimestamp();
Version.CurrentBlockIndex = _blockchain.CurrentBlock?.Index ?? 0;
}
}
}

0 comments on commit 4b838f0

Please sign in to comment.