Skip to content

Commit d2c0e8c

Browse files
authored
Merge pull request #11 from zhouyuguangsc/master
Remote IP could not be parsed.
2 parents 0970cb6 + f863c53 commit d2c0e8c

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

NebulaNet/GraphClient.cs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
using Thrift;
55
using Thrift.Protocol;
66
using Thrift.Transport.Client;
7+
using System.Net;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Reflection;
11+
using Nebula.Common;
12+
using System.Threading;
713

814
namespace NebulaNet
915
{
@@ -17,18 +23,23 @@ public class GraphClient
1723
public GraphClient(string ip, int port)
1824
{
1925
var tConfiguration = new TConfiguration();
20-
socketTransport = new TSocketTransport(ip, port, tConfiguration);
2126

22-
socketTransport.OpenAsync(default);
27+
if (IPAddress.TryParse(ip, out IPAddress iPAddress))
28+
socketTransport = new TSocketTransport(iPAddress, port, tConfiguration);
29+
else
30+
socketTransport = new TSocketTransport(ip, port, tConfiguration);
31+
32+
socketTransport.OpenAsync(default).Wait();
2333

2434
protocolProtocol = new TBinaryProtocol(socketTransport);
2535

2636
Client = new GraphService.Client(protocolProtocol);
2737
}
28-
2938
public void Authenticate(string user, string passwd)
3039
{
31-
var authResponse = Client.authenticate(System.Text.Encoding.Default.GetBytes(user), System.Text.Encoding.Default.GetBytes(passwd)).Result;
40+
41+
var authResponse = Client.authenticate(System.Text.Encoding.ASCII.GetBytes(user), System.Text.Encoding.ASCII.GetBytes(passwd)).Result;
42+
3243
if (authResponse.Error_code != 0)
3344
{
3445
throw new Exception("error auth!");
@@ -44,12 +55,42 @@ public async Task<ExecutionResponse> Execute(string statement)
4455
var executionResponse = await Client.execute(sessionId, System.Text.Encoding.Default.GetBytes(statement));
4556
if (executionResponse.Error_code != 0)
4657
{
47-
throw new Exception(System.Text.Encoding.ASCII.GetString(executionResponse.Error_msg) + statement + " execute failed.");
58+
throw new Exception(System.Text.Encoding.UTF8.GetString(executionResponse.Error_msg) + statement + " execute failed.");
4859
}
4960

5061
return executionResponse;
5162
}
5263

64+
public async Task<List<T>> Execute<T>(string statement) where T : class
65+
{
66+
var executionResponse = await Execute(statement);
67+
if (executionResponse.Data == null)
68+
return default;
69+
70+
//获取列名
71+
var columnNames = executionResponse.Data.Column_names
72+
.Select(x => System.Text.Encoding.UTF8.GetString(x).ToLower()).ToArray();
73+
74+
//查找可用属性和数据索引
75+
var indexAndProps = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)//BindingFlags.IgnoreCase
76+
.Select(x => new { Index= Array.IndexOf(columnNames,x.Name.ToLower()),Prop=x })
77+
.Where(x => x.Index >= 0);
78+
79+
//映射对象
80+
var result = new List<T>();
81+
foreach (var row in executionResponse.Data.Rows)
82+
{
83+
var o = Activator.CreateInstance<T>();
84+
foreach (var item in indexAndProps)
85+
{
86+
item.Prop.SetValue(o, row.Values[item.Index].GetValue(item.Prop.PropertyType));
87+
}
88+
result.Add(o);
89+
}
90+
91+
return result;
92+
}
93+
5394
public async Task SignOff()
5495
{
5596
await Client.signout(sessionId);

NebulaNet/NebulaNetExtensions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Nebula.Common
6+
{
7+
internal static class NebulaNetExtensions
8+
{
9+
internal static object GetValue(this @Value value, Type targetType)
10+
{
11+
switch (Type.GetTypeCode(targetType))
12+
{
13+
case TypeCode.Boolean:
14+
return value.BVal;
15+
case TypeCode.Int32:
16+
return (int)value.IVal;
17+
case TypeCode.Int64:
18+
return value.IVal;
19+
case TypeCode.Double:
20+
return value.FVal;
21+
case TypeCode.String:
22+
return System.Text.Encoding.UTF8.GetString(value.SVal);
23+
case TypeCode.DateTime:
24+
return value.DtVal;
25+
default:
26+
return default;
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)