コード例 #1
0
ファイル: Connection.cs プロジェクト: sitegui/asyncProtocol
        /// <summary>
        /// Send a call to the server
        /// </summary>
        /// <param name="type">The call type (must have been registered with Registry.RegisterClientCall)</param>
        /// <param name="data">The data pack to send</param>
        /// <param name="onReturn">The callback to be executed when the server answers this call</param>
        /// <param name="onException">The callback to be executed when the server answer this call with an exception (or the call timeouts or the connection closes)</param>
        /// <param name="timeout">The timeout (in ms), 0 means no timeout</param>
        public void SendCall(uint type, Data data, ReturnDelegate onReturn = null, ExceptionDelegate onException = null, int timeout = 60000)
        {
            // Validate the data
            if (!IsReady)
            {
                throw new InvalidOperationException("The connection has already been closed");
            }
            Registry.RegisteredCall call = Registry.GetClientCall(type);
            if (call == null)
            {
                throw new ArgumentException("Invalid call type " + type);
            }
            if (data.Format != call.ArgsFormat.FormatString)
            {
                throw new ArgumentException("Invalid data type '" + data.Format + "' for call " + type);
            }

            // Create the meta-data
            byte[] binData   = data.GetBytes();
            byte[] binMeta   = new Data().AddUint(type).AddUint(++LastSentId).GetBytes();
            byte[] binLength = new Data().AddUint((ulong)(binData.Length + binMeta.Length)).GetBytes();

            // Send the call
            Socket.Write(binLength);
            Socket.Write(binMeta);
            Socket.Write(binData);

            // Set timeout
            Timer interval = null;

            if (timeout != 0)
            {
                interval           = new Timer(timeout);
                interval.AutoReset = false;
                interval.Elapsed  += TimeoutCallback;
                interval.Start();
            }

            // Save info about the sent call
            PendingCalls.AddLast(new PendingCall(LastSentId, call, onReturn, onException, interval));
        }
コード例 #2
0
ファイル: Connection.cs プロジェクト: sitegui/asyncProtocol
            /// <summary>
            /// Internal method to send the answer
            /// </summary>
            /// <param name="exceptionType">The exception type (0 means no exception, normal return instead)</param>
            /// <param name="data">The data pack to send</param>
            /// <returns>Return whether the answer was sent (false if the connection is closed)</returns>
            bool Answer(uint exceptionType, Data data)
            {
                // Validate the connection
                if (answered)
                {
                    throw new InvalidOperationException("Answer already sent");
                }
                if (!Conn.IsReady)
                {
                    return(false);
                }

                // Validate the arguments
                if (exceptionType != 0)
                {
                    if (!Call.HasException(exceptionType))
                    {
                        throw new ArgumentException("Invalid exception " + exceptionType + " to call " + Call.Id);
                    }
                }
                else
                {
                    if (data.Format != Call.ReturnFormat.FormatString)
                    {
                        throw new ArgumentException("Invalid data type '" + data.Format + "' for return type " + Call.Id);
                    }
                }

                // Send the answer
                byte[] binData   = data.GetBytes();
                byte[] binMeta   = new Data().AddUint((ulong)0).AddInt(CallId).AddInt(exceptionType).GetBytes();
                byte[] binLength = new Data().AddUint((ulong)(binData.Length + binMeta.Length)).GetBytes();
                Conn.Socket.Write(binLength);
                Conn.Socket.Write(binMeta);
                Conn.Socket.Write(binData);
                answered = true;
                return(true);
            }
コード例 #3
0
ファイル: Connection.cs プロジェクト: sitegui/asyncProtocol
            /// <summary>
            /// Internal method to send the answer
            /// </summary>
            /// <param name="exceptionType">The exception type (0 means no exception, normal return instead)</param>
            /// <param name="data">The data pack to send</param>
            /// <returns>Return whether the answer was sent (false if the connection is closed)</returns>
            bool Answer(uint exceptionType, Data data)
            {
                // Validate the connection
                if (answered)
                    throw new InvalidOperationException("Answer already sent");
                if (!Conn.IsReady)
                    return false;

                // Validate the arguments
                if (exceptionType != 0) {
                    if (!Call.HasException(exceptionType))
                        throw new ArgumentException("Invalid exception " + exceptionType + " to call " + Call.Id);
                } else {
                    if (data.Format != Call.ReturnFormat.FormatString)
                        throw new ArgumentException("Invalid data type '" + data.Format + "' for return type " + Call.Id);
                }

                // Send the answer
                byte[] binData = data.GetBytes();
                byte[] binMeta = new Data().AddUint((ulong)0).AddInt(CallId).AddInt(exceptionType).GetBytes();
                byte[] binLength = new Data().AddUint((ulong)(binData.Length + binMeta.Length)).GetBytes();
                Conn.Socket.Write(binLength);
                Conn.Socket.Write(binMeta);
                Conn.Socket.Write(binData);
                answered = true;
                return true;
            }
コード例 #4
0
ファイル: Connection.cs プロジェクト: sitegui/asyncProtocol
        /// <summary>
        /// Send a call to the server
        /// </summary>
        /// <param name="type">The call type (must have been registered with Registry.RegisterClientCall)</param>
        /// <param name="data">The data pack to send</param>
        /// <param name="onReturn">The callback to be executed when the server answers this call</param>
        /// <param name="onException">The callback to be executed when the server answer this call with an exception (or the call timeouts or the connection closes)</param>
        /// <param name="timeout">The timeout (in ms), 0 means no timeout</param>
        public void SendCall(uint type, Data data, ReturnDelegate onReturn = null, ExceptionDelegate onException = null, int timeout = 60000)
        {
            // Validate the data
            if (!IsReady)
                throw new InvalidOperationException("The connection has already been closed");
            Registry.RegisteredCall call = Registry.GetClientCall(type);
            if (call == null)
                throw new ArgumentException("Invalid call type " + type);
            if (data.Format != call.ArgsFormat.FormatString)
                throw new ArgumentException("Invalid data type '" + data.Format + "' for call " + type);

            // Create the meta-data
            byte[] binData = data.GetBytes();
            byte[] binMeta = new Data().AddUint(type).AddUint(++LastSentId).GetBytes();
            byte[] binLength = new Data().AddUint((ulong)(binData.Length + binMeta.Length)).GetBytes();

            // Send the call
            Socket.Write(binLength);
            Socket.Write(binMeta);
            Socket.Write(binData);

            // Set timeout
            Timer interval = null;
            if (timeout != 0) {
                interval = new Timer(timeout);
                interval.AutoReset = false;
                interval.Elapsed += TimeoutCallback;
                interval.Start();
            }

            // Save info about the sent call
            PendingCalls.AddLast(new PendingCall(LastSentId, call, onReturn, onException, interval));
        }