public async Task Invoke(HttpContext context) { ProtocolId protocolId = ProtocolId.None; try { var httpRequest = context.Request; var httpResponse = context.Response; XXTea xxtea = null; ProtocolReq req = null; byte encrypted; uint hash; int length = (int)httpRequest.ContentLength; byte[] inBuffer = new byte[length]; using (var inStream = httpRequest.Body) using (var stream = new MemoryStream(inBuffer)) using (var reader = new BinaryReader(stream)) { await inStream.CopyToAsync(stream); stream.Position = 0; encrypted = reader.ReadByte(); hash = reader.ReadUInt32(); if (encrypted == 2) { xxtea = new XXTea(new uint[] { hash ^ ServerConfig.Key1, hash ^ ServerConfig.Key2, hash ^ ServerConfig.Key3, hash ^ ServerConfig.Key4 }); } else if (encrypted == 1) { xxtea = new XXTea(new uint[] { ServerConfig.Key1, ServerConfig.Key2, ServerConfig.Key3, ServerConfig.Key4 }); req = DecryptAndDeserialize(xxtea, inBuffer, 5, length - 5); protocolId = req.Protocol.ProtocolId; if (protocolId == ProtocolId.Auth) { } else { throw new Exception("encrypted == 1 is allowed to only Protocol Auth"); } } else if (encrypted == 0) { req = Serializer.Deserialize <ProtocolReq>(stream); protocolId = req.Protocol.ProtocolId; if (protocolId == ProtocolId.HandShake) { } else { throw new Exception("encrypted == 0 is allowed to Protocol HandShake"); } } else { throw new Exception("Encrypted : Unavaiable Value"); } } byte[] data = null; ProtocolRes response = await Service.ProcessAsync(context, req.Suid, hash, req.Protocol); if (response.ProtocolId == ProtocolId.Error) { encrypted = 0; xxtea = null; } using (var stream = new MemoryStream()) using (var writer = new BinaryWriter(stream)) { writer.Write(encrypted); if (xxtea != null) { writer.Write(SerializeAndEncrypt(xxtea, response)); } else { Serializer.Serialize(stream, response); } data = stream.ToArray(); httpResponse.ContentLength = data.Length; using (Stream outStream = httpResponse.Body) { await outStream.WriteAsync(data, 0, data.Length); } } } catch (Exception ex) { } }
/// <summary> /// Checks all <c>Protocol</c>s are unique and that /// each required up/down service is provided /// </summary> /// <param name="protocols">Protocols that will be used for the stack</param> private void sanityCheck(Protocol[] protocols) { Protocol prot; String name; ProtocolReq req; ArrayList req_list = new ArrayList(); int evt_type; // Checks for unique names for (int i = 0; i < protocols.Length; i++) { prot = (Protocol)protocols[i]; name = prot.Name; for (int j = 0; j < protocols.Length; j++) { if (i == j) { continue; } if (String.Compare(name, protocols[j].Name) == 0) { throw new Exception("Configurator.sanityCheck(): protocol name " + name + " has been used more than once; protocol names have to be unique !"); } } } // Checks whether all requirements of all layers are met for (int i = 0; i < protocols.Length; i++) { prot = (Protocol)protocols[i]; req = new ProtocolReq(prot.Name); req.up_reqs = prot.requiredUpServices(); req.down_reqs = prot.requiredDownServices(); req.up_provides = prot.providedUpServices(); req.down_provides = prot.providedDownServices(); req_list.Add(req); } for (int i = 0; i < req_list.Count; i++) { req = (ProtocolReq)req_list[i]; // check whether layers above this one provide corresponding down services if (req.up_reqs != null) { for (int j = 0; j < req.up_reqs.Count; j++) { evt_type = ((int)req.up_reqs[j]); if (!providesDownServices(i, req_list, evt_type)) { throw new Exception("Configurator.sanityCheck(): event " + Event.type2String(evt_type) + " is required by " + req.name + ", but not provided by any of the layers above"); } } } // check whether layers below this one provide corresponding up services if (req.down_reqs != null) { // check whether layers above this one provide up_reqs for (int j = 0; j < req.down_reqs.Count; j++) { evt_type = ((int)req.down_reqs[j]); if (!providesUpServices(i, req_list, evt_type)) { throw new Exception("Configurator.sanityCheck(): event " + Event.type2String(evt_type) + " is required by " + req.name + ", but not provided by any of the layers below"); } } } } }
/// <summary> /// Checks all <c>Protocol</c>s are unique and that /// each required up/down service is provided /// </summary> /// <param name="protocols">Protocols that will be used for the stack</param> private void sanityCheck(Protocol[] protocols) { Protocol prot; String name; ProtocolReq req; ArrayList req_list=new ArrayList(); int evt_type; // Checks for unique names for(int i=0; i < protocols.Length; i++) { prot = (Protocol)protocols[i]; name = prot.Name; for(int j=0; j < protocols.Length; j++) { if(i==j) continue; if(String.Compare(name,protocols[j].Name)==0) { throw new Exception("Configurator.sanityCheck(): protocol name " + name + " has been used more than once; protocol names have to be unique !"); } } } // Checks whether all requirements of all layers are met for(int i=0; i < protocols.Length; i++) { prot=(Protocol)protocols[i]; req=new ProtocolReq(prot.Name); req.up_reqs=prot.requiredUpServices(); req.down_reqs=prot.requiredDownServices(); req.up_provides=prot.providedUpServices(); req.down_provides=prot.providedDownServices(); req_list.Add(req); } for(int i=0; i < req_list.Count; i++) { req=(ProtocolReq)req_list[i]; // check whether layers above this one provide corresponding down services if(req.up_reqs != null) { for(int j=0; j < req.up_reqs.Count; j++) { evt_type=((int)req.up_reqs[j]); if(!providesDownServices(i, req_list, evt_type)) { throw new Exception("Configurator.sanityCheck(): event " + Event.type2String(evt_type) + " is required by " + req.name + ", but not provided by any of the layers above"); } } } // check whether layers below this one provide corresponding up services if(req.down_reqs != null) { // check whether layers above this one provide up_reqs for(int j=0; j < req.down_reqs.Count; j++) { evt_type=((int)req.down_reqs[j]); if(!providesUpServices(i, req_list, evt_type)) { throw new Exception("Configurator.sanityCheck(): event " + Event.type2String(evt_type) + " is required by " + req.name + ", but not provided by any of the layers below"); } } } } }
/// <summary>Throws an exception if sanity check fails. Possible sanity check is uniqueness of all protocol /// names. /// </summary> public virtual void sanityCheck(System.Collections.ArrayList protocols) { System.Collections.ArrayList names = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); Protocol prot; string name; ProtocolReq req; System.Collections.ArrayList req_list = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); int evt_type; // Checks for unique names for (int i = 0; i < protocols.Count; i++) { prot = (Protocol) protocols[i]; name = prot.Name; for (int j = 0; j < names.Count; j++) { if (name.Equals(names[j])) { throw new System.Exception("Configurator.sanityCheck(): protocol name " + name + " has been used more than once; protocol names have to be unique !"); } } names.Add(name); } // Checks whether all requirements of all layers are met for (int i = 0; i < protocols.Count; i++) { prot = (Protocol) protocols[i]; req = new ProtocolReq(prot.Name); req.up_reqs = prot.requiredUpServices(); req.down_reqs = prot.requiredDownServices(); req.up_provides = prot.providedUpServices(); req.down_provides = prot.providedDownServices(); req_list.Add(req); } for (int i = 0; i < req_list.Count; i++) { req = (ProtocolReq) req_list[i]; // check whether layers above this one provide corresponding down services if (req.up_reqs != null) { for (int j = 0; j < req.up_reqs.Count; j++) { evt_type = ((System.Int32) req.up_reqs[j]); if (!providesDownServices(i, req_list, evt_type)) { throw new System.Exception("Configurator.sanityCheck(): event " + Event.type2String(evt_type) + " is required by " + req.name + ", but not provided by any of the layers above"); } } } // check whether layers below this one provide corresponding up services if (req.down_reqs != null) { // check whether layers above this one provide up_reqs for (int j = 0; j < req.down_reqs.Count; j++) { evt_type = ((System.Int32) req.down_reqs[j]); if (!providesUpServices(i, req_list, evt_type)) { throw new System.Exception("Configurator.sanityCheck(): event " + Event.type2String(evt_type) + " is required by " + req.name + ", but not provided by any of the layers below"); } } } } }