44using Thrift ;
55using Thrift . Protocol ;
66using 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 ;
137
148namespace NebulaNet
159{
@@ -23,23 +17,18 @@ public class GraphClient
2317 public GraphClient ( string ip , int port )
2418 {
2519 var tConfiguration = new TConfiguration ( ) ;
20+ socketTransport = new TSocketTransport ( ip , port , tConfiguration ) ;
2621
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 ( ) ;
22+ socketTransport . OpenAsync ( default ) ;
3323
3424 protocolProtocol = new TBinaryProtocol ( socketTransport ) ;
3525
3626 Client = new GraphService . Client ( protocolProtocol ) ;
3727 }
28+
3829 public void Authenticate ( string user , string passwd )
3930 {
40-
41- var authResponse = Client . authenticate ( System . Text . Encoding . ASCII . GetBytes ( user ) , System . Text . Encoding . ASCII . GetBytes ( passwd ) ) . Result ;
42-
31+ var authResponse = Client . authenticate ( System . Text . Encoding . Default . GetBytes ( user ) , System . Text . Encoding . Default . GetBytes ( passwd ) ) . Result ;
4332 if ( authResponse . Error_code != 0 )
4433 {
4534 throw new Exception ( "error auth!" ) ;
@@ -55,42 +44,12 @@ public async Task<ExecutionResponse> Execute(string statement)
5544 var executionResponse = await Client . execute ( sessionId , System . Text . Encoding . Default . GetBytes ( statement ) ) ;
5645 if ( executionResponse . Error_code != 0 )
5746 {
58- throw new Exception ( System . Text . Encoding . UTF8 . GetString ( executionResponse . Error_msg ) + statement + " execute failed." ) ;
47+ throw new Exception ( System . Text . Encoding . ASCII . GetString ( executionResponse . Error_msg ) + statement + " execute failed." ) ;
5948 }
6049
6150 return executionResponse ;
6251 }
6352
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-
9453 public async Task SignOff ( )
9554 {
9655 await Client . signout ( sessionId ) ;
0 commit comments