123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using athosRT.tool;
- using athosRT.tool.Server;
- using SuperSocket.SocketBase;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using EV = athosRT.tool.EV;
- using SuperSocket.Common;
- namespace athosRT.Devices.EFEM
- {
- public class SocketServer
- {
- private RAppServer _server = (RAppServer)null;
- private static object _lockerSession = new object();
- private RAppSession _session = (RAppSession)null;
- private string _endMark;
- private IServerCallback _callback;
- private int _port;
- private bool _isFirstTimeReadyInfoMessage = true;
- public SocketServer(IServerCallback callback, int port, string endMark = ";\r")
- {
- this._endMark = endMark;
- this._callback = callback;
- this._port = port;
- this._server = new RAppServer(Encoding.ASCII, string.Empty, this._endMark);
- this._server.NewSessionConnected += new SessionHandler<RAppSession>(this.OnNewSessionConnected);
- this._server.SessionClosed += new SessionHandler<RAppSession, CloseReason>(this.OnSessionClosed);
- this._server.NewRequestReceived += new RequestHandler<RAppSession, RRequestInfo>(this.OnReceive);
- }
- public void Send(string msg)
- {
- if (this._session != null)
- {
- if (!msg.StartsWith("INFO:READY") || msg.StartsWith("INFO:READY") && this._isFirstTimeReadyInfoMessage)
- {
- EV.PostInfoLog("Server", string.Format("[Send] {0};", (object)msg));
- this._isFirstTimeReadyInfoMessage = false;
- }
- lock (SocketServer._lockerSession)
- this._session.Send(string.Format("{0};\r", (object)msg));
- }
- else
- LogObject.Info("SocketServer", "Session is null, can not send out"+ msg);
- }
- private void OnNewSessionConnected(RAppSession session)
- {
- EV.PostInfoLog("Server", string.Format("Client {0} connected", (object)session.SessionID));
- lock (SocketServer._lockerSession)
- {
- if (this._session != null)
- {
- EV.PostWarningLog("Server", string.Format("New connection in, previous connection {0} removed", (object)this._session.SessionID));
- this._session.Close();
- }
- this._session = session;
- }
- this._callback.OnConnected(this._session.SessionID);
- }
- private void OnSessionClosed(RAppSession session, CloseReason reason)
- {
- EV.PostInfoLog("Server", string.Format("Client {0} Disconnected, {1}", (object)session.SessionID, (object)reason));
- lock (SocketServer._lockerSession)
- {
- if (this._session != null && this._session.SessionID == session.SessionID)
- this._session = (RAppSession)null;
- }
- this._callback.OnDisconnected(session.SessionID);
- }
- private void OnReceive(RAppSession session, RRequestInfo requestInfo)
- {
- string bodyString = requestInfo.Body.BodyString;
- EV.PostInfoLog("Server", string.Format("[Recv] {0};", (object)bodyString));
- string str = bodyString;
- char[] separator = new char[2] { ';', '\r' };
- foreach (string msg in str.Split(separator, StringSplitOptions.RemoveEmptyEntries))
- {
- this._callback.OnReceived(msg);
- Thread.Sleep(50);//原来就有
- }
- }
- public bool Start(string server, int port)
- {
- this._server.Stop();
- if (this._server.State == SuperSocket.SocketBase.ServerState.NotInitialized && !this._server.Setup(server, port))
- {
- EV.PostAlarmLog("Server", string.Format("Can not create server {0}:{1}", (object)server, (object)port));
- return false;
- }
- if (!this._server.Start())
- {
- EV.PostAlarmLog("Server", string.Format("Can not listen on {0}:{1}", (object)server, (object)port));
- return false;
- }
- //EV.PostInfoLog("Server", string.Format("Listen on {0}:{1}", (object)server, (object)port));
- return true;
- }
- public void Stop()
- {
- if (this._session != null)
- {
- lock (SocketServer._lockerSession)
- this._session.Close();
- }
- this._server.Stop();
- EV.PostInfoLog("EFEM", string.Format("Stopped Listen on {0}", (object)this._port));
- }
- }
- }
|