public static byte[] Serialize(PieceState state) { string raw = state.Id + ";" + state.Color.ToString() + ";" + state.NewPosition.X + ";" + state.NewPosition.Y; byte[] bytes = Encoding.UTF8.GetBytes(raw); return bytes; }
public void SendChange(PieceState state) { if (_socketState == SocketState.Client) { _socket.Send(Serializer.Serialize(state)); } else { _other.Send(Serializer.Serialize(state)); } Console.WriteLine("Sent data"); }
public PieceState?PollState() { if (_buffer == null || _buffer.Length < 1) { return(null); } Console.WriteLine("Received data"); PieceState state = Serializer.DeSerialize(_buffer); _buffer = null; return(state); }
public static PieceState DeSerialize(byte[] raw) { string stateString = Encoding.UTF8.GetString(raw); int indexFirst = stateString.IndexOf(';') + 1; int indexSecond = stateString.IndexOf(';', indexFirst) + 1; int indexThird = stateString.IndexOf(';', indexSecond) + 1; float x = int.Parse(stateString.Substring(indexSecond, 1)); float y = int.Parse(stateString.Substring(indexThird, 1)); PieceState state = new PieceState(int.Parse(stateString.Substring(0, stateString.IndexOf(';'))), Enum.Parse<GameColor>(stateString.Substring(indexFirst, stateString.Length - stateString.IndexOf(';', indexFirst) + 1)), new Vector2(x, y)); return state; }