Example #1
0
        /// <summary>
        /// Sends a GameMessage instance to the connected server.
        /// </summary>
        /// <param name="Message">The GameMessage instance to be sent</param>
        /// <param name="Flush">Flush TCP buffer or not</param>
        protected void Send(GameMessage Message, bool Flush = true)
        {
            // use MessageController to sign the message with valid CRC and SS
            messageController.SignMessage(Message);

            // serialize message to bytes
            byte[] MessageBytes = Message.Bytes;

            // write the message bytes to stream
            tcpStream.Write(MessageBytes, 0, MessageBytes.Length);

            // only flush if we should do so
            if (Flush)
            {
                tcpStream.Flush();
            }

            // track sending of UseCharacter message
            if ((MessageTypeGameMode)Message.PI == MessageTypeGameMode.UseCharacter)
            {
                // set state
                connectionState = ConnectionState.Playing;

                // log
                Logger.Log(MODULENAME, LogType.Info, "ConnectionState: Playing");
            }

            // loop back game message for logging purposes
            if (IsOutgoingPacketLogEnabled)
            {
                OutgoingPacketLog.Enqueue(Message);
            }
        }
        /// <summary>
        /// Sends a GameMessage instance to the connected server.
        /// </summary>
        /// <param name="Message">The GameMessage instance to be sent</param>
        /// <param name="Flush">Flush TCP buffer or not</param>
        protected void Send(GameMessage Message, bool Flush = true)
        {
            // surpress accidentially sent messages
            // after BP_REQ_QUIT and while we're waiting for BP_QUIT (server won't understand)
            if (isQuitting)
            {
                return;
            }

            // enable UDP for MeridianNext
#if !VANILLA && !OPENMERIDIAN
            const bool USE_UDP = true;
#else
            const bool USE_UDP = false;
#endif

            // send some by UDP
            if (USE_UDP && Message is GameModeMessage && (
                    Message.PI == (byte)MessageTypeGameMode.ReqMove ||
                    Message.PI == (byte)MessageTypeGameMode.ReqTurn ||
                    Message.PI == (byte)MessageTypeGameMode.ReqAttack))
            {
                // create UDP header (don't care about existing)
                Message.Header = new MessageHeader.Udp();

                // use MessageController to sign the message with valid CRC and SS
                messageController.SignMessage(Message);

                // serialize message to bytes
                byte[] MessageBytes = Message.Bytes;

                // write the message bytes to stream
                socketUDP.SendTo(MessageBytes, socket.RemoteEndPoint);
            }
            else
            {
                // create TCP header (don't care about existing)
                Message.Header = new MessageHeader.Tcp();

                // use MessageController to sign the message with valid CRC and SS
                messageController.SignMessage(Message);

                // serialize message to bytes
                byte[] MessageBytes = Message.Bytes;

                // write the message bytes to stream
                tcpStream.Write(MessageBytes, 0, MessageBytes.Length);

                // only flush if we should do so
                if (Flush)
                {
                    tcpStream.Flush();
                }
            }

            // track sending of UseCharacter message
            if (Message is UseCharacterMessage)
            {
                // set state
                connectionState = ConnectionState.Playing;

                // log
                Logger.Log(MODULENAME, LogType.Info, "ConnectionState: Playing");
            }

            else if (Message is ReqQuitMessage)
            {
                // flip quitting. no more message can be sent
                // until flipped back to false on BP_QUIT
                isQuitting = true;
            }

            // loop back game message for logging purposes
            if (IsOutgoingPacketLogEnabled)
            {
                OutgoingPacketLog.Enqueue(Message);
            }
        }
        /// <summary>
        /// Sends a GameMessage instance to the connected server.
        /// </summary>
        /// <param name="Message">The GameMessage instance to be sent</param>
        /// <param name="Flush">Flush TCP buffer or not</param>
        protected void Send(GameMessage Message, bool Flush = true)
        {
            // surpress accidentially sent messages
            // after BP_REQ_QUIT and while we're waiting for BP_QUIT (server won't understand)
            if (isQuitting)
            {
                return;
            }

#if !VANILLA && !OPENMERIDIAN
            // true for UDP pings which should always be sent through UDP regardless 'useUdp'
            bool isUdpPing =
                Message is GameModeMessage &&
                Message.PI == (byte)MessageTypeGameMode.UdpPing;
#else
            bool isUdpPing = false;
#endif

            // send some by UDP
            if (isUdpPing || (useUdp && Message is GameModeMessage && (
                                  Message.PI == (byte)MessageTypeGameMode.ReqMove ||
                                  Message.PI == (byte)MessageTypeGameMode.ReqTurn ||
                                  Message.PI == (byte)MessageTypeGameMode.ReqAttack)))
            {
                // create UDP header (don't care about existing)
                Message.Header = new MessageHeader.Udp();

                // get size of message in bytes
                int byteLength = Message.ByteLength;
                if (byteLength > 0 && byteLength <= SENDBUFFERSIZE)
                {
                    // use MessageController to sign the message with valid CRC and SS
                    messageController.SignMessage(Message);

                    // serialize
                    Message.WriteTo(sendBuffer, 0);

                    // write the message bytes to stream
                    socketUDP.SendTo(sendBuffer, 0, byteLength, SocketFlags.None, socket.RemoteEndPoint);
                }
            }
            else
            {
                // create TCP header (don't care about existing)
                Message.Header = new MessageHeader.Tcp();

                // get size of message in bytes
                int byteLength = Message.ByteLength;
                if (byteLength > 0 && byteLength <= SENDBUFFERSIZE)
                {
                    // use MessageController to sign the message with valid CRC and SS
                    messageController.SignMessage(Message);

                    //serialize
                    Message.WriteTo(sendBuffer, 0);

                    // write the message bytes to stream
                    tcpStream.Write(sendBuffer, 0, byteLength);

                    // only flush if we should do so
                    if (Flush)
                    {
                        tcpStream.Flush();
                    }
                }
            }

            // track sending of UseCharacter message
            if (Message is UseCharacterMessage)
            {
                // set state
                connectionState = ConnectionState.Playing;

                // log
                Logger.Log(MODULENAME, LogType.Info, "ConnectionState: Playing");
            }

            else if (Message is ReqQuitMessage)
            {
                // flip quitting. no more message can be sent
                // until flipped back to false on BP_QUIT
                isQuitting = true;
            }

            // loop back game message for logging purposes
            // whoever handles the log has to deal with pooled ones
            if (IsOutgoingPacketLogEnabled)
            {
                OutgoingPacketLog.Enqueue(Message);
            }

            // possibly push back pooled ones
            else
            {
                MessagePool.PushFree(Message);
            }
        }