protected override async Task OnClientDataReceived(IPeerProxy client, byte[] data) { Stream stream = new MemoryStream(data); PeerPacketHeader packetHeader = stream.DeserializePeerPacketHeader(); switch ((PacketType)packetHeader.Type) { case PacketType.System: { HandleSystemPacket(client, stream); } break; case PacketType.Post: { HandlePostPacket(client, stream); } break; case PacketType.Vote: { HandleVotePacket(stream); } break; } }
public Task WriteTo <T>(IPeerProxy peer, T data) { if (!ConnectedPeersProxy.Contains(peer)) { throw new ArgumentException($"Unknown peerClient: {peer}"); } RawPacket rawPacket = new RawPacket(); rawPacket.Payload = PreparePeerData(peer, data); rawPacket.MAC = Hasher.ComputeHash(rawPacket.Payload); byte[] serializedPacket = rawPacket.SerializeToBuffer(); return(peer.SendAsync(serializedPacket)); }
protected override async Task OnClientDataReceived(IPeerProxy client, byte[] data, int bytesRead) { PeerPacketHeader packetHeader = data.DeserializePeerPacketHeader(); switch ((MessageType)packetHeader.Type) { case MessageType.SYSTEM: { } break; case MessageType.MSG: { } break; case MessageType.VOTE: { PeerPacket <Vote> votePacket = data.DeserializePeerPacket <Vote>(); Vote vote = votePacket.Payload; switch (vote.Ogranization) { case OrganizationType.Union: break; case OrganizationType.Company: break; case OrganizationType.Village: break; case OrganizationType.City: break; case OrganizationType.Government: break; case OrganizationType.Country: break; case OrganizationType.Anonymous: break; default: throw new ArgumentOutOfRangeException(); } } break; } }
protected override byte[] PreparePeerData <T>(IPeerProxy peer, T data) { byte[] result = null; var packetTypeAttribute = (PacketTypeAttribute)data.GetType() .GetCustomAttributes(typeof(PacketTypeAttribute), true) .FirstOrDefault(); if (packetTypeAttribute != null) { PeerPacket <T> votePacket = new PeerPacket <T>(); votePacket.Header.Type = (ulong)packetTypeAttribute.PacketType; votePacket.Header.FromPoint = PeerId; votePacket.Header.ToPoint = peer.PeerId; votePacket.Payload = data; result = votePacket.SerializePeerPacket(); } return(result); }
private void HandleSystemPacket(IPeerProxy client, Stream data) { PeerPacket <Networking.Messages.System> systemPacket = data.DeserializePeerPacket <Networking.Messages.System>(); Networking.Messages.System systemInfo = systemPacket.Payload; SystemType systemEvent = systemInfo.SystemEvent; switch (systemEvent) { case SystemType.CREATE_NEW_BLOCK: break; case SystemType.FIND_BLOCK: break; case SystemType.READ_BLOCK: break; } }
protected async Task OnDataReceived(IPeerProxy peer, byte[] data) { var readMessage = ReadPeersProxy[peer]; readMessage.AddRange(new List <byte>(data)); try { RawPacket rawPacket = readMessage.DeserializeRawPacket(); if (IsMacEquals(Hasher.ComputeHash(rawPacket.Payload), rawPacket.MAC)) { Console.WriteLine("RawPacket is valid. Let's process it ..."); await OnClientDataReceived(peer, rawPacket.Payload); } readMessage.Clear(); } catch (SerializationException ex) { Console.WriteLine($"SerializationException ex: {ex}"); } }
private void HandlePostPacket(IPeerProxy client, Stream data) { PeerPacket <Post> postPacket = data.DeserializePeerPacket <Post>(); _nextBlockToCreate.Add(PacketType.Post, postPacket.Payload); }
// Used to retrieve a neighbor given the proxy. // Maps the proxy from the incoming message's service context to a neighbor instance. public IPeerNeighbor GetNeighborFromProxy(IPeerProxy proxy) { PeerNeighbor neighbor = null; lock (ThisLock) { if (state == State.Opened) { // Find the neighbor containing the specified proxy. foreach (PeerNeighbor nbr in this.neighborList) { if (nbr.Proxy == proxy) { neighbor = nbr; break; } } } } return neighbor; }
// Open an accepting (incoming) neighbor. callbackInstance is where msgs meant for // remote endpoint should be sent. public void Open(IPeerProxy callbackInstance) { this.initiator = false; this.Proxy = callbackInstance; }
public IpcRequestMessageContext(IpcBufferMessage ipcBufferMessage, IPeerProxy peer) { IpcBufferMessage = ipcBufferMessage; Peer = peer; }