Esempio n. 1
0
        private byte[] Serialize_ServerAdvertise_Message_PDU(ServerAdvertise_Message_PDU Message)
        {                                                    // Convert an Message_PDU structure into a byte array
            byte[] bByteArray = new byte[m_iSA_MessageSize]; // Create a byte array to hold the serialised form of the message PDU

            int iFieldOffset = 0;                            // Create an offset value which ensures the byte array is constructed with correct field positions and separations

            byte[] bWorkingArray = BitConverter.GetBytes((Int32)Message.eMessageType);
            Buffer.BlockCopy(bWorkingArray /*src*/, 0 /*source offset*/, bByteArray /*dest*/, iFieldOffset /*dest offset*/, m_iSA_FieldSize1 /*count*/);

            iFieldOffset += m_iSA_FieldSize1;               // Adjust offset for second field
            int iDataLength2 = Message.cServerName.Length;  // Get the length of data actually stored in field2 (variable length data, fixed size field)

            Buffer.BlockCopy(Message.cServerName /*src*/, 0 /*source offset*/, bByteArray /*dest*/, iFieldOffset /*dest offset*/, iDataLength2 /*count*/);

            iFieldOffset            += m_iSA_FieldSize2;       // Adjust offset for third field
            bByteArray[iFieldOffset] = Message.iIP_Address_Byte_1;
            iFieldOffset            += m_iSA_FieldSize3;       // Adjust offset for fourth field
            bByteArray[iFieldOffset] = Message.iIP_Address_Byte_2;
            iFieldOffset            += m_iSA_FieldSize4;       // Adjust offset for fifth field
            bByteArray[iFieldOffset] = Message.iIP_Address_Byte_3;
            iFieldOffset            += m_iSA_FieldSize5;       // Adjust offset for sixth field
            bByteArray[iFieldOffset] = Message.iIP_Address_Byte_4;

            iFieldOffset += m_iSA_FieldSize6;                  // Adjust offset for seventh field


            iFieldOffset++;                                           // Skip one byte to start port encoding on 4-byte boundary

            bByteArray[iFieldOffset++] = (byte)(Message.iPort / 256); // Create the MSB byte of the port value
            bByteArray[iFieldOffset++] = (byte)(Message.iPort % 256); // Create the LSB byte of the port value
            bByteArray[iFieldOffset++] = 0;                           // Complete the encoding of the additional bytes (so that the natively Int16 port value is encoded as Int32)
            bByteArray[iFieldOffset++] = 0;                           // Complete the encoding of the additional bytes (so that the natively Int16 port value is encoded as Int32)

            return(bByteArray);
        }
Esempio n. 2
0
        void Send_ServerAdvertisement_Broadcast()     // this will send an broadcast to all players which is divdeded.
        {
            ServerAdvertise_Message_PDU Message = new ServerAdvertise_Message_PDU();

            Message.eMessageType = MessageType.SERVER_ADVERTISE;
            string sServerName = TruncateString(txtServerName.Text, MAX_SERVER_NAME_LEN);

            Message.cServerName = Encoding.ASCII.GetBytes(sServerName);      // turns servername into bytes.
            byte[] bLocalAddress = m_LocalEndPoint.Address.GetAddressBytes();
            Message.iIP_Address_Byte_1 = bLocalAddress[0];                   //turns ip part1 into bytes
            Message.iIP_Address_Byte_2 = bLocalAddress[1];                   //turns ip part2 into bytes
            Message.iIP_Address_Byte_3 = bLocalAddress[2];                   //turns ip part3 into bytes
            Message.iIP_Address_Byte_4 = bLocalAddress[3];                   //turns ip part4 into bytes
            Message.iPort = iPort;                                           // gets the port.and turns it into the message that will be sent to everyone on the getaway.

            byte[] bBuffer = Serialize_ServerAdvertise_Message_PDU(Message); // this will serialize the message array so it will be easy and short to send.
            int    iBytesSent;

            try
            {
                iBytesSent = m_SendServerAdvertisementsSocket.SendTo(bBuffer, m_SendServerAdvertisementsEndPoint);
                if (0 < iBytesSent)
                {
                }
            }
            catch (SocketException ex)
            {
                MessageBox.Show("Server Advertise broadcast send failed", "Snakes And Ladders");     //show message box that the server advertise failed.
            }
        }