/// <summary> /// 调用返回值(警告:每次调用只能使用一次) /// </summary> /// <param name="value"></param> public void Get(out fastCSharp.net.returnValue value) { waitHandle.Wait(); value.Type = outputParameter; outputParameter = returnValue.type.Unknown; typePool <waitCall> .PushNotNull(this); }
private void _M0(socket socket, ref subArray <byte> data) { fastCSharp.net.returnValue.type returnType = fastCSharp.net.returnValue.type.Unknown; try { _i0 inputParameter = new _i0(); if ((socket.Flags & fastCSharp.net.tcp.commandServer.commandFlags.JsonSerialize) == 0 ? fastCSharp.emit.dataDeSerializer.DeSerialize(ref data, ref inputParameter) : fastCSharp.code.cSharp.tcpBase.JsonDeSerialize(ref inputParameter, ref data)) { _o0 outputParameter = new _o0(); Func <fastCSharp.net.returnValue <fastCSharp.code.cSharp.tcpBase.subByteArrayBuffer>, bool> callbackReturn = fastCSharp.net.tcp.commandServer.socket.callback <_o0, fastCSharp.code.cSharp.tcpBase.subByteArrayBuffer> .Get(socket, ref outputParameter, 0); if (callbackReturn != null) { _value_.serverAsynchronous(inputParameter.data, callbackReturn); } return; } returnType = fastCSharp.net.returnValue.type.ServerDeSerializeError; } catch (Exception error) { returnType = fastCSharp.net.returnValue.type.ServerException; fastCSharp.log.Error.Add(error, null, true); } socket.SendStream(socket.Identity, returnType); }
/// <summary> /// 等待返回 /// </summary> /// <returns>是否存在返回值</returns> public fastCSharp.net.returnValue.type Wait() { waitHandle.Wait(); returnValue.type outputParameter = this.outputParameter; this.outputParameter = returnValue.type.Unknown; typePool <waitCall> .PushNotNull(this); return(outputParameter); }
/// <summary> /// TCP调用客户端调用结束 /// </summary> /// <param name="client">TCP调用服务器信息</param> /// <param name="returnType">是否回调成功</param> protected void _end_(ref clientIdentity client, fastCSharp.net.returnValue.type returnType) { if (isDisposed == 0) { if (returnType == fastCSharp.net.returnValue.type.Success) { Monitor.Enter(clientLock); callCount -= clients[client.Index].End(client.Identity); Monitor.Exit(clientLock); } else { Monitor.Enter(clientLock); keyValue <clientType, keyValue <host, int> > errorClient = clients[client.Index].Error(client.Identity); if (errorClient.Key == null) { Monitor.Exit(clientLock); } else { callCount -= errorClient.Value.Value; try { freeIndexs.Add(client.Index); } finally { Monitor.Exit(clientLock); pub.Dispose(ref errorClient.Key); host host = errorClient.Value.Key; bool isCreate = newServer(ref host); if (isCreate) { tryCheck(); log.Default.Add("恢复TCP调用服务端[调用错误] " + host.Host + ":" + host.Port.toString(), new System.Diagnostics.StackFrame(), false); } else { log.Default.Add("移除TCP调用服务端[调用错误] " + host.Host + ":" + host.Port.toString(), new System.Diagnostics.StackFrame(), false); } } } } } client.Identity = int.MinValue; client.Client = null; }
private void _M1(socket socket, ref subArray <byte> data) { fastCSharp.net.returnValue.type returnType = fastCSharp.net.returnValue.type.Unknown; try { _i1 inputParameter = new _i1(); if ((socket.Flags & fastCSharp.net.tcp.commandServer.commandFlags.JsonSerialize) == 0 ? fastCSharp.emit.dataDeSerializer.DeSerialize(ref data, ref inputParameter) : fastCSharp.code.cSharp.tcpBase.JsonDeSerialize(ref inputParameter, ref data)) { _s1 /**/.Call(socket, _value_, ref inputParameter); return; } returnType = fastCSharp.net.returnValue.type.ServerDeSerializeError; } catch (Exception error) { returnType = fastCSharp.net.returnValue.type.ServerException; fastCSharp.log.Error.Add(error, null, true); } socket.SendStream(socket.Identity, returnType); }
private void _M4(socket socket, ref subArray <byte> data) { fastCSharp.net.returnValue.type returnType = fastCSharp.net.returnValue.type.Unknown; try { { _o4 outputParameter = new _o4(); Func <fastCSharp.net.returnValue <fastCSharp.demo.chatServer.server.message>, bool> callbackReturn = fastCSharp.net.tcp.commandServer.socket.callback <_o4, fastCSharp.demo.chatServer.server.message> .GetKeep(socket, ref outputParameter, 0); if (callbackReturn != null) { _value_.receive(socket, callbackReturn); } return; } } catch (Exception error) { returnType = fastCSharp.net.returnValue.type.ServerException; fastCSharp.log.Error.Add(error, null, true); } socket.SendStream(socket.Identity, returnType); }
private void _M2(socket socket, ref subArray <byte> data) { fastCSharp.net.returnValue.type returnType = fastCSharp.net.returnValue.type.Unknown; try { { _o2 outputParameter = new _o2(); Func <fastCSharp.net.returnValue <string[]>, bool> callbackReturn = fastCSharp.net.tcp.commandServer.socket.callback <_o2, string[]> .GetKeep(socket, ref outputParameter, 0); if (callbackReturn != null) { _value_.getUsers(socket, callbackReturn); } return; } } catch (Exception error) { returnType = fastCSharp.net.returnValue.type.ServerException; fastCSharp.log.Error.Add(error, null, true); } socket.SendStream(socket.Identity, returnType); }
/// <summary> /// 回调处理 /// </summary> /// <param name="outputParameter">是否调用成功</param> public override void Callback(ref fastCSharp.net.returnValue outputParameter) { this.outputParameter = outputParameter.Type; //if (!outputParameter) log.Default.Add("异步调用失败(bool)", true, false); waitHandle.Set(); }
/// <summary> /// 检测任务 /// </summary> internal override void Check() { if (isDisposed == 0) { int count = 0; DateTime now = date.nowTime.Now.AddTicks(-checkTicks); Monitor.Enter(clientLock); try { if (removeClients.Length < currentCount) { removeClients = new keyValue <clientType, int> [clients.Length]; } for (int index = 0; index != currentCount; ++index) { clientType client = clients[index].CheckTimeout(now); if (client != null && _loadBalancingCheckTime_(client) < now) { removeClients[count++].Set(client, index); } } } finally { Monitor.Exit(clientLock); } while (count != 0) { fastCSharp.net.returnValue.type returnType = fastCSharp.net.returnValue.type.Unknown; clientType client = removeClients[--count].Key; try { returnType = _loadBalancingCheck_(client); } catch (Exception error) { log.Error.Add(error, null, false); } if (returnType != fastCSharp.net.returnValue.type.Success) { int index = removeClients[count].Value; Monitor.Enter(clientLock); keyValue <host, int> host = clients[index].Remove(client); if (host.Key.Host == null) { Monitor.Exit(clientLock); } else { callCount -= host.Value; try { freeIndexs.Add(index); } finally { Monitor.Exit(clientLock); } pub.Dispose(ref client); if (newServer(ref host.Key)) { log.Default.Add("恢复TCP调用服务端[检测超时] " + host.Key.Host + ":" + host.Key.Port.toString(), new System.Diagnostics.StackFrame(), false); } else { log.Default.Add("移除TCP调用服务端[检测超时] " + host.Key.Host + ":" + host.Key.Port.toString(), new System.Diagnostics.StackFrame(), false); } } } } Monitor.Enter(clientLock); if (currentCount == freeIndexs.length) { isCheckTask = 0; Monitor.Exit(clientLock); } else { Monitor.Exit(clientLock); addCheck(); } } }
private void _M0(socket socket, ref subArray <byte> data) { fastCSharp.net.returnValue.type returnType = fastCSharp.net.returnValue.type.Unknown; try { _i0 inputParameter = new _i0(); if ((socket.Flags & fastCSharp.net.tcp.commandServer.commandFlags.JsonSerialize) == 0 ? fastCSharp.emit.dataDeSerializer.DeSerialize(ref data, ref inputParameter) : fastCSharp.code.cSharp.tcpBase.JsonDeSerialize(ref inputParameter, ref data)) { _s0 /**/.Call(socket, _value_, ref inputParameter); return; } returnType = fastCSharp.net.returnValue.type.ServerDeSerializeError; } catch (Exception error) { returnType = fastCSharp.net.returnValue.type.ServerException; fastCSharp.log.Error.Add(error, null, true); } socket.SendStream(socket.Identity, returnType); } [fastCSharp.emit.dataSerialize(IsMemberMap = false, IsReferenceMember = false)] internal struct _i1 { public int left; public int right; } [fastCSharp.emit.dataSerialize(IsMemberMap = false, IsReferenceMember = false)] [fastCSharp.emit.boxSerialize] #if NOJIT internal struct _o1 : fastCSharp.net.asynchronousMethod.IReturnParameter #else internal struct _o1 : fastCSharp.net.asynchronousMethod.IReturnParameter <int> #endif { [fastCSharp.emit.jsonSerialize.member(IsIgnoreCurrent = true)] [fastCSharp.emit.jsonParse.member(IsIgnoreCurrent = true)] public int Ret; public int Return { get { return(Ret); } set { Ret = value; } } #if NOJIT public object ReturnObject { get { return(Ret); } set { Ret = (int)value; } } #endif } private void _M1(socket socket, ref subArray <byte> data) { fastCSharp.net.returnValue.type returnType = fastCSharp.net.returnValue.type.Unknown; try { _i1 inputParameter = new _i1(); if ((socket.Flags & fastCSharp.net.tcp.commandServer.commandFlags.JsonSerialize) == 0 ? fastCSharp.emit.dataDeSerializer.DeSerialize(ref data, ref inputParameter) : fastCSharp.code.cSharp.tcpBase.JsonDeSerialize(ref inputParameter, ref data)) { _o1 outputParameter = new _o1(); Func <fastCSharp.net.returnValue <int>, bool> callbackReturn = fastCSharp.net.tcp.commandServer.socket.callback <_o1, int> .Get(socket, ref outputParameter, 0); if (callbackReturn != null) { _value_.xor(inputParameter.left, inputParameter.right, callbackReturn); } return; } returnType = fastCSharp.net.returnValue.type.ServerDeSerializeError; } catch (Exception error) { returnType = fastCSharp.net.returnValue.type.ServerException; fastCSharp.log.Error.Add(error, null, true); } socket.SendStream(socket.Identity, returnType); } } /// <summary> /// TCP负载均衡服务 /// </summary> public sealed class tcpLoadBalancing : fastCSharp.net.tcp.commandLoadBalancingServer <tcpClient> { /// <summary> /// TCP负载均衡服务 /// </summary> /// <param name="attribute">TCP调用服务器端配置信息</param> /// <param name="verify">TCP验证实例</param> #if NOJIT public tcpLoadBalancing(fastCSharp.code.cSharp.tcpServer attribute = null, fastCSharp.code.cSharp.tcpBase.ITcpClientVerify verify = null) #else public tcpLoadBalancing(fastCSharp.code.cSharp.tcpServer attribute = null, fastCSharp.code.cSharp.tcpBase.ITcpClientVerify verify = null) #endif : base(attribute ?? fastCSharp.code.cSharp.tcpServer.GetConfig("loadBalancingTestLoadBalancing", typeof(fastCSharp.demo.loadBalancingTcpCommand.server)), verify) { } protected override tcpClient _createClient_(fastCSharp.code.cSharp.tcpServer attribute) { tcpClient client = new tcpClient(attribute, _verify_); fastCSharp.net.tcp.commandClient commandClient = client.TcpCommandClient; if (commandClient != null && commandClient.StreamSocket != null) { return(client); } fastCSharp.pub.Dispose(ref client); return(null); } protected override DateTime _loadBalancingCheckTime_(tcpClient client) { fastCSharp.net.tcp.commandClient tcpClient = client.TcpCommandClient; return(tcpClient != null ? tcpClient.LoadBalancingCheckTime : DateTime.MinValue); } protected override fastCSharp.net.returnValue.type _loadBalancingCheck_(tcpClient client) { fastCSharp.net.tcp.commandClient tcpClient = client.TcpCommandClient; return(tcpClient == null ? fastCSharp.net.returnValue.type.ClientDisposed : tcpClient.LoadBalancingCheck()); } public fastCSharp.net.returnValue <int> add(int left, int right) { fastCSharp.net.returnValue.type _returnType_; _returnType_ = fastCSharp.net.returnValue.type.Unknown; int _tryCount_ = 3; do { clientIdentity _client_ = _getClient_(); if (_client_.Client == null) { _returnType_ = fastCSharp.net.returnValue.type.ClientDisposed; break; } try { fastCSharp.net.returnValue <int> _return_ = _client_.Client.add(left, right); _end_(ref _client_, _return_.Type); if (_return_.Type == fastCSharp.net.returnValue.type.Success || _return_.Type == fastCSharp.net.returnValue.type.VersionExpired) { return(_return_); } System.Threading.Thread.Sleep(1); } catch (Exception _error_) { _end_(ref _client_, _returnType_ = fastCSharp.net.returnValue.type.ClientException); fastCSharp.log.Error.Add(_error_, null, false); } }while (--_tryCount_ > 0); return(new fastCSharp.net.returnValue <int> { Type = _returnType_ }); } sealed class _l1 : fastCSharp.code.cSharp.tcpBase.loadBalancingCallback <int> { private clientIdentity _client_; private tcpLoadBalancing _loadBalancingServer_; private int left; private int right; protected override void _call_() { fastCSharp.net.returnValue.type _returnType_; try { _client_ = _loadBalancingServer_._getClient_(); if (_client_.Client != null) { _client_.Client.xor(left, right, _onReturnHandle_); return; } _returnType_ = fastCSharp.net.returnValue.type.ClientDisposed; } catch (Exception error) { _loadBalancingServer_._end_(ref _client_, _returnType_ = fastCSharp.net.returnValue.type.ClientException); fastCSharp.log.Error.Add(error, null, false); } _push_(new fastCSharp.net.returnValue <int> { Type = _returnType_ }); } protected override void _push_(fastCSharp.net.returnValue.type isReturn) { _loadBalancingServer_._end_(ref _client_, isReturn); _loadBalancingServer_ = null; left = default(int); right = default(int); fastCSharp.typePool <_l1> .PushNotNull(this); } public static void _Call_(tcpLoadBalancing _loadBalancingServer_, int left, int right, Action <fastCSharp.net.returnValue <int> > _onReturn_) { _l1 _callback_ = fastCSharp.typePool <_l1> .Pop(); if (_callback_ == null) { try { _callback_ = new _l1(); } catch (Exception error) { fastCSharp.log.Error.Add(error, null, false); _onReturn_(new fastCSharp.net.returnValue <int> { Type = fastCSharp.net.returnValue.type.ClientException }); return; } } _callback_.left = left; _callback_.right = right; _callback_._loadBalancingServer_ = _loadBalancingServer_; _callback_._onReturn_ = _onReturn_; _callback_._tryCount_ = 3; _callback_._call_(); } } public void xor(int left, int right, Action <fastCSharp.net.returnValue <int> > _onReturn_) { _l1 /**/._Call_(this, left, right, _onReturn_); } }
private void _M0(socket socket, ref subArray <byte> data) { fastCSharp.net.returnValue.type returnType = fastCSharp.net.returnValue.type.Unknown; try { _i0 inputParameter = new _i0(); if ((socket.Flags & fastCSharp.net.tcp.commandServer.commandFlags.JsonSerialize) == 0 ? fastCSharp.emit.dataDeSerializer.DeSerialize(ref data, ref inputParameter) : fastCSharp.code.cSharp.tcpBase.JsonDeSerialize(ref inputParameter, ref data)) { _s0 /**/.Call(socket, _value_, ref inputParameter); return; } returnType = fastCSharp.net.returnValue.type.ServerDeSerializeError; } catch (Exception error) { returnType = fastCSharp.net.returnValue.type.ServerException; fastCSharp.log.Error.Add(error, null, true); } socket.SendStream(socket.Identity, returnType); } sealed class _s1 : fastCSharp.net.tcp.commandServer.serverCall <_s1, fastCSharp.demo.chatServer.server> { private void get(ref fastCSharp.net.returnValue value) { try { serverValue.logout(socket); value.Type = fastCSharp.net.returnValue.type.Success; } catch (Exception error) { value.Type = fastCSharp.net.returnValue.type.ServerException; fastCSharp.log.Error.Add(error, null, true); } } public override void Call() { fastCSharp.net.returnValue value = new fastCSharp.net.returnValue(); if (isVerify == 0) { get(ref value); socket.SendStream(ref identity, ref value, flags); } fastCSharp.typePool <_s1> .PushNotNull(this); } } private void _M1(socket socket, ref subArray <byte> data) { fastCSharp.net.returnValue.type returnType = fastCSharp.net.returnValue.type.Unknown; try { { _s1 /**/.Call(socket, _value_); return; } } catch (Exception error) { returnType = fastCSharp.net.returnValue.type.ServerException; fastCSharp.log.Error.Add(error, null, true); } socket.SendStream(socket.Identity, returnType); }