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 }); } } }
public void Send(byte[] bytes, int len) { if (!encComplete) throw new IOException("Key exchange is not yet completed"); if (encType != EncryptionType.None) { byte[] outbytes = new byte[len]; Encryptor.TransformBlock(bytes, 0, len, outbytes, 0); bytes = outbytes; //Console.Write("Encryptor IV: "); LogBytes(encryptor.Key, encryptor.IV.length); } #if DEBUG Console.Write(ID + " Sent: "); LogBytes(bytes, len); #endif try { sock.Send(bytes, len, SocketFlags.None); } catch (Exception e) { closeException = e; closeReason = "Exception in send: " + e.Message; Close(); } }
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(); }