Пример #1
0
        private void DoReceiveBson(TcpBsonPackage package)
        {
            var dataType = Type.GetType(package.DataType);

            lock (mJsonHandlers)
            {
                foreach (var handler in mJsonHandlers)
                {
                    if (handler.Type == dataType)
                    {
                        handler.Callback.DynamicInvoke(BsonConvert.DeserializeObject(package.DataBytes, dataType), this);
                    }
                }
            }
        }
Пример #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);
            }
        }
Пример #3
0
        internal void Sync(TcpBsonPackage package)
        {
            var sync = BsonConvert.DeserializeObject <TcpBsonCookieSync>(package.DataBytes);

            lock (this)
            {
                switch (sync.Action)
                {
                case TcpBsonCookieSyncAction.Clear:
                    mCookies.Clear();
                    break;

                case TcpBsonCookieSyncAction.Add:
                case TcpBsonCookieSyncAction.Update:
                    mCookies[sync.Name] = sync.Value;
                    break;

                case TcpBsonCookieSyncAction.Remove:
                    mCookies.Remove(sync.Name);
                    break;
                }
            }
        }
Пример #4
0
 private void DoReceiveResponse(TcpBsonPackage package)
 {
     if (package.DataType == "ERROR")
     {
         var errorResponse = BsonConvert.DeserializeObject <TcpBsonResponse <string> >(package.DataBytes);
         var responseCxt   = mWaitResponseContexts.FirstOrDefault(r => r.Id == errorResponse.Id);
         if (responseCxt != null)
         {
             responseCxt.SetError(errorResponse.Object);
         }
     }
     else
     {
         var responseType = typeof(TcpBsonResponse <>).MakeGenericType(Type.GetType(package.DataType));
         var responseBody = BsonConvert.DeserializeObject(package.DataBytes, responseType);
         var id           = (Guid)responseType.GetProperty("Id").GetValue(responseBody, null);
         var responseCxt  = mWaitResponseContexts.FirstOrDefault(r => r.Id == id);
         if (responseCxt != null)
         {
             responseCxt.SetResponse(responseType.GetProperty("Object").GetValue(responseBody, null));
         }
     }
 }