Exemple #1
0
        /// <summary>
        /// Iterates through any queued messages and sends them to the client.
        /// </summary>
        /// <returns><c>True</c> if the end operation succeeded, otherwise <c>false</c>.</returns>
        private bool SendQueuedPackets()
        {
            while (this.queuedPackets.Count > 0)
            {
                // Get a packet and attempt to serialize it to the send buffer
                IPacket packet  = this.queuedPackets.Dequeue();
                int     written = Framing.WritePacket(this.writeBuffer, BUFFER_SIZE, 0, packet);
                Debug.Assert(written > 0, "[ConnectedClient.SendQueuedPackets] No data in packet");

                // Write the buffer to the socket
                if (this.socket.Poll(1, SelectMode.SelectWrite))
                {
                    int sent = this.socket.Send(this.writeBuffer, written, SocketFlags.None);
                    if (sent == 0)
                    {
                        return(false);
                    }
                    Debug.WriteLine(String.Format("[ConnectedClient.SendQueuedPackets] Sent a packet of type {0} with {1} byte(s)", packet.PacketHeader, sent));
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
        /// <summary>
        /// Send any waiting server messages via the socket.
        /// </summary>
        private void SendQueuedMessages()
        { 
            while (messages.Count > 0)
            {
                // Create a new packet and add waiting messages to it
                var packet = new Packet();
                while (packet.Count < Packet.MESSAGES_PER_PACKET)
                {
                    packet.Add(messages.Dequeue());
                    if (messages.Count == 0)
                        break;
                }

                // Write the serialized packet into the buffer
                int packetSize = Framing.WritePacket(packetBuffer, BUFFER_SIZE, 0, packet);

                try
                {
                    // Attempt to send the serialized packet to the server
                    if (this.socket.Poll(POLL_TIMEOUT, SelectMode.SelectWrite))
                    {
                        int sent = this.socket.Send(packetBuffer, packetSize, SocketFlags.None);
                        if (sent == 0)
                        {
                            Debug.LogWarning("[ServerConnection.SendQueuedMessages] 0 bytes written to socket. Connection lost?");
                        }
                    }
                    else
                    {
                        Debug.LogWarning("[ServerConnection.SendQueuedMessages] Unable to write to socket. Connection lost?");
                    }
                }
                catch (SocketException ex)
                {
                    Debug.LogError("[ServerConnection.SendQueuedMessages] " + ex.Message + " " + ex.NativeErrorCode);
                }
            }
            // TODO: send periodic heartbeat if no other messages queued
        }