Пример #1
0
        /// <summary>
        /// 向指定的 <see cref="System.Net.Sockets.Socket"/> 中写入一条消息。
        /// </summary>
        /// <param name="socket">要进行写入的 <see cref="System.Net.Sockets.Socket"/>。</param>
        /// <param name="message">要写入的消息。</param>
        /// <returns>一个 <see cref="Kei.KNetwork.MessageIOErrorCode"/> 值,表示写入的结果。</returns>
        public static MessageIOErrorCode WriteMessage(this Socket socket, KMessage message)
        {
            var data = message.ToByteArray();

            try
            {
                socket.Send(data);
                return(MessageIOErrorCode.NoError);
            }
            catch (Exception)
            {
                return(MessageIOErrorCode.WriteFailed);
            }
        }
Пример #2
0
        /// <summary>
        /// 向指定的 <see cref="System.IO.Stream"/> 中写入一条消息。
        /// </summary>
        /// <param name="stream">要进行写入的 <see cref="System.IO.Stream"/>。</param>
        /// <param name="message">要写入的消息。</param>
        /// <returns>一个 <see cref="Kei.KNetwork.MessageIOErrorCode"/> 值,表示写入的结果。</returns>
        public static MessageIOErrorCode WriteMessage(this Stream stream, KMessage message)
        {
            var data = message.ToByteArray();

            try
            {
                stream.Write(data, 0, data.Length);
                stream.Flush();
                return(MessageIOErrorCode.NoError);
            }
            catch (Exception)
            {
                return(MessageIOErrorCode.WriteFailed);
            }
        }