internal Table(TableDb tdb, ScalableDb db, string table) { tableName = Encoding.UTF8.GetBytes(table + "_"); this.db = db; name = table; this.tdb = tdb; switch (table) { case "__tables": initialized = true; break; default: break; } }
async Task Initialize(byte[] name, TableDb db, string hostname, int port, TimeSpan timeout) { this.timeout = timeout; this.name = name; nameEnd = new byte[name.Length + 1]; nameEnd[name.Length] = 1; this.db = db; entry = new byte[name.Length + 16]; Buffer.BlockCopy(name, 0, entry, 0, name.Length); Buffer.BlockCopy(id.ToByteArray(), 0, entry, name.Length, 16); var row = TableRow.From(new { Key = entry, Hostname = hostname, port = port }); row.UseLinearHash = true; mclient = new UdpClient(new IPEndPoint(IPAddress.Any, port)); Action recvLoop = async() => { while (running) { try { var packet = await mclient.ReceiveAsync(); //Receive a sink. BinaryReader mreader = new BinaryReader(new MemoryStream(packet.Buffer)); switch (mreader.ReadByte()) { case 0: //Ping request await mclient.SendAsync(new byte[] { 1 }, 1, packet.RemoteEndPoint); break; case 1: //Ping response TaskCompletionSource <bool> msrc = null; lock (pendingPings) { if (pendingPings.ContainsKey(packet.RemoteEndPoint)) { msrc = pendingPings[packet.RemoteEndPoint]; pendingPings.Remove(packet.RemoteEndPoint); } } msrc?.SetResult(true); break; case 2: //Ping endpoint request if (await Ping(new IPEndPoint(new IPAddress(mreader.ReadBytes(4)), mreader.ReadInt32()))) { byte[] me = new byte[17]; me[0] = 3; Buffer.BlockCopy(mreader.ReadBytes(16), 0, me, 1, 16); await mclient.SendAsync(me, me.Length, packet.RemoteEndPoint); } break; case 3: //Ping endpoint response msrc = null; Guid ian = new Guid(mreader.ReadBytes(16)); lock (pendingPingRequests) { if (pendingPingRequests.ContainsKey(ian)) { msrc = pendingPingRequests[ian]; pendingPingRequests.Remove(ian); } } msrc?.SetResult(true); break; case 4: //Message NtfyMessage(new ScalableMessage() { From = new Guid(mreader.ReadBytes(16)), ID = new Guid(mreader.ReadBytes(16)), Message = mreader.ReadBytes(mreader.ReadInt32()) }); break; } }catch (Exception er) { } } }; await db["__queues"].Upsert(row); }
public P2PQueue(byte[] name, TableDb db, string hostname, int port, TimeSpan timeout) { initTask = Initialize(name, db, hostname, port, timeout); }
public TransactedDb(TableDb db) { this.db = db; }