Esempio n. 1
0
 /// <summary>
 /// Send an Object
 /// </summary>
 /// <typeparam name="T">Object type</typeparam>
 /// <param name="obj">Object</param>
 /// <param name="callback">Post-Send callback</param>
 public void SendObject <T>(T obj, Action callback = null)
 {
     SendPackage(new TcpBsonPackage
     {
         Type      = TcpBsonPackageType.Bson,
         DataType  = GetDataType(typeof(T)),
         DataBytes = BsonConvert.SerializeObject(obj),
         Callback  = callback
     });
 }
Esempio n. 2
0
        private void DoReceiveRequest(TcpBsonPackage package)
        {
            var requestType = Type.GetType(package.DataType);
            var dataType    = typeof(TcpBsonRequest <>).MakeGenericType(requestType);

            var requestBody = BsonConvert.DeserializeObject(package.DataBytes, dataType) as TcpBsonRequest;
            var handler     = mReceiveRequestCallbacks.FirstOrDefault(c => c.URI.Equals(requestBody.URI, StringComparison.OrdinalIgnoreCase) && (c.RequestType == requestType || c.RequestType.IsAssignableFrom(requestType)));

            var responsePackage = new TcpBsonPackage {
                Type = TcpBsonPackageType.Response
            };
            object responseBody = null;

            if (handler == null)
            {
                responseBody = new TcpBsonResponse <string> {
                    Id = requestBody.Id, Object = "No handle this request"
                };
                responsePackage.DataType = "ERROR";
                return;
            }
            try
            {
                var response         = handler.Callback.DynamicInvoke(requestBody.Object, this);
                var responseDataType = response != null?response.GetType() : handler.ResponseType;

                var responseType = typeof(TcpBsonResponse <>).MakeGenericType(responseDataType);

                var responseData = responseType.GetConstructor(Type.EmptyTypes).Invoke(null);
                responseType.GetProperty("Id").SetValue(responseData, requestBody.Id, null);
                responseType.GetProperty("Object").SetValue(responseData, response, null);

                responsePackage.DataType = responseDataType.AssemblyQualifiedName;
                responseBody             = responseData;
            }
            catch (Exception e)
            {
                responseBody = new TcpBsonResponse <string> {
                    Id = requestBody.Id, Object = "Handle Exception - " + e.ToString()
                };
                responsePackage.DataType = "ERROR";
            }
            finally
            {
                responsePackage.DataBytes = BsonConvert.SerializeObject(responseBody);
                SendPackage(responsePackage);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Sync Send an Request and wait Response
        /// </summary>
        /// <typeparam name="TRequest">Response Object Type</typeparam>
        /// <param name="uri">A request uri</param>
        /// <param name="obj">Request object</param>
        /// <param name="millisecondsTimeout">Response timeout milliseconds, Default is one Minute.</param>
        /// <returns>Response object</returns>
        public object SendRequest <TRequest>(string uri, TRequest obj, int millisecondsTimeout = 60000)
        {
            var data = new TcpBsonRequest <TRequest>
            {
                Id     = Guid.NewGuid(),
                URI    = uri,
                Object = obj
            };
            TcpBsonRequestContext cxt = new TcpBsonRequestContext(this, data.Id);

            SendPackage(new TcpBsonPackage
            {
                Type      = TcpBsonPackageType.Request,
                DataType  = typeof(TRequest).AssemblyQualifiedName,
                DataBytes = BsonConvert.SerializeObject(data)
            });
            mWaitResponseContexts.Add(cxt);
            try
            {
                if (cxt.WaitHandler.WaitOne(millisecondsTimeout))
                {
                    if (cxt.IsError)
                    {
                        throw new Exception(cxt.ErrorMessage);
                    }
                    return(cxt.Response);
                }
                else
                {
                    throw new TimeoutException("Response is Timeout!");
                }
            }
            finally
            {
                mWaitResponseContexts.Remove(cxt);
            }
        }
Esempio n. 4
0
 /// <summary>
 /// End a init, This will restore cookie sync.
 /// </summary>
 public void EndInit()
 {
     mInInitMode = false;
     if (mInitSyncs.Any())
     {
         lock (mInitSyncs)
         {
             foreach (var sync in mInitSyncs)
             {
                 mClient.SendPackage(new TcpBsonPackage {
                     Type = TcpBsonPackageType.CookieSync, DataType = typeof(TcpBsonCookieSync).AssemblyQualifiedName, DataBytes = BsonConvert.SerializeObject(sync)
                 });
             }
             mInitSyncs.Clear();
         }
     }
 }
Esempio n. 5
0
 private void SendSync(TcpBsonCookieSync sync)
 {
     if (mInInitMode)
     {
         lock (mInitSyncs)
         {
             mInitSyncs.Add(sync);
         }
     }
     else
     {
         mClient.SendPackage(new TcpBsonPackage {
             Type = TcpBsonPackageType.CookieSync, DataType = typeof(TcpBsonCookieSync).AssemblyQualifiedName, DataBytes = BsonConvert.SerializeObject(sync)
         });
     }
 }