131 lines
3.5 KiB
C#
131 lines
3.5 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Net.Sockets;
|
|||
|
using System.Net;
|
|||
|
using System.Threading;
|
|||
|
using SafeMobileLib;
|
|||
|
|
|||
|
namespace Safedispatch_4_0
|
|||
|
{
|
|||
|
public class TcpClass
|
|||
|
{
|
|||
|
private TcpClient client;
|
|||
|
private IPEndPoint serverEndPoint;
|
|||
|
private NetworkStream clientStream;
|
|||
|
private string serverIp;
|
|||
|
private int serverPort;
|
|||
|
private bool connected = false;
|
|||
|
private Thread thr=null;
|
|||
|
|
|||
|
public TcpClass()
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public bool Start(string ip, int port)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
serverIp = ip;
|
|||
|
serverPort = port;
|
|||
|
client = new TcpClient();
|
|||
|
serverEndPoint = new IPEndPoint(IPAddress.Parse(serverIp), serverPort);
|
|||
|
client.Connect(serverEndPoint);
|
|||
|
clientStream = client.GetStream();
|
|||
|
|
|||
|
thr = new Thread(new ParameterizedThreadStart(HandleData));
|
|||
|
thr.Start(clientStream);
|
|||
|
connected = true;
|
|||
|
}
|
|||
|
catch(Exception ex)
|
|||
|
{
|
|||
|
SM.Debug("Ex:" + ex.ToString());
|
|||
|
connected = false;
|
|||
|
}
|
|||
|
return connected;
|
|||
|
}
|
|||
|
|
|||
|
public void Stop()
|
|||
|
{
|
|||
|
if (clientStream != null)
|
|||
|
{
|
|||
|
clientStream.Close();
|
|||
|
}
|
|||
|
if (client != null)
|
|||
|
{
|
|||
|
client.Close();
|
|||
|
}
|
|||
|
connected = false;
|
|||
|
}
|
|||
|
|
|||
|
public void Send(byte[] buffer)
|
|||
|
{
|
|||
|
if (connected)
|
|||
|
{
|
|||
|
clientStream.Write(buffer, 0, buffer.Length);
|
|||
|
clientStream.Flush();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void HandleData(object ns)
|
|||
|
{
|
|||
|
NetworkStream nStream = (NetworkStream)ns;
|
|||
|
byte[] message = new byte[128];
|
|||
|
int bytesRead;
|
|||
|
while(MainForm2.isRunning)
|
|||
|
{
|
|||
|
bytesRead = 0;
|
|||
|
try
|
|||
|
{
|
|||
|
//blocks until a message is recevied
|
|||
|
bytesRead = nStream.Read(message, 0, 128);
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
//a socket error has occured
|
|||
|
break;
|
|||
|
}
|
|||
|
if (bytesRead == 0)
|
|||
|
{
|
|||
|
//the client has disconnected from the server
|
|||
|
break;
|
|||
|
}
|
|||
|
//message has successfully been received
|
|||
|
//ASCIIEncoding encoder = new ASCIIEncoding();
|
|||
|
if (this.OnMessageRecv != null)
|
|||
|
{
|
|||
|
this.OnMessageRecv(message, bytesRead);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
nStream.Close();
|
|||
|
if (this.OnConnectionEnded != null)
|
|||
|
{
|
|||
|
this.OnConnectionEnded();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public delegate void MessageRecv(byte[] data, int recv);
|
|||
|
public event MessageRecv OnMessageRecv;
|
|||
|
|
|||
|
public delegate void ConnectionEnded();
|
|||
|
public event ConnectionEnded OnConnectionEnded;
|
|||
|
}
|
|||
|
|
|||
|
/*public struct Recording
|
|||
|
{
|
|||
|
public long id;
|
|||
|
public DateTime startTime;
|
|||
|
public DateTime endTime;
|
|||
|
public string hddLocation;
|
|||
|
public int subID;
|
|||
|
public int gwID;
|
|||
|
public int radioGWID;
|
|||
|
public int typeSD;
|
|||
|
public int calltype;
|
|||
|
}*/
|
|||
|
|
|||
|
}
|