Пример #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);
                }
            }
        }