-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCConnectionManager.cs
More file actions
144 lines (116 loc) · 4.48 KB
/
Copy pathCConnectionManager.cs
File metadata and controls
144 lines (116 loc) · 4.48 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Ground_Floor
{
static class CConnectionConstants
{
public const int Error_None = 0;
public const int Error_ServerNotAvailable = 1;
public const int Error_ConnectionRefused = 2;
public const int Error_ConnectionTimeout = 3;
public const int Error_NotConnected = 3;
}
class CConnectionManager
{
private CWinSock _Socket;
private CMessage _Message;
private bool _bConnected;
private string sServerAddress;
private int iEntrancePort;
public CConnectionManager(string ServerAddress, int EntrancePort)
{
sServerAddress = ServerAddress;
iEntrancePort = EntrancePort;
}
public int ConnectToServer()
{
int iReturn;
byte[] bRcvd = new byte[124];
bool bConnectionMessageReceived = false;
bool bConnectionGranted = false;
bool bTimeOutElapsed = false;
int StartTime;
_bConnected = false;
_Socket = new CWinSock(sServerAddress,iEntrancePort);
iReturn = 0;
if (_Socket.Connect())
{
//_Message = new CMessage();
//_Message.SetTextMessage(CMessageConstants.CMD_TXT_ASK_FOR_CONNECTION, sDeviceID.ToCharArray(), 0);
//_Socket.SendByte(_Message.CreateFrame());
//Thread.Sleep(250);
//StartTime = DateTime.Now.Millisecond;
//while ((!bConnectionMessageReceived) && (!bTimeOutElapsed))
//{
// bRcvd = _Socket.ReceiveByte();
// if (_Message.SetMessageFromBuffer(bRcvd, 0))
// {
// Console.WriteLine("Message received : " + _Message.Command_Type().ToString());
// if (_Message.Command_Type() == CMessageConstants.CMD_TYPE_COMMUNICATION)
// {
// bConnectionMessageReceived = true;
// if (_Message.Command_Id() == CMessageConstants.CMD_COMM_CONNECTION_GRANTED)
// {
// Console.WriteLine("Connection Cranted.");
// _Socket.Disconnect();
// _Socket.setPort(_Message.GetPrmInt(0));
// _Socket.Connect();
// bConnectionGranted = true;
// iReturn = 0;
// }
// else
// {
// bConnectionGranted = false;
// }
// }
// Thread.Sleep(250);
// }
// if ((DateTime.Now.Millisecond - StartTime) > 5000) bTimeOutElapsed = true;
//}
//if ((!bConnectionGranted) && (!bTimeOutElapsed))
// iReturn = CConnectionConstants.Error_ConnectionRefused;
//if (bTimeOutElapsed)
// iReturn = CConnectionConstants.Error_ConnectionTimeout;
}
else
{
iReturn = CConnectionConstants.Error_ServerNotAvailable;
}
if (iReturn == CConnectionConstants.Error_None) _bConnected = true;
return iReturn;
}
public bool IsConnected()
{
return _bConnected;
}
public int SendMessage(CMessage MsgToSend)
{
int iReturn = CConnectionConstants.Error_None;
iReturn = this.ConnectToServer();
if (!_bConnected)
{
iReturn = CConnectionConstants.Error_NotConnected;
}
else
{
_Socket.SendByte(MsgToSend.CreateFrame());
}
_Socket.Disconnect();
return iReturn;
}
public CMessage ReceiveMessage()
{
CMessage MessageReturn = null;
byte[] bRcvd = new byte[124];
if (_bConnected)
{
MessageReturn = new CMessage();
bRcvd = _Socket.ReceiveByte();
if (!MessageReturn.SetMessageFromBuffer(bRcvd, 0)) MessageReturn = null;
}
return MessageReturn;
}
}
}