#
luxiaotao1123
2020-08-04 6abc20e29568c129f4ca71eccec9310534a8c779
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.zy.common.HslCommunication.Enthernet.SimplifyNet;
 
import com.zy.common.HslCommunication.Core.IMessage.HslMessage;
import com.zy.common.HslCommunication.Core.Net.HslProtocol;
import com.zy.common.HslCommunication.Core.Net.NetHandle;
import com.zy.common.HslCommunication.Core.Net.NetworkBase.NetworkDoubleBase;
import com.zy.common.HslCommunication.Core.Transfer.RegularByteTransform;
import com.zy.common.HslCommunication.Core.Types.OperateResultExOne;
import com.zy.common.HslCommunication.Utilities;
 
import java.util.UUID;
 
 
/**
 * 同步访问的网络客户端
 */
public class NetSimplifyClient extends NetworkDoubleBase<HslMessage,RegularByteTransform>
{
 
 
    /**
     * 实例化一个客户端的对象,用于和服务器通信
     * @param ipAddress Ip地址
     * @param port 端口号
     */
    public NetSimplifyClient(String ipAddress, int port)
    {
        this.setIpAddress(ipAddress);
        this.setPort( port);
    }
 
    /**
     * 实例化一个客户端的对象,用于和服务器通信
     * @param ipAddress Ip地址
     * @param port 端口号
     * @param token 令牌
     */
    public NetSimplifyClient(String ipAddress, int port, UUID token)
    {
        this.setIpAddress(ipAddress);
        this.setPort( port);
        this.Token = token;
    }
 
 
    /**
     * 实例化一个客户端对象,需要手动指定Ip地址和端口
     */
    public NetSimplifyClient()
    {
 
    }
 
 
    /**
     * 客户端向服务器进行请求,请求字符串数据
     * @param customer 用户的指令头
     * @param send 发送数据
     * @return 带结果说明的对象
     */
    public OperateResultExOne<String> ReadFromServer(NetHandle customer, String send) {
        OperateResultExOne<String> result = new OperateResultExOne<String>();
        byte[] data = send.isEmpty() ? new byte[0] : Utilities.string2Byte(send);
        OperateResultExOne<byte[]> temp = ReadFromServerBase(HslProtocol.ProtocolUserString, customer.get_CodeValue(), data);
        result.IsSuccess = temp.IsSuccess;
        result.ErrorCode = temp.ErrorCode;
        result.Message = temp.Message;
        if (temp.IsSuccess) {
            result.Content = Utilities.byte2String(temp.Content);
        }
        temp = null;
        return result;
    }
 
 
    /**
     * 客户端向服务器进行请求,请求字节数据
     * @param customer 用户的指令头
     * @param send 发送的字节内容
     * @return 带结果说明的对象
     */
    public OperateResultExOne<byte[]> ReadFromServer(NetHandle customer, byte[] send) {
        return ReadFromServerBase(HslProtocol.ProtocolUserBytes, customer.get_CodeValue(), send);
    }
 
 
    /**
     * 需要发送的底层数据
     * @param headCode 数据的指令头
     * @param customer 需要发送的底层数据
     * @param send 需要发送的底层数据
     * @return 带结果说明的对象
     */
    private OperateResultExOne<byte[]> ReadFromServerBase(int headCode, int customer, byte[] send) {
        OperateResultExOne<byte[]> read = ReadFromCoreServer(HslProtocol.CommandBytes(headCode, customer, Token, send));
        if (!read.IsSuccess) {
            return read;
        }
 
        if(read.Content.length < HslProtocol.HeadByteLength){
            read.IsSuccess = false;
            read.Message = "length is not correct :" +read.Content.length;
            return read;
        }
 
        byte[] headBytes = new byte[HslProtocol.HeadByteLength];
        byte[] contentBytes = new byte[read.Content.length - HslProtocol.HeadByteLength];
 
        System.arraycopy(read.Content,0,headBytes,0,HslProtocol.HeadByteLength);
        if (contentBytes.length > 0) {
            System.arraycopy(read.Content, HslProtocol.HeadByteLength, contentBytes, 0, read.Content.length - HslProtocol.HeadByteLength);
        }
 
        contentBytes = HslProtocol.CommandAnalysis(headBytes, contentBytes);
        return OperateResultExOne.CreateSuccessResult(contentBytes);
    }
 
 
 
    /**
     * 获取本对象的字符串表示形式
     * @return 字符串信息
     */
    @Override
    public String toString() {
        return "NetSimplifyClient";
    }
 
 
}