示例#1
0
        private bool SendCommandToServer(OTCNetMessage cmd)
        {
            try
            {
                semaphor.WaitOne();

                // Formatea los datos necesarios del mensaje
                if (String.IsNullOrEmpty(cmd.Message))
                {
                    SetMetaDataIfIsNull(cmd);
                }

                // Envia la longitud del mensaje
                byte[] buffer = new byte[4];
                buffer = BitConverter.GetBytes((int)cmd.ToByte().Length);
                _networkStream.Write(buffer, 0, 4);
                _networkStream.Flush();

                // Envia el mensaje
                buffer = cmd.ToByte();
                _networkStream.Write(buffer, 0, buffer.Length);

                _networkStream.Flush();

                semaphor.Release();
                return(true);
            }
            catch
            {
                semaphor.Release();
                return(false);
            }
        }
示例#2
0
        // This Semaphor is to protect the critical section from concurrent access of sender threads.
        private bool SendCommandToClient(OTCNetMessage cmd)
        {
            try
            {
                _semaphor.WaitOne();

                // Envia la longitud del mensaje
                byte[] buffer = new byte[4];
                buffer = BitConverter.GetBytes((int)cmd.ToByte().Length);
                _netStream.Write(buffer, 0, 4);
                _netStream.Flush();

                // Envia el mensaje
                buffer = cmd.ToByte();
                _netStream.Write(buffer, 0, buffer.Length);

                _netStream.Flush();

                /*
                 * // Type
                 * byte[] buffer = new byte[4];
                 * buffer = BitConverter.GetBytes((int)cmd.Type);
                 * _netStream.Write(buffer, 0, 4);
                 * _netStream.Flush();
                 *
                 * // Sender IP
                 * byte[] FromIPBuffer = Encoding.ASCII.GetBytes(cmd.FromIP.ToString());
                 * buffer = new byte[4];
                 * buffer = BitConverter.GetBytes(FromIPBuffer.Length);
                 * _netStream.Write(buffer, 0, 4);
                 * _netStream.Flush();
                 * _netStream.Write(FromIPBuffer, 0, FromIPBuffer.Length);
                 * _netStream.Flush();
                 *
                 * // Sender Name
                 * byte[] senderNameBuffer = Encoding.Unicode.GetBytes(cmd.From.ToString());
                 * buffer = new byte[4];
                 * buffer = BitConverter.GetBytes(senderNameBuffer.Length);
                 * _netStream.Write(buffer, 0, 4);
                 * _netStream.Flush();
                 * _netStream.Write(senderNameBuffer, 0, senderNameBuffer.Length);
                 * _netStream.Flush();
                 *
                 * // Target
                 * byte[] ipBuffer = Encoding.ASCII.GetBytes(cmd.ToIP.ToString());
                 * buffer = new byte[4];
                 * buffer = BitConverter.GetBytes(ipBuffer.Length);
                 * _netStream.Write(buffer, 0, 4);
                 * _netStream.Flush();
                 * _netStream.Write(ipBuffer, 0, ipBuffer.Length);
                 * _netStream.Flush();
                 *
                 * // Meta Data.
                 * if (String.IsNullOrEmpty(cmd.Message))
                 * cmd.Message = "\n";
                 *
                 * byte[] metaBuffer = Encoding.Unicode.GetBytes(cmd.Message);
                 * buffer = new byte[4];
                 * buffer = BitConverter.GetBytes(metaBuffer.Length);
                 * _netStream.Write(buffer, 0, 4);
                 * _netStream.Flush();
                 * _netStream.Write(metaBuffer, 0, metaBuffer.Length);
                 * _netStream.Flush();
                 */

                _semaphor.Release();
                return(true);
            }
            catch
            {
                _semaphor.Release();
                return(false);
            }
        }