예제 #1
0
        public void Send(Package pkg)
        {
            if (Status == ClientStatus.Stopped)
            {
                return;
            }

            if (pkg != null)
            {
                lock (sendLock)
                {
                    System.IO.MemoryStream           msWBuffer = new System.IO.MemoryStream();
                    Thrift.Transport.TTransport      tp        = new Thrift.Transport.TStreamTransport(null, msWBuffer);
                    Thrift.Protocol.TCompactProtocol cp        = new Thrift.Protocol.TCompactProtocol(tp);
                    switch (pkg.PackType)
                    {
                    case PackType.Command:
                    {
                        cp.WriteI16(PackType.Command);
                        cp.WriteString((string)pkg.Content);
                    }
                    break;

                    case PackType.ResultModel:
                    {
                        cp.WriteI16(PackType.ResultModel);
                        ResultModel rm = (ResultModel)pkg.Content;
                        rm.Write(cp);
                    }
                    break;

                    case PackType.RequestTask:
                    {
                        cp.WriteI16(PackType.RequestTask);
                        RequestTask rm = (RequestTask)pkg.Content;
                        rm.Write(cp);
                    }
                    break;

                    default:
                        break;
                    }

                    tp.Flush();
                    byte[] buffer = msWBuffer.ToArray();
                    client.Send(buffer, buffer.Length);
                    msWBuffer.Flush();
                }
            }
        }
        public void Send(Package pkg)
        {
            lock (sendLock)
            {
                if (pkg == null)
                {
                    return;
                }
                try
                {
                    System.IO.MemoryStream           msWBuffer = new System.IO.MemoryStream();
                    Thrift.Transport.TTransport      tp        = new Thrift.Transport.TStreamTransport(null, msWBuffer);
                    Thrift.Protocol.TCompactProtocol cp        = new Thrift.Protocol.TCompactProtocol(tp);

                    switch (pkg.PackType)
                    {
                    case PackType.Command:
                    {
                        cp.WriteI16(PackType.Command);
                        cp.WriteString((string)pkg.Content);
                    }
                    break;

                    case PackType.TaskModel:
                    {
                        cp.WriteI16(PackType.TaskModel);
                        TaskModel task = pkg.Content as TaskModel;
                        task.Write(cp);
                    }
                    break;

                    default:
                        break;
                    }

                    tp.Flush();
                    byte[] buffer = msWBuffer.ToArray();
                    server.Send(pkg.ConnId, buffer, buffer.Length);
                    msWBuffer.Flush();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("发送消息错误:" + ex.Message);
                }
            }
        }
        protected HandleResult OnReceive(IntPtr connId, byte[] bytes)
        {
            System.IO.MemoryStream           ms = new System.IO.MemoryStream(bytes);
            Thrift.Transport.TTransport      tp = new Thrift.Transport.TStreamTransport(ms, null);
            Thrift.Protocol.TCompactProtocol cp = new Thrift.Protocol.TCompactProtocol(tp);
            try
            {
                Package pack     = null;
                short   packType = cp.ReadI16();
                switch (packType)
                {
                case PackType.Command:
                {
                    try
                    {
                        string test = cp.ReadString();
                        pack = new Package()
                        {
                            ConnId = connId, PackType = packType, Content = test
                        };
                    }
                    catch (Thrift.TException ex)
                    {
                        // 包解析错误
                        Console.WriteLine(ex.Message);
                        pack = null;
                    }
                }
                break;

                case PackType.ResultModel:
                {
                    try
                    {
                        ResultModel rm = new ResultModel();
                        rm.Read(cp);
                        pack = new Package()
                        {
                            ConnId = connId, PackType = packType, Content = rm
                        };
                    }
                    catch (Thrift.TException ex)
                    {
                        // 包解析错误
                        Console.WriteLine(ex.Message);
                        pack = null;
                    }
                }
                break;

                case PackType.RequestTask:
                {
                    try
                    {
                        RequestTask task = new RequestTask();
                        task.Read(cp);
                        pack = new Package()
                        {
                            ConnId = connId, PackType = packType, Content = task
                        };
                    }
                    catch (Thrift.TException ex)
                    {
                        // 包解析错误
                        Console.WriteLine(ex.Message);
                        pack = null;
                    }
                }
                break;

                default:
                    pack = null;
                    break;
                }

                if (pack != null)
                {
                    lock (recvLock)
                    {
                        recvQueue.Enqueue(pack);
                    }
                }

                return(HandleResult.Ok);
            }
            catch (Exception ex)
            {
                Console.WriteLine("接收函数异常: " + ex.Message);
                return(HandleResult.Ignore);
            }
        }