Skip to content

Commit 52bb87c

Browse files
author
“寧々”
committed
2021.10.10
1 parent 57a5d0a commit 52bb87c

File tree

8 files changed

+37
-9
lines changed

8 files changed

+37
-9
lines changed

MiniCarLib/Core/IComClient.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace MiniCarLib.Core
1111
public interface IComClient
1212
{
1313
bool IsAlive { get; set; }
14+
15+
void Disconnect();
1416
}
1517

1618
public class CarUDPClient : IComClient
@@ -23,6 +25,11 @@ public CarUDPClient(IPEndPoint cl)
2325
public bool IsAlive { get { return true; } set { } }
2426

2527
public IPEndPoint Client { get; }
28+
29+
public virtual void Disconnect()
30+
{
31+
//throw new NotImplementedException();
32+
}
2633
}
2734

2835
public class CarTCPClient : IComClient
@@ -33,6 +40,12 @@ public CarTCPClient(TcpClient cl)
3340
}
3441
public TcpClient Client { get; }
3542
public bool IsAlive { get ; set ; }
43+
44+
public void Disconnect()
45+
{
46+
Client.Close();
47+
IsAlive = false;
48+
}
3649
//public bool IsAlive => Client.Connected;
3750
}
3851
}

MiniCarLib/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
3333
//通过使用 "*",如下所示:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.04.0.0")]
36-
[assembly: AssemblyFileVersion("1.04.0.0")]
35+
[assembly: AssemblyVersion("1.6.0.0")]
36+
[assembly: AssemblyFileVersion("1.6.0.0")]

MiniCarLib/QianCar.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class QianCar : ICar
2121

2222
public int ID { get; set; }
2323

24-
public QianMapPoint CurrentPoint { get; set; }
25-
public byte Direction { get; set; }
24+
public virtual QianMapPoint CurrentPoint { get; set; }
25+
public virtual byte Direction { get; set; }
2626

2727
public ushort RouteRemain { get; set; }
2828
}

MiniCarLib/QianCarAPI.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ namespace MiniCarLib
1515
public static class QianCarAPI
1616
{
1717
static QianCarController controller;
18-
static QianCarController Controller => controller;
18+
public static QianCarController Controller => controller;
1919
static bool _inited = false;
2020
public static bool IsInited => _inited;
21+
public static QianCarMap Map => controller.Map;
22+
public static CarList AllCars => controller.AllCars;
2123

2224
public static event OnCustomDataEventHandler OnCustomData;
2325
public static event OnCarRegisteredHandler OnCarRegistered;
@@ -33,7 +35,7 @@ public static class QianCarAPI
3335
private static void InitEvents()
3436
{
3537
controller.OnCustomData += OnCustomData;
36-
controller.AfterRegistered += (car, data) => OnCarRegistered?.Invoke((QianCar)car);
38+
controller.AfterRegistered += (car, data) => { if (car != null) OnCarRegistered?.Invoke((QianCar)car); };
3739
controller.AfterReportState += (car, data) => OnCarStateReported?.Invoke((QianCar)car,((ReportCarStateData)data).IsACK);
3840
controller.OnApplyForEnter += (car, data) => OnCarApplyForEnter?.Invoke(
3941
(QianCar)car,

MiniCarLib/QianCarController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ public virtual void UnregisterResponseHandler(QianCar car, UnregisterResponseDat
232232
{
233233
car.State = CarState.UnRegistered;
234234
AllCars.RemoveCar(car);
235+
Server.DisconnectClient(car.ComClient);
235236
}
236237
}
237238

@@ -281,7 +282,8 @@ public virtual void QueryDataHandler(QianCar car, ReportCarStateData data)
281282
car.CurrentPoint = Map[data.PointID];
282283
car.Direction = data.Direction;
283284
car.RouteRemain = data.RouteRemain;
284-
285+
286+
285287
}
286288

287289

MiniCarLib/QianCarData.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ public enum CarState : byte
2929
public enum CarErrorState : byte
3030
{
3131
None = 0,
32-
ComError = 1,
32+
ComDataError = 1,
3333
RunError = 2,
34-
ServerError = 3
3534
}
3635

3736
public enum DataFunctionType : byte

MiniCarLib/QianCarServer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,10 @@ protected virtual void OnRegDataReceived(IComClient client, byte[] data)
114114

115115
OnCarDataReceived?.Invoke(client, header, dataobj);
116116
}
117+
118+
public void DisconnectClient(IComClient client)
119+
{
120+
client.Disconnect();
121+
}
117122
}
118123
}

MiniCarLibTester/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Threading.Tasks;
55
using System.Windows.Forms;
66

7+
using MiniCarLib;
8+
79
namespace MiniCarLibTester
810
{
911
static class Program
@@ -14,11 +16,16 @@ static class Program
1416
[STAThread]
1517
static void Main()
1618
{
19+
20+
21+
1722
Application.EnableVisualStyles();
1823
Application.SetCompatibleTextRenderingDefault(false);
1924
Application.Run(new Form1());
2025

2126

2227
}
28+
29+
2330
}
2431
}

0 commit comments

Comments
 (0)