forked from Hansoft/HansoftExport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SessionManager.cs
116 lines (100 loc) · 3.01 KB
/
SessionManager.cs
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
using HPMSdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HPMInt8 = System.SByte;
using HPMUInt8 = System.Byte;
using HPMInt16 = System.Int16;
using HPMUInt16 = System.UInt16;
using HPMInt32 = System.Int32;
using HPMUInt32 = System.UInt32;
using HPMInt64 = System.Int64;
using HPMUInt64 = System.UInt64;
using HPMError = System.Int32;
using HPMString = System.String;
namespace Hansoft.SdkUtils
{
class SessionManager
{
private static SessionManager instance = null;
private string sdkUser;
private string sdkUserPwd;
private string server;
private int port;
private string database;
HPMSdkSession hpmSession;
HPMSdkCallback hpmCallback;
public static void Initialize(string sdkUser, string sdkUserPwd, string server, int port, string database)
{
if (instance != null)
instance.CloseSession();
instance = new SessionManager(sdkUser, sdkUserPwd, server, port, database);
}
private SessionManager()
{
}
public static SessionManager Instance
{
get
{
if (instance == null)
instance = new SessionManager();
return instance;
}
}
private SessionManager(string sdkUser, string sdkUserPwd, string server, int port, string database)
{
this.sdkUser = sdkUser;
this.sdkUserPwd = sdkUserPwd;
this.server = server;
this.port = port;
this.database = database;
hpmCallback = new HPMSdkCallback();
}
public void CloseSession()
{
if (hpmSession != null)
HPMSdkSession.SessionDestroy(ref hpmSession);
}
public bool Connected
{
get
{
return hpmSession != null;
}
}
public bool Connect()
{
if (hpmSession == null)
{
try
{
hpmSession = HPMSdkSession.SessionOpen(server, port, database, sdkUser, sdkUserPwd, hpmCallback, null, true, EHPMSdkDebugMode.Debug, (IntPtr)null, 0, "", "", null);
Logger.Information("Connection established");
}
catch (Exception e)
{
Logger.Exception("Could not connect to Hansoft Server", e);
}
}
return hpmSession!=null;
}
public HPMSdkSession Session
{
get
{
if (hpmSession == null)
Connect();
return hpmSession;
}
}
class HPMSdkCallback : HPMSdkCallbacks
{
public override void On_ProcessError(EHPMError error)
{
Logger.Error("On_ProcessError: " + HPMSdkSession.ErrorToStr(error));
}
}
}
}