Skip to content

Commit c377138

Browse files
committed
added exception event for tcp server.
1 parent 4731a4b commit c377138

7 files changed

Lines changed: 70 additions & 24 deletions

File tree

build/version.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup Condition="'$(AssemblyName)'=='GodSharp.Socket'">
33
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
4-
<Version>2019.1.0.0-preview3</Version>
5-
<FileVersion>2019.1.0.0-preview3</FileVersion>
4+
<Version>2019.1.0.0-preview4</Version>
5+
<FileVersion>2019.1.0.0-preview4</FileVersion>
66
</PropertyGroup>
77
</Project>

samples/GodSharp.Socket.TcpServerSample/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ static void Main(string[] args)
3838
OnException = (c) =>
3939
{
4040
Console.WriteLine($"{c.RemoteEndPoint} exception:{c.Exception.StackTrace.ToString()}.");
41+
},
42+
OnServerException= (c) =>
43+
{
44+
Console.WriteLine($"{c.LocalEndPoint} exception:{c.Exception.StackTrace.ToString()}.");
4145
}
4246
};
4347

@@ -49,6 +53,8 @@ static void Main(string[] args)
4953

5054
server.Stop();
5155
Console.WriteLine("GodSharp.Socket.TcpServer Stopped!");
56+
57+
Console.ReadLine();
5258
}
5359
}
5460
}

src/GodSharp.Socket/Abstractions/Components/ITcpServer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ public interface ITcpServer : INetBase<ITcpConnection>, IDisposable
1010
Socket Instance { get; }
1111

1212
IDictionary<string, ITcpConnection> Connections { get; }
13+
14+
SocketEventHandler<NetServerEventArgs> OnServerException { get; set; }
1315
}
14-
}
16+
}

src/GodSharp.Socket/Abstractions/Events/NetEventArgs.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ public class NetServerEventArgs : NetEventArgs
9999
/// </value>
100100
public ITcpServer TcpServer { get; internal set; }
101101

102+
/// <summary>
103+
/// Gets or sets the connection.
104+
/// </summary>
105+
/// <value>
106+
/// The connection.
107+
/// </value>
108+
public ITcpConnection Connection { get; set; }
109+
102110
/// <summary>
103111
/// Initializes a new instance of the <see cref="NetServerEventArgs"/> class.
104112
/// </summary>
@@ -111,10 +119,12 @@ public NetServerEventArgs()
111119
/// </summary>
112120
/// <param name="tcpServer">The TCP server.</param>
113121
/// <param name="localEndPoint">The local end point.</param>
114-
public NetServerEventArgs(ITcpServer tcpServer, IPEndPoint localEndPoint)
122+
/// <param name="connection">The connection.</param>
123+
public NetServerEventArgs(ITcpServer tcpServer, IPEndPoint localEndPoint = null, ITcpConnection connection = null)
115124
{
116125
if (tcpServer != null) TcpServer = tcpServer;
117126
if (localEndPoint != null) LocalEndPoint = localEndPoint;
127+
if (connection != null) Connection = connection;
118128
}
119129
}
120130
}

src/GodSharp.Socket/Abstractions/Listeners/NetListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void Stop()
4848
Exception exception = null;
4949
try
5050
{
51-
Connection?.Instance.Disconnect(true);
51+
Connection?.Instance.Disconnect(false);
5252
Connection?.Instance.Close();
5353
}
5454
catch (Exception ex)

src/GodSharp.Socket/Tcp/TcpServer.cs

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace GodSharp.Sockets
1010
{
1111
public sealed class TcpServer :NetBase<ITcpConnection, NetServerEventArgs>, ITcpServer, IDisposable
1212
{
13+
private readonly object ConnectionsLock = new object();
14+
1315
private bool stopping = false;
1416

1517
private bool running;
@@ -18,9 +20,11 @@ public sealed class TcpServer :NetBase<ITcpConnection, NetServerEventArgs>, ITcp
1820
public Socket Instance { get; private set; }
1921

2022
public IPEndPoint LocalEndPoint { get; private set; }
21-
23+
2224
public IDictionary<string, ITcpConnection> Connections { get; private set; } = new Dictionary<string, ITcpConnection>();
2325

26+
public SocketEventHandler<NetServerEventArgs> OnServerException { get; set; }
27+
2428
public TcpServer(TcpServerOptions options) => OnConstructing(options);
2529

2630
public TcpServer(int port = 7788, string host = null, int backlog = int.MaxValue, AddressFamily family = AddressFamily.InterNetwork, string name = null, int id = 0)
@@ -33,6 +37,7 @@ public TcpServer(int port = 7788, string host = null, int backlog = int.MaxValue
3337
}
3438
catch (Exception ex)
3539
{
40+
OnServerException?.Invoke(new NetServerEventArgs(this, LocalEndPoint) { Exception = ex });
3641
throw ex;
3742
}
3843
}
@@ -78,6 +83,7 @@ private void OnConstructing(TcpServerOptions options)
7883
}
7984
catch (Exception ex)
8085
{
86+
OnServerException?.Invoke(new NetServerEventArgs(this, LocalEndPoint) { Exception = ex });
8187
throw ex;
8288
}
8389
}
@@ -96,7 +102,7 @@ public override void Start()
96102
}
97103
catch (Exception ex)
98104
{
99-
throw ex;
105+
OnServerException?.Invoke(new NetServerEventArgs(this, LocalEndPoint) { Exception = ex });
100106
}
101107
}
102108

@@ -112,7 +118,7 @@ public override void Stop()
112118
}
113119
catch (Exception ex)
114120
{
115-
throw ex;
121+
OnServerException?.Invoke(new NetServerEventArgs(this, LocalEndPoint) { Exception = ex });
116122
}
117123
finally
118124
{
@@ -121,7 +127,7 @@ public override void Stop()
121127

122128
running = false;
123129
stopping = false;
124-
130+
125131
OnStopped?.Invoke(new NetServerEventArgs(this, LocalEndPoint));
126132
}
127133

@@ -133,12 +139,23 @@ protected override void OnDisconnectedHandler(NetClientEventArgs<ITcpConnection>
133139

134140
private void BeginAccept()
135141
{
136-
Instance.BeginAccept(AcceptCallback, null);
142+
try
143+
{
144+
Instance.BeginAccept(AcceptCallback, null);
145+
}
146+
catch (Exception ex)
147+
{
148+
OnServerException?.Invoke(new NetServerEventArgs(this, LocalEndPoint) { Exception = ex });
149+
150+
Stop();
151+
}
137152
}
138153

139154
private void AcceptCallback(IAsyncResult result)
140155
{
141-
bool error = false;
156+
bool accept = false;
157+
ITcpConnection connection = null;
158+
142159
try
143160
{
144161
if (!Running || stopping) return;
@@ -148,35 +165,38 @@ private void AcceptCallback(IAsyncResult result)
148165
if (stopping) return;
149166

150167
BeginAccept();
168+
accept = true;
151169

152-
ITcpConnection connection = new TcpConnection(socket) { OnConnected = OnConnectedHandler, OnReceived = OnReceivedHandler, OnDisconnected = OnDisconnectedHandler, OnException = OnExceptionHandler };
170+
connection = new TcpConnection(socket) { OnConnected = OnConnectedHandler, OnReceived = OnReceivedHandler, OnDisconnected = OnDisconnectedHandler, OnException = OnExceptionHandler };
153171
connection.Start();
154172

155173
RemoveListener(connection.Key);
156174

157-
Connections.Add(connection.Key, connection);
175+
lock (ConnectionsLock)
176+
{
177+
Connections.Add(connection.Key, connection);
178+
}
158179

159180
OnConnected?.Invoke(new NetClientEventArgs<ITcpConnection>(connection));
160181
}
161-
catch (SocketException ex)
162-
{
163-
error = true;
164-
throw ex;
165-
}
166182
catch (Exception ex)
167183
{
168-
error = true;
169-
throw ex;
184+
OnServerException?.Invoke(new NetServerEventArgs(this, LocalEndPoint, connection) { Exception = ex });
170185
}
171186
finally
172187
{
173-
if (error) Stop();
188+
if (Running && !accept && !stopping) BeginAccept();
174189
}
175190
}
176191

177192
private void RemoveListener(string key)
178193
{
179-
bool existed = Connections?.ContainsKey(key) == true;
194+
bool existed = false;
195+
196+
lock (ConnectionsLock)
197+
{
198+
existed = Connections?.ContainsKey(key) == true;
199+
}
180200

181201
try
182202
{
@@ -188,11 +208,17 @@ private void RemoveListener(string key)
188208
}
189209
catch (Exception ex)
190210
{
191-
Console.WriteLine(ex.Message);
211+
OnServerException?.Invoke(new NetServerEventArgs(this, LocalEndPoint) { Exception = ex });
192212
}
193213
finally
194214
{
195-
if (existed) Connections?.Remove(key);
215+
if (existed)
216+
{
217+
lock (ConnectionsLock)
218+
{
219+
Connections?.Remove(key);
220+
}
221+
}
196222
}
197223
}
198224

src/GodSharp.Socket/Tcp/TcpServerOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class TcpServerOptions : NetOptions<ITcpConnection>
1616

1717
public SocketEventHandler<NetServerEventArgs> OnStopped { get; set; }
1818

19+
public SocketEventHandler<NetServerEventArgs> OnServerException { get; set; }
20+
1921
public TcpServerOptions()
2022
{
2123
}

0 commit comments

Comments
 (0)