Skip to content
This repository was archived by the owner on Oct 28, 2022. It is now read-only.

Commit 1090db8

Browse files
committed
Removed async keyword which was not working on Mono and made it plain .NET 4.0 and fully compatible with Xamarin Studio :)
1 parent 61311bb commit 1090db8

File tree

9 files changed

+40
-142
lines changed

9 files changed

+40
-142
lines changed

ReactiveSockets-rc.nuspec

Lines changed: 0 additions & 38 deletions
This file was deleted.

ReactiveSockets.nuspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
33
<metadata>
44
<id>ReactiveSockets</id>
5-
<version>0.1.3</version>
5+
<version>0.2.0</version>
66
<title>Reactive Sockets</title>
77
<summary>The easiest way to do socket programming in .NET, leveraging simple Rx queries to implement your protocols.</summary>
88
<description>
@@ -20,12 +20,12 @@
2020
<iconUrl>https://github.com/clariuslabs/adapter/raw/master/Common/ClariusLabsIcon.png</iconUrl>
2121
<tags>reactive socket</tags>
2222
<dependencies>
23-
<group targetFramework="net45">
23+
<group targetFramework="net40">
2424
<dependency id="Rx-Main" version="2.1.30214.0" />
2525
</group>
2626
</dependencies>
2727
<frameworkAssemblies>
28-
<frameworkAssembly assemblyName="System.Net" targetFramework="net45" />
28+
<frameworkAssembly assemblyName="System.Net" targetFramework="net40" />
2929
</frameworkAssemblies>
3030
</metadata>
3131
</package>

ReactiveSockets/ReactiveClient.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ public ReactiveClient(string hostname, int port)
2626
/// <summary>
2727
/// Attemps to connect to the TCP server.
2828
/// </summary>
29-
public async Task ConnectAsync()
29+
public Task ConnectAsync()
3030
{
3131
var client = new TcpClient();
32-
await client.ConnectAsync(hostname, port);
33-
Connect(client);
32+
return Task.Factory
33+
.FromAsync<string, int>(client.BeginConnect, client.EndConnect, hostname, port, null)
34+
.ContinueWith(_ => Connect(client), TaskContinuationOptions.OnlyOnRanToCompletion);
3435
}
3536

3637
/// <summary>

ReactiveSockets/ReactiveListener.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Net.Sockets;
88
using System.Reactive.Linq;
99
using System.Reactive.Subjects;
10+
using System.Threading.Tasks;
1011

1112
/// <summary>
1213
/// Implements a TCP listener.
@@ -74,7 +75,7 @@ public void Start()
7475
.FromAsync(() =>
7576
{
7677
Tracer.Log.ReactiveListenerAwaitingNewTcpConnection();
77-
return listener.AcceptTcpClientAsync();
78+
return Task.Factory.FromAsync<TcpClient>(listener.BeginAcceptTcpClient, listener.EndAcceptTcpClient, TaskCreationOptions.AttachedToParent);
7879
})
7980
.Repeat()
8081
.Select(client => new ReactiveSocket(client))

ReactiveSockets/ReactiveSocket.cs

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ internal ReactiveSocket(TcpClient client)
4949
/// Protected constructor used by <see cref="ReactiveClient"/>
5050
/// client.
5151
/// </summary>
52-
protected internal ReactiveSocket()
52+
protected internal ReactiveSocket()
5353
{
5454
receiver = received.GetConsumingEnumerable().ToObservable(TaskPoolScheduler.Default);
5555
}
@@ -140,9 +140,7 @@ protected internal void Connect(TcpClient client)
140140
cancellation = new CancellationTokenSource();
141141

142142
// Subscribe to the new client with the new token.
143-
Observable
144-
.Create<byte>(obs => Read(client, obs, cancellation.Token))
145-
.Subscribe(b => received.Add(b), cancellation.Token);
143+
BeginRead();
146144

147145
Connected(this, EventArgs.Empty);
148146

@@ -205,39 +203,36 @@ public void Dispose()
205203
Disposed(this, EventArgs.Empty);
206204
}
207205

208-
private async Task<IDisposable> Read(TcpClient client, IObserver<byte> obs, CancellationToken token)
206+
private void BeginRead()
209207
{
210-
while (!token.IsCancellationRequested)
208+
Task.Factory.StartNew(() =>
211209
{
212-
try
213-
{
214-
var buffer = new byte[client.Available];
215-
var count = await client.GetStream().ReadAsync(buffer, 0, buffer.Length, token);
216-
foreach (var b in buffer.Take(count))
217-
obs.OnNext(b);
218-
}
219-
catch (Exception e)
210+
var stream = client.GetStream();
211+
var buffer = new byte[128];
212+
while (!cancellation.IsCancellationRequested)
220213
{
221-
if (!token.IsCancellationRequested)
214+
try
222215
{
223-
Tracer.Log.ReactiveSocketReadFailed(e);
224-
225-
obs.OnError(e);
226-
Disconnect(false);
216+
var read = Task.Factory.FromAsync<byte[], int, int, int>(
217+
stream.BeginRead,
218+
stream.EndRead,
219+
buffer, 0, buffer.Length,
220+
default(object),
221+
TaskCreationOptions.AttachedToParent).Result;
222+
223+
for (int i = 0; i < read; i++)
224+
received.Add(buffer[i]);
227225
}
228-
// Token cancellation was requested: we have two
229-
// scenarios:
230-
// - Dispose: requested the cancel as part of a Dispose.
231-
// this is truly the end of this instance.
232-
// - Disconnect: requested cancel but we may reconnect.
233-
else if (disposed)
226+
catch (Exception e)
234227
{
235-
obs.OnCompleted();
228+
if (!cancellation.IsCancellationRequested)
229+
{
230+
Tracer.Log.ReactiveSocketReadFailed(e);
231+
Disconnect(false);
232+
}
236233
}
237234
}
238-
}
239-
240-
return (IDisposable)cancellation;
235+
}, cancellation.Token);
241236
}
242237

243238
/// <summary>
@@ -271,8 +266,8 @@ public Task SendAsync(byte[] bytes, CancellationToken cancellation)
271266
syncLock.EnterWriteLock();
272267
try
273268
{
274-
client.GetStream()
275-
.WriteAsync(bytes, 0, bytes.Length, cancellation)
269+
var stream = client.GetStream();
270+
Task.Factory.FromAsync<byte[], int, int>(stream.BeginWrite, stream.EndWrite, bytes, 0, bytes.Length, null, TaskCreationOptions.AttachedToParent)
276271
.Wait(cancellation);
277272

278273
foreach (var b in bytes)

ReactiveSockets/ReactiveSockets.csproj

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>ReactiveSockets</RootNamespace>
1111
<AssemblyName>ReactiveSockets</AssemblyName>
12-
<TargetFrameworkVersion Condition="'$(TargetFrameworkVersion)' == ''">v4.5</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<TargetFrameworkProfile />
1515
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
@@ -34,22 +34,7 @@
3434
<WarningLevel>4</WarningLevel>
3535
<Prefer32Bit>false</Prefer32Bit>
3636
</PropertyGroup>
37-
<ItemGroup Condition="'$(TargetFrameworkVersion)' == 'v4.0'">
38-
<Reference Include="Microsoft.Threading.Tasks">
39-
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.14-rc\lib\net40\Microsoft.Threading.Tasks.dll</HintPath>
40-
</Reference>
41-
<Reference Include="Microsoft.Threading.Tasks.Extensions">
42-
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.14-rc\lib\net40\Microsoft.Threading.Tasks.Extensions.dll</HintPath>
43-
</Reference>
44-
<Reference Include="Microsoft.Threading.Tasks.Extensions.Desktop">
45-
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.14-rc\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll</HintPath>
46-
</Reference>
47-
<Reference Include="System.Runtime">
48-
<HintPath>..\packages\Microsoft.Bcl.1.0.16-rc\lib\net40\System.Runtime.dll</HintPath>
49-
</Reference>
50-
<Reference Include="System.Threading.Tasks">
51-
<HintPath>..\packages\Microsoft.Bcl.1.0.16-rc\lib\net40\System.Threading.Tasks.dll</HintPath>
52-
</Reference>
37+
<ItemGroup>
5338
<Reference Include="System.Reactive.Core">
5439
<HintPath>..\packages\Rx-Core.2.1.30214.0\lib\Net40\System.Reactive.Core.dll</HintPath>
5540
</Reference>
@@ -64,21 +49,6 @@
6449
<HintPath>..\packages\Rx-PlatformServices.2.1.30214.0\lib\Net40\System.Reactive.PlatformServices.dll</HintPath>
6550
</Reference>
6651
</ItemGroup>
67-
<ItemGroup Condition="'$(TargetFrameworkVersion)' == 'v4.5'">
68-
<Reference Include="System.Reactive.Core">
69-
<HintPath>..\packages\Rx-Core.2.1.30214.0\lib\Net45\System.Reactive.Core.dll</HintPath>
70-
</Reference>
71-
<Reference Include="System.Reactive.Interfaces">
72-
<HintPath>..\packages\Rx-Interfaces.2.1.30214.0\lib\Net45\System.Reactive.Interfaces.dll</HintPath>
73-
</Reference>
74-
<Reference Include="System.Reactive.Linq">
75-
<HintPath>..\packages\Rx-Linq.2.1.30214.0\lib\Net45\System.Reactive.Linq.dll</HintPath>
76-
</Reference>
77-
<Reference Include="System.Reactive.PlatformServices">
78-
<SpecificVersion>False</SpecificVersion>
79-
<HintPath>..\packages\Rx-PlatformServices.2.1.30214.0\lib\Net45\System.Reactive.PlatformServices.dll</HintPath>
80-
</Reference>
81-
</ItemGroup>
8252
<ItemGroup>
8353
<Reference Include="System" />
8454
<Reference Include="System.Core" />
@@ -128,7 +98,6 @@
12898
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
12999
</ItemGroup>
130100
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
131-
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.0-rc\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.0-rc\tools\Microsoft.Bcl.Build.targets')" />
132101
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
133102
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
134103
Other similar extension points exist, see Microsoft.Common.targets.

ReactiveSockets/app.config

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,2 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<configuration>
3-
<runtime>
4-
<assemblyBinding xmlns:bcl="urn:schemas-microsoft-com:bcl" xmlns="urn:schemas-microsoft-com:asm.v1">
5-
<dependentAssembly bcl:name="System.Runtime">
6-
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
7-
<bindingRedirect oldVersion="0.0.0.0-2.5.16.0" newVersion="2.5.16.0" />
8-
</dependentAssembly>
9-
<dependentAssembly bcl:name="System.Threading.Tasks">
10-
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
11-
<bindingRedirect oldVersion="0.0.0.0-2.5.16.0" newVersion="2.5.16.0" />
12-
</dependentAssembly>
13-
</assemblyBinding>
14-
</runtime>
15-
</configuration>
2+
<configuration />

ReactiveSockets/packages.config

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Microsoft.Bcl" version="1.0.16-rc" targetFramework="net40" />
4-
<package id="Microsoft.Bcl.Async" version="1.0.14-rc" targetFramework="net40" />
5-
<package id="Microsoft.Bcl.Build" version="1.0.0-rc" targetFramework="net40" />
63
<package id="netfx-System.StringFormatWith" version="1.1.0.0" targetFramework="net40" />
74
<package id="netfx-System.StringResources" version="1.0.0.10" targetFramework="net40" />
85
<package id="Rx-Core" version="2.1.30214.0" targetFramework="net40" />

build.bat

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
echo Initializing
2-
rmdir /s /q out45
32
rmdir /s /q out40
43
.nuget\nuget install .\ReactiveSockets\packages.config -OutputDirectory packages
54

6-
echo Building stable version
7-
8-
md .\out45
9-
md .\out45\lib
10-
md .\out45\lib\net45
11-
msbuild .\ReactiveSockets\ReactiveSockets.csproj /p:Configuration=Release /p:TargetFrameworkVersion=v4.5 /t:Rebuild
12-
xcopy .\ReactiveSockets\bin\Release\ReactiveSockets.dll .\out45\lib\net45\* /Y
13-
xcopy .\ReactiveSockets\bin\Release\ReactiveSockets.xml .\out45\lib\net45\* /Y
14-
xcopy .\ReactiveSockets.nuspec .\out45\* /Y
15-
.nuget\nuget pack .\out45\ReactiveSockets.nuspec
16-
17-
echo Building prerelease version
18-
195
md .\out40
206
md .\out40\lib
217
md .\out40\lib\net40
228
msbuild .\ReactiveSockets\ReactiveSockets.csproj /p:Configuration=Release /p:TargetFrameworkVersion=v4.0 /t:Rebuild
239
xcopy .\ReactiveSockets\bin\Release\ReactiveSockets.dll .\out40\lib\net40\* /Y
2410
xcopy .\ReactiveSockets\bin\Release\ReactiveSockets.xml .\out40\lib\net40\* /Y
25-
xcopy .\ReactiveSockets-rc.nuspec .\out40\* /Y
26-
.nuget\nuget pack .\out40\ReactiveSockets-rc.nuspec
11+
xcopy .\ReactiveSockets.nuspec .\out40\* /Y
12+
.nuget\nuget pack .\out40\ReactiveSockets.nuspec

0 commit comments

Comments
 (0)