Пример #1
0
        private ReturnResult SendDataAsWait(long Id, byte[] Data)
        {
            using (ReturnEventWaitHandle wait = new ReturnEventWaitHandle(MillisecondsTimeout, false, EventResetMode.AutoReset))
            {
                if (!SyncWaitDic.TryAdd(Id, wait))
                {
                    LogAction.Log(LogType.Err, "Insert Wait Dic fail");
                    return(null);
                }

                Client.Send(Data);

                wait.WaitOne();
                var value = wait.Result;
                wait.Dispose();

                if (value != null)
                {
                    if (value.Arguments == null)
                    {
                        return(null);
                    }
                    else
                    {
                        return(value);
                    }
                }
                else
                {
                    return(null);
                }
            }
        }
Пример #2
0
        private void BinaryInputOffsetHandler(byte[] data, int offset, int count, SocketAsyncEventArgs socketAsync)
        {
            try
            {
                ASyncToken tmp = socketAsync.UserToken as ASyncToken;

                if (tmp != null)
                {
                    tmp.Write(data, offset, count);
                }
                else if (count >= 8 && data[offset] == 0xFF && data[offset + 1] == 0xFE && data[offset + 5] == 0xCE &&
                         data[offset + 7] == 0xED)
                {
                    var token = NewASyncToken(socketAsync);
                    socketAsync.UserToken = token;

                    if (count > 8)
                    {
                        byte[] bakdata = new byte[count - 8];
                        Buffer.BlockCopy(data, offset + 8, bakdata, 0, bakdata.Length);

                        token.Write(bakdata, 0, bakdata.Length);
                    }
                }
            }
            catch (Exception er)
            {
                LogAction.Log(LogType.Err, er.ToString(), er);
            }
        }
Пример #3
0
 public bool WaitOne()
 {
     if (!WaitHandle.WaitOne(MillisecondsTimeout))
     {
         LogAction.Log(LogType.War, "Wait Result Time Out");
     }
     return(true);
 }
Пример #4
0
        private void DataOn(byte[] data)
        {
            ReadBytes read = null;

            if (DecodingHandler != null)
            {
                read = new ReadBytes(data, 4, -1, DecodingHandler);
            }
            else
            {
                read = new ReadBytes(data);
            }

            int cmd;
            int length;


            if (read.ReadInt32(out length) && read.ReadInt32(out cmd) && read.Length == read.Length)
            {
                switch (cmd)
                {
                case CmdDef.CallCmd:
                {
                    CallPack tmp;

                    if (read.ReadObject <CallPack>(out tmp))
                    {
                        try
                        {
                            CallPackRun(tmp);
                        }
                        catch (Exception er)
                        {
                            LogAction.Log(LogType.Err, "CMD:" + tmp.CmdTag + "\r\n" + er.ToString());
                        }
                    }
                }
                break;

                case CmdDef.ReturnResult:
                {
                    ReturnResult result;

                    if (read.ReadObject <ReturnResult>(out result))
                    {
                        SetReturnValue(result);
                    }
                }
                break;
                }
            }
        }
Пример #5
0
        public void Run()
        {
            Func <Task> wrappedGhostThreadFunction = async() =>
            {
                try
                {
                    if (IsHaveReturn)
                    {
                        Result = await(Task <ReturnResult>) Method.Invoke(Obj, Args);

                        if (Complete != null)
                        {
                            Complete(Result);
                        }
                    }
                    else
                    {
                        await(Task) Method.Invoke(Obj, Args);
                    }
                }
                catch (Exception er)
                {
                    IsError = true;
                    Error   = er;

                    if (IsHaveReturn)
                    {
                        var nullx = new ReturnResult();
                        nullx.Id = this.Id;

                        if (Complete != null)
                        {
                            Complete(nullx);
                        }
                    }

                    LogAction.Log(LogType.Err, "Cmd:" + Cmd + " Error:\r\n" + Error.ToString());
                }
                finally
                {
                    IsOver = true;
                    CCloudClient.RemoveAsyncCall(Id);
                }
            };


            fiber = new Fiber();
            fiber.SetAction(wrappedGhostThreadFunction);
            fiber.Start();
        }
Пример #6
0
        private void SetReturnValue(ReturnResult result)
        {
            long idx = result.Id;

            if (CallBackDiy.ContainsKey(idx))
            {
                AsyncCalls call;

                if (CallBackDiy.TryRemove(result.Id, out call))
                {
                    try
                    {
                        call.SetRet(result);
                    }
                    catch (Exception er)
                    {
                        LogAction.Log(LogType.Err, "CMD:" + call.Cmd + " ERROR:\r\n" + er.Message);
                    }
                }
            }
            else if (AsyncRunDiy.ContainsKey(idx))
            {
                AsyncRun call;

                if (AsyncRunDiy.TryRemove(result.Id, out call))
                {
                    try
                    {
                        call.SetRet(result);
                    }
                    catch (Exception er)
                    {
                        LogAction.Log(LogType.Err, "AsynRun ID:" + result.Id + " ERROR:\r\n" + er.Message);
                    }
                }
            }
            else if (SyncWaitDic.ContainsKey(idx))
            {
                ReturnEventWaitHandle wait;

                if (SyncWaitDic.TryRemove(result.Id, out wait))
                {
                    wait.Set(result);
                }
            }
            else
            {
                throw new InvalidOperationException("not call the Id");
            }
        }
Пример #7
0
        private void MessageInputHandler(string message, SocketAsyncEventArgs socketAsync, int erorr)
        {
            if (socketAsync.UserToken != null)
            {
                ASyncToken tmp = socketAsync.UserToken as ASyncToken;
                if (tmp != null)
                {
                    tmp.Disconnect(message);
                }
            }

            socketAsync.UserToken = null;
#if !COREFX
            socketAsync.AcceptSocket.Close();
#endif
            socketAsync.AcceptSocket.Dispose();
            LogAction.Log(message);
        }
Пример #8
0
        private void CallPackRun(CallPack pack)
        {
            if (Module.ModuleDiy.ContainsKey(pack.CmdTag))
            {
                AsyncMethodDef method = Module.ModuleDiy[pack.CmdTag];

                object[] args = null;

                int argcount = 0;

                if (pack.Arguments != null)
                {
                    argcount = pack.Arguments.Count;
                }

                if (method.ArgsType.Length > 0 && method.ArgsType.Length == (argcount + 1))
                {
                    args = new object[method.ArgsType.Length];

                    args[0] = this;
                    int x = 1;
                    for (int i = 0; i < (method.ArgsType.Length - 1); i++)
                    {
                        x       = i + 1;
                        args[x] = Serialization.UnpackSingleObject(method.ArgsType[x], pack.Arguments[i]);
                    }
                }

                if (args == null)
                {
                    LogAction.Log(LogType.Err, "Server Call To Me-> Cmd:{0} ArgsCount:{1} Args count is Error", pack.CmdTag, argcount);
                }

                if (method.IsAsync)
                {
                    if (!method.IsOut)
                    {
                        AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, method.Obj, method.methodInfo, args, false);
                        args[0]           = _calls_;
                        _calls_.CallSend += SendData;
                        _calls_.Run();

                        AsyncCallDiy.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_);
                    }
                    else
                    {
                        AsyncCalls _calls_ = new AsyncCalls(pack.Id, pack.CmdTag, this, method.Obj, method.methodInfo, args, true);
                        args[0]           = _calls_;
                        _calls_.CallSend += SendData;
                        _calls_.Complete += RetrunResultData;
                        _calls_.Run();


                        AsyncCallDiy.AddOrUpdate(pack.Id, _calls_, (a, b) => _calls_);
                    }
                }
                else //SYNC
                {
                    if (!method.IsOut)
                    {
                        method.methodInfo.Invoke(method.Obj, args);
                    }
                    else
                    {
                        try
                        {
                            object res = method.methodInfo.Invoke(method.Obj, args);

                            if (res != null)
                            {
                                ReturnResult tmp = new ReturnResult(res);
                                tmp.Id = pack.Id;
                                RetrunResultData(tmp);
                            }
                        }
                        catch (Exception er)
                        {
                            ReturnResult tmp = new ReturnResult();
                            tmp.Id = pack.Id;
                            RetrunResultData(tmp);

                            LogAction.Log(LogType.Err, "Cmd:{0} ERROR:" + er.ToString(), pack.CmdTag);
                        }
                    }
                }
            }
            else
            {
                LogAction.Log(LogType.Err, "Server Call To Me-> Cmd:{0} Not Find Cmd", pack.CmdTag);
            }
        }
Пример #9
0
        public void DataOn(byte[] data)
        {
            ReadBytes read = null;

            if (CurrentServer.DecodeingHandler != null)
            {
                read = new ReadBytes(data, 4, -1, CurrentServer.DecodeingHandler);
            }
            else
            {
                read = new ReadBytes(data);
            }

            int cmd;
            int length;

            if (read.ReadInt32(out length) && read.ReadInt32(out cmd) && read.Length == length)
            {
                switch (cmd)
                {
                case CmdDef.CallCmd:
                {
                    CallPack tmp;

                    if (read.ReadObject <CallPack>(out tmp))
                    {
                        try
                        {
                            CallPackRun(tmp);
                        }
                        catch (Exception er)
                        {
                            LogAction.Log(LogType.Err, "CMD:" + tmp.CmdTag + "\r\n" + er.ToString());
                        }
                    }
                }
                break;

                case CmdDef.ReturnResult:
                {
                    ReturnResult result;

                    if (read.ReadObject <ReturnResult>(out result))
                    {
                        if (CallBackDiy.ContainsKey(result.Id))
                        {
                            AsyncCalls call;

                            if (CallBackDiy.TryRemove(result.Id, out call))
                            {
                                try
                                {
                                    call.SetRet(result);
                                }
                                catch (Exception er)
                                {
                                    LogAction.Log(LogType.Err, "Cmd:" + call.Cmd + " Error:\r\n" + er.ToString());
                                }
                            }
                        }
                    }
                }
                break;
                }
            }
        }
Пример #10
0
        private bool ConnectionFilter(SocketAsyncEventArgs socketAsync)
        {
            LogAction.Log(socketAsync.AcceptSocket.RemoteEndPoint + " Connect");

            return(IsCanConn == null || IsCanConn((IPEndPoint)socketAsync.AcceptSocket.RemoteEndPoint));
        }
Пример #11
0
 public void Pause()
 {
     Server.Stop();
     LogAction.Log("Server is Pause");
 }
Пример #12
0
 public void Start()
 {
     Server.Start();
     LogAction.Log("Server is Start");
 }