private void buttonSendComplexMessage_Click(object sender, EventArgs e)
        {
            try
            {
                // Create the message to send
                Messages.ComplexMessage message = new Messages.ComplexMessage();
                message.UniqueID = Guid.NewGuid();
                message.Time     = DateTimeOffset.Now;
                message.Message  = textBoxMessage.Text;

                // Serialize the message to a binary array
                byte[] binaryMessage = Messages.Util.Serialize(message);

                // Send the message; the state is used by ClientSocket_WriteCompleted to display an output to the log
                string description = "<complex message: " + message.UniqueID + ">";
                SocketPacketProtocol.WritePacketAsync(ClientSocket, binaryMessage, description);

                textBoxLog.AppendText("Sending message " + description + Environment.NewLine);
            }
            catch (Exception ex)
            {
                ResetSocket();
                textBoxLog.AppendText("Error sending message to socket: [" + ex.GetType().Name + "] " + ex.Message + Environment.NewLine);
            }
            finally
            {
                RefreshDisplay();
            }
        }
예제 #2
0
        private void buttonSendComplexMessage_Click(object sender, EventArgs e)
        {
            // This function sends a complex message to all connected clients
            Messages.ComplexMessage message = new Messages.ComplexMessage();
            message.UniqueID = Guid.NewGuid();
            message.Time     = DateTimeOffset.Now;
            message.Message  = textBoxMessage.Text;

            string description = "<complex message: " + message.UniqueID + ">";

            // Serialize it to a binary array
            byte[] binaryObject = Messages.Util.Serialize(message);

            // Keep a list of all errors for child sockets
            Dictionary <ServerChildTcpSocket, Exception> SocketErrors = new Dictionary <ServerChildTcpSocket, Exception>();

            // Start a send on each child socket
            foreach (KeyValuePair <ServerChildTcpSocket, ChildSocketContext> childSocket in ChildSockets)
            {
                // Ignore sockets that are disconnecting
                if (childSocket.Value.State != ChildSocketState.Connected)
                {
                    continue;
                }

                try
                {
                    textBoxLog.AppendText("Sending to " + childSocket.Key.RemoteEndPoint.ToString() + ": " + description + Environment.NewLine);
                    SocketPacketProtocol.WritePacketAsync(childSocket.Key, binaryObject, description);
                }
                catch (Exception ex)
                {
                    // Make a note of the error to handle later
                    SocketErrors.Add(childSocket.Key, ex);
                }
            }

            // Handle all errors. This is done outside the enumeration loop because the child socket
            //  error recovery will remove the socket from the list of child sockets.
            foreach (KeyValuePair <ServerChildTcpSocket, Exception> error in SocketErrors)
            {
                textBoxLog.AppendText("Child Socket error sending message to " + error.Key.RemoteEndPoint.ToString() + ": [" + error.Value.GetType().Name + "] " + error.Value.Message + Environment.NewLine);
                ResetChildSocket(error.Key);
            }

            // In case there were any errors, the display may need to be updated
            RefreshDisplay();
        }