Пример #1
0
        public bool move(string from, string to)
        {
            Packet pack = new Packet();
            pack.protocol = defines.mProtocol;
            pack.type = MessageType.Move;
            pack.gameId = m_gameID;
            pack.packetID = m_index;
            pack.from = from;
            pack.to = to;
            sendPacket(pack);
            pack = new Packet();
            pack = receivePacket();

            if (pack.gameId != m_gameID || pack.type != MessageType.ACK || pack.packetID != m_index)
                return false;
            m_index++;
            pack = receivePacket();

            if (pack.gameId == m_gameID && pack.type == MessageType.MoveAccepted)
            {
                m_index++;
                return true;
            }
            else
                return false;
        }
Пример #2
0
        public static void Serialize(Stream stream, Packet pack)
        {
            BinaryWriter wr = new BinaryWriter(stream);

            wr.Write(pack.protocol);
            wr.Write((byte)pack.type);
            wr.Write(pack.gameId);
            wr.Write(pack.packetID);
            wr.Write(pack.from);
            wr.Write(pack.to);

        }
Пример #3
0
        public static Packet Deserialize(Stream stream)
        {
            Packet pack = new Packet();
            BinaryReader br = new BinaryReader(stream);

            pack.protocol = br.ReadByte();
            pack.type = (MessageType)br.ReadByte();
            pack.gameId = br.ReadString();
            pack.packetID = br.ReadInt32();
            pack.from = br.ReadString();
            pack.to = br.ReadString();
            return pack;
        }
Пример #4
0
 public void initiateGame()
 {
     Packet pack = new Packet();
     pack.protocol = defines.mProtocol;
     pack.type = MessageType.Start;
     pack.gameId = "";
     pack.packetID = m_index++;
     pack.from = "";
     pack.to = "";
     sendPacket(pack);
     pack = receivePacket();
     m_gameID = pack.gameId;
 }
Пример #5
0
 private void sendPacket(Packet pack)
 {
     PacketEncoder.Serialize(m_client.GetStream(), pack);
 }
Пример #6
0
        static void Main(string[] args)
        {
            cChessGame test = new cChessGame();


            TcpListener listen = new TcpListener(new IPAddress(new byte[] { 0, 0, 0, 0 }), 8080);
            listen.Start();
            List<TcpClient> clients = new List<TcpClient>();
            Console.WriteLine(" >> Server Started");
            clients.Add(listen.AcceptTcpClient());
            Console.WriteLine(" >> Connection accepted");

            Packet packIncoming;
            Packet packOutgoing = new Packet();

            NetworkStream ns = clients[0].GetStream();
            packIncoming = PacketEncoder.Deserialize(ns);
            if (packIncoming.type == MessageType.Start)
            {
                packOutgoing.protocol = defines.mProtocol;
                packOutgoing.type = MessageType.ACK;
                packOutgoing.gameId = "123aaf15";
                packOutgoing.packetID = packIncoming.packetID;
                packOutgoing.from = "";
                packOutgoing.to = "";
            }
            PacketEncoder.Serialize(ns, packOutgoing);

            packIncoming = PacketEncoder.Deserialize(ns);
            bool testbool = false;

            if (packIncoming.type == MessageType.Move && packIncoming.gameId == "123sghkj15")
            {
                cMove move = new cMove();
                move.from = packIncoming.from;
                move.to = packIncoming.to;
                testbool = test.move(move);
            }
            packOutgoing.protocol = defines.mProtocol;
            packOutgoing.type = MessageType.ACK;
            packOutgoing.gameId = "123sghkj15";
            packOutgoing.packetID = packIncoming.packetID;
            packOutgoing.from = "";
            packOutgoing.to = "";
            PacketEncoder.Serialize(ns, packOutgoing);

            if (testbool)
            {
                packOutgoing.protocol = defines.mProtocol;
                packOutgoing.type = MessageType.MoveAccepted;
                packOutgoing.gameId = "123sghkj15";
                packOutgoing.packetID = packIncoming.packetID + 1;
                packOutgoing.from = "";
                packOutgoing.to = "";
                PacketEncoder.Serialize(ns, packOutgoing);
            }
            else
            {
                packOutgoing.protocol = defines.mProtocol;
                packOutgoing.type = MessageType.MoveDenied;
                packOutgoing.gameId = "123sghkj15";
                packOutgoing.packetID = packIncoming.packetID + 1;
                packOutgoing.from = "";
                packOutgoing.to = "";
                PacketEncoder.Serialize(ns, packOutgoing);
            }
            /* NetworkStream ns = new NetworkStream();
             byte[] bytes = PacketEncoder.Serialize(pack);
             Packet pack2 = PacketEncoder.Deserialize(bytes);*/

        }