SocketServer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using athosRT.tool;
  2. using athosRT.tool.Server;
  3. using SuperSocket.SocketBase;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using EV = athosRT.tool.EV;
  11. using SuperSocket.Common;
  12. namespace athosRT.Devices.EFEM
  13. {
  14. public class SocketServer
  15. {
  16. private RAppServer _server = (RAppServer)null;
  17. private static object _lockerSession = new object();
  18. private RAppSession _session = (RAppSession)null;
  19. private string _endMark;
  20. private IServerCallback _callback;
  21. private int _port;
  22. private bool _isFirstTimeReadyInfoMessage = true;
  23. public SocketServer(IServerCallback callback, int port, string endMark = ";\r")
  24. {
  25. this._endMark = endMark;
  26. this._callback = callback;
  27. this._port = port;
  28. this._server = new RAppServer(Encoding.ASCII, string.Empty, this._endMark);
  29. this._server.NewSessionConnected += new SessionHandler<RAppSession>(this.OnNewSessionConnected);
  30. this._server.SessionClosed += new SessionHandler<RAppSession, CloseReason>(this.OnSessionClosed);
  31. this._server.NewRequestReceived += new RequestHandler<RAppSession, RRequestInfo>(this.OnReceive);
  32. }
  33. public void Send(string msg)
  34. {
  35. if (this._session != null)
  36. {
  37. if (!msg.StartsWith("INFO:READY") || msg.StartsWith("INFO:READY") && this._isFirstTimeReadyInfoMessage)
  38. {
  39. EV.PostInfoLog("Server", string.Format("[Send] {0};", (object)msg));
  40. this._isFirstTimeReadyInfoMessage = false;
  41. }
  42. lock (SocketServer._lockerSession)
  43. this._session.Send(string.Format("{0};\r", (object)msg));
  44. }
  45. else
  46. LogObject.Info("SocketServer", "Session is null, can not send out"+ msg);
  47. }
  48. private void OnNewSessionConnected(RAppSession session)
  49. {
  50. EV.PostInfoLog("Server", string.Format("Client {0} connected", (object)session.SessionID));
  51. lock (SocketServer._lockerSession)
  52. {
  53. if (this._session != null)
  54. {
  55. EV.PostWarningLog("Server", string.Format("New connection in, previous connection {0} removed", (object)this._session.SessionID));
  56. this._session.Close();
  57. }
  58. this._session = session;
  59. }
  60. this._callback.OnConnected(this._session.SessionID);
  61. }
  62. private void OnSessionClosed(RAppSession session, CloseReason reason)
  63. {
  64. EV.PostInfoLog("Server", string.Format("Client {0} Disconnected, {1}", (object)session.SessionID, (object)reason));
  65. lock (SocketServer._lockerSession)
  66. {
  67. if (this._session != null && this._session.SessionID == session.SessionID)
  68. this._session = (RAppSession)null;
  69. }
  70. this._callback.OnDisconnected(session.SessionID);
  71. }
  72. private void OnReceive(RAppSession session, RRequestInfo requestInfo)
  73. {
  74. string bodyString = requestInfo.Body.BodyString;
  75. EV.PostInfoLog("Server", string.Format("[Recv] {0};", (object)bodyString));
  76. string str = bodyString;
  77. char[] separator = new char[2] { ';', '\r' };
  78. foreach (string msg in str.Split(separator, StringSplitOptions.RemoveEmptyEntries))
  79. {
  80. this._callback.OnReceived(msg);
  81. Thread.Sleep(50);//原来就有
  82. }
  83. }
  84. public bool Start(string server, int port)
  85. {
  86. this._server.Stop();
  87. if (this._server.State == SuperSocket.SocketBase.ServerState.NotInitialized && !this._server.Setup(server, port))
  88. {
  89. EV.PostAlarmLog("Server", string.Format("Can not create server {0}:{1}", (object)server, (object)port));
  90. return false;
  91. }
  92. if (!this._server.Start())
  93. {
  94. EV.PostAlarmLog("Server", string.Format("Can not listen on {0}:{1}", (object)server, (object)port));
  95. return false;
  96. }
  97. //EV.PostInfoLog("Server", string.Format("Listen on {0}:{1}", (object)server, (object)port));
  98. return true;
  99. }
  100. public void Stop()
  101. {
  102. if (this._session != null)
  103. {
  104. lock (SocketServer._lockerSession)
  105. this._session.Close();
  106. }
  107. this._server.Stop();
  108. EV.PostInfoLog("EFEM", string.Format("Stopped Listen on {0}", (object)this._port));
  109. }
  110. }
  111. }