123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
-
- using EVCB_OCPP.Domain;
- using SuperSocket.Common;
- using SuperWebSocket;
- using SuperWebSocket.SubProtocol;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Security;
- using System.Security.Cryptography.X509Certificates;
- using System.Text;
- namespace OCPPServer.Protocol
- {
- public class OCPPWSServer : WebSocketServer<ClientData>
- {
- /// <summary>
- /// 可允許連線Clinet數
- /// </summary>
- public int connectNum { get; set; }
- /// <summary>
- /// 是否限制連線Clinet數
- /// </summary>
- public bool beConnectLimit { get; set; }
- /// <summary>
- /// Initializes a new instance of the <see cref="WebSocketServer"/> class.
- /// </summary>
- /// <param name="subProtocols">The sub protocols.</param>
- public OCPPWSServer(IEnumerable<ISubProtocol<ClientData>> subProtocols)
- : base(subProtocols)
- {
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="WebSocketServer"/> class.
- /// </summary>
- /// <param name="subProtocol">The sub protocol.</param>
- public OCPPWSServer(ISubProtocol<ClientData> subProtocol)
- : base(subProtocol)
- {
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="WebSocketServer"/> class.
- /// </summary>
- public OCPPWSServer()
- : base(new List<ISubProtocol<ClientData>>())
- {
- }
- protected override bool ValidateClientCertificate(ClientData session, object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
- {
- Console.WriteLine(string.Format("{0} :{1}", session.ChargeBoxId + " ValidateClientCertificate", sslPolicyErrors));
- return true;
- // return base.ValidateClientCertificate(session, sender, certificate, chain, sslPolicyErrors);
- }
- protected override bool ValidateHandshake(ClientData session, string origin)
- {
- string[] words = session.Path.Split('/');
- session.ChargeBoxId = words.Last();
- Console.WriteLine(string.Format("{0} :ValidateHandshake: {1}", DateTime.Now.ToString("yy/MM/dd HH:mm:ss.fff"), session.Path));
- bool isExistedSN = false;
- using (var db = new MainDBContext())
- {
- var machine = db.Machine.Where(x => x.ChargeBoxId == session.ChargeBoxId).FirstOrDefault();
-
- isExistedSN = machine == null ? false : true;
- }
- if (!isExistedSN)
- {
- StringBuilder responseBuilder = new StringBuilder();
- responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}","1.1",
- (int)HttpStatusCode.NotFound, @"Not Found");
- responseBuilder.AppendWithCrCf();
- string sb = responseBuilder.ToString();
- byte[] data = Encoding.UTF8.GetBytes(sb);
-
- ((IWebSocketSession)session).SendRawData(data,0,data.Length);
- return false;
- }
- return true;
- }
- }
- }
|