Skip to content

Instantly share code, notes, and snippets.

@ywjno
Last active August 29, 2015 14:08
Show Gist options
  • Save ywjno/7071fcc375002fca16f9 to your computer and use it in GitHub Desktop.
Save ywjno/7071fcc375002fca16f9 to your computer and use it in GitHub Desktop.
RemoteSQLite.cs
using System;
using System.Data;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
/*
Queen Remote SQLite Client v3.7
by:[email protected]
30.06.2014
*/
public class RemoteSQLite
{
private Socket _tcp_client = null;
private bool isSTOP = false;
public RemoteSQLite(IPEndPoint remote_ipep)
{
_tcp_client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_tcp_client.Connect(remote_ipep);
Thread heartbeat = heartbeat = new Thread(HeartBeat);
heartbeat.Start();
}
public void Close()
{
isSTOP = true;
_tcp_client.Shutdown(SocketShutdown.Both);
_tcp_client.Close();
}
public string Echo()
{
SendString("ECHO|null");
byte[] buffer = new byte[32];
int n = _tcp_client.Receive(buffer);
return Encoding.UTF8.GetString(buffer, 0, n);
}
public bool Reg(string name, string pswd)
{
SendString(string.Format("REG|{0}|{1}", name, pswd));
byte[] buffer = new byte[16];
int n = _tcp_client.Receive(buffer);
if (n <= 0) return false;
string msg = Encoding.UTF8.GetString(buffer, 0, n);
if (msg != "REG|TRUE") return false;
return true;
}
public bool Login(string name, string pswd)
{
SendString(string.Format("LOGIN|{0}|{1}", name, pswd));
byte[] buffer = new byte[16];
int n = _tcp_client.Receive(buffer);
if (n <= 0) return false;
string msg = Encoding.UTF8.GetString(buffer, 0, n);
if (msg != "LOGIN|TRUE") return false;
return true;
}
public bool Pswd(string new_pswd)
{
SendString(string.Format("PSWD|{0}", new_pswd));
byte[] buffer = new byte[16];
int n = _tcp_client.Receive(buffer);
if (n <= 0) return false;
string msg = Encoding.UTF8.GetString(buffer, 0, n);
if (msg != "PSWD|TRUE") return false;
return true;
}
public DataTable Reader(string cmd)
{
SendString(string.Format("READER|{0}", cmd));
DataTable retDT = new DataTable();
byte[] buffer = new byte[1024 * 65];
int n = _tcp_client.Receive(buffer);
if (n <= 0) return retDT;
byte[] dest = new byte[n];
Array.Copy(buffer, dest, n);
retDT = DatatableDeserialize(Decompress(dest));
return retDT;
}
public bool Writer(string cmd)
{
SendString(string.Format("WRITER|{0}", cmd));
byte[] buffer = new byte[16];
int n = _tcp_client.Receive(buffer);
if (n <= 0) return false;
string msg = Encoding.UTF8.GetString(buffer, 0, n);
if (msg != "WRITER|TRUE") return false;
return true;
}
private void SendString(string msg)
{
byte[] buffer = Encoding.UTF8.GetBytes(msg);
_tcp_client.Send(buffer);
}
private void HeartBeat()
{
while (!isSTOP)
{
SendString("HB");
byte[] buffer = new byte[32];
int n = _tcp_client.Receive(buffer);
Thread.Sleep(1000 * 60);
}
}
private DataTable DatatableDeserialize(byte[] data)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(data);
return bf.Deserialize(ms) as DataTable;
}
private byte[] Decompress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream(data);
GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress);
MemoryStream out_ms = new MemoryStream();
byte[] block = new byte[1024];
while (true)
{
int bytesRead = compressedzipStream.Read(block, 0, block.Length);
if (bytesRead <= 0) break;
else out_ms.Write(block, 0, bytesRead);
}
compressedzipStream.Close();
return out_ms.ToArray();
}
catch
{
return Encoding.UTF8.GetBytes("error decompress");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment