Пример #1
0
        public void SendMessage(uint code, byte[] bytes, byte paramType, int len)
        {
            if (paramType > 0)
            {
                ByteBuilder b = new ByteBuilder(3);
                b.AddParameter(bytes, paramType);
                bytes = b.Read(0, b.Length);
                len = bytes.Length;
            }

            lock (sock)
            {
                byte checksum = 0; byte[] ba;
                switch (MessageType)
                {
                    case MessageType.CodeAndLength:
                        Send(ba = UintToBytes(code));
                        for (int i = 0; i < 4; i++) checksum += ba[i];
                        Send(ba = IntToBytes(len));
                        for (int i = 0; i < 4; i++) checksum += ba[i];
                        if (encType != EncryptionType.None) Send(new byte[] { checksum });
                        break;
                    case MessageType.Length:
                        Send(ba = IntToBytes(len));
                        for (int i = 0; i < 4; i++) checksum += ba[i];
                        if (encType != EncryptionType.None) Send(new byte[] { checksum });
                        break;
                }
                Send(bytes, len);
                if (encType != EncryptionType.None)
                {
                    checksum = 0;
                    for (int i = 0; i < len; i++) checksum += bytes[i];
                    Send(new byte[] { checksum });
                }
            }

        }
Пример #2
0
 void SendResponse(ClientInfo ci, HttpRequest req, HttpResponse resp, bool close)
 {
     #if DEBUG
     Console.WriteLine("Response: "+resp.ReturnCode + Responses[resp.ReturnCode]);
     #endif
     ByteBuilder bb = new ByteBuilder();
     bb.Add(Encoding.UTF8.GetBytes("HTTP/1.1 " + resp.ReturnCode + " " + Responses[resp.ReturnCode] +
             "\r\nDate: "+DateTime.Now.ToString("R")+
             "\r\nServer: RedCoronaEmbedded/1.0"+
             "\r\nConnection: "+(close ? "close" : "Keep-Alive")));
     bb.Add(Encoding.UTF8.GetBytes("\r\nContent-Encoding: " + (resp.Encoding == null ? "utf-8" : resp.Encoding)));
     if (resp.RawContent == null)
         bb.Add(Encoding.UTF8.GetBytes("\r\nContent-Length: " + resp.Content.Length));
     else
         bb.Add(Encoding.UTF8.GetBytes("\r\nContent-Length: " + resp.RawContent.Length));
     if(resp.ContentType != null)
         bb.Add(Encoding.UTF8.GetBytes("\r\nContent-Type: "+resp.ContentType));
     if(req.Session != null) bb.Add(Encoding.UTF8.GetBytes("\r\nSet-Cookie: _sessid="+req.Session.ID+"; path=/"));
     foreach(KeyValuePair<string, string> de in resp.Header) bb.Add(Encoding.UTF8.GetBytes("\r\n" + de.Key + ": " + de.Value));
     bb.Add(Encoding.UTF8.GetBytes("\r\n\r\n")); // End of header
     if(resp.RawContent != null) bb.Add(resp.RawContent);
     else bb.Add(Encoding.UTF8.GetBytes(resp.Content));
     ci.Send(bb.Read(0, bb.Length));
     #if DEBUG
     Console.WriteLine("** SENDING\n"+resp.Content);
     #endif
     if(close) ci.Close();
 }