private static async Task SendResponse(NetworkStream stream) { var packet = new PeerPacket <Vote>(); Vote vote = new Vote() { Ogranization = OrganizationType.Country, VoteType = (int)CountryVoteType.President }; packet.Payload = vote; await stream.CopyToAsync(packet.SerializePeerPacket()); await stream.FlushAsync(); }
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; } }
private void HandlePostPacket(IPeerProxy client, Stream data) { PeerPacket <Post> postPacket = data.DeserializePeerPacket <Post>(); _nextBlockToCreate.Add(PacketType.Post, postPacket.Payload); }