Skip to content

Commit

Permalink
Server 建立Connect完成
Browse files Browse the repository at this point in the history
将继续书写Process函数,完成信息处理
  • Loading branch information
DeckerCHAN committed Sep 1, 2013
1 parent 8be6580 commit 67f3d86
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 104 deletions.
69 changes: 69 additions & 0 deletions ServerCore/Connect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GPSServer.ServerCore.Connect
{
internal class Connect : IDisposable
{
private Thread SessionThread = null;
public string SessionIp { get; private set; }
public DateTime LastActive { get; private set; }

public Connect(string ip, byte[] initBuffer)
{
SessionIp = ip;
LastActive = DateTime.Now;
}

public void Dispose()
{
this.SessionThread.Abort();
this.SessionThread = null;
this.SessionIp = null;
GC.Collect();
}

public byte[] ProcessMessege(byte [] buffer)
{
return null;
}

}

internal class ConnectList : List<Connect>
{
public ConnectList():base()
{

}
/// <summary>
/// 尝试添加一个新链接,如果链接已经存在则返回已存在的链接
/// </summary>
/// <param name="ip">链接的IP地址和端口</param>
/// <param name="initBuffer">识别数据流</param>
/// <returns>一个链接</returns>
public Connect AddConnect(string ip, byte[] initBuffer)
{
foreach (var connect in this)
{
if (ip == connect.SessionIp)
{
return connect;
}
}
var newConnect = new Connect(ip, initBuffer);
this.Add(newConnect);
return newConnect;
}





}
}
28 changes: 0 additions & 28 deletions ServerCore/Devices.cs

This file was deleted.

2 changes: 1 addition & 1 deletion ServerCore/Protocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Protocol(string protocolName)
}

public Symbol[] SymbolList { get; set; }
public string ProtocolName { [UsedImplicitly] private get; set; }
public string ProtocolName { private get; set; }
}

internal struct Symbol
Expand Down
110 changes: 35 additions & 75 deletions ServerCore/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
using System.Net;
using System.Text;
using System.Threading;
using GPSServer.ServerCore.Connect;
using LumiSoft.Net;
using LumiSoft.Net.IO;
using LumiSoft.Net.TCP;


namespace GPSServer.ServerCore
{
public class ServerEventArgs : EventArgs
Expand All @@ -25,24 +27,26 @@ public override string ToString()

public class Server
{
private readonly TCP_Server<TCP_ServerSession> Core;
private TCP_Server<TCP_ServerSession> _core;
private ConnectList _connects;
private Object DataBase;

//TODO:通过GET和SET加入对服务器的控制!
//TODO:控制服务器ProcessMessege返回值

public Server(int port)
{
Core = new TCP_Server<TCP_ServerSession>();
_core = new TCP_Server<TCP_ServerSession>();
_connects = new ConnectList();

try
{
Core.Started += (i, o) => OnServerStarted();
Core.Stopped += (i, o) => OnServerStoped();
Core.Error += CoreError;
Core.SessionCreated += ProcessMessege;
_core.Started += (i, o) => OnServerStarted();
_core.Stopped += (i, o) => OnServerStoped();
_core.Error += CoreError;
_core.SessionCreated += ProcessMessege;

Core.Bindings = new[]
{new IPBindInfo(Dns.GetHostEntry(String.Empty).HostName, BindInfoProtocol.TCP, IPAddress.Any, port)};
_core.Bindings = new[] { new IPBindInfo(Dns.GetHostEntry(String.Empty).HostName, BindInfoProtocol.TCP, IPAddress.Any, port) };
}
catch (Exception ex)
{
Expand All @@ -57,7 +61,10 @@ private void CoreError(object sender, Error_EventArgs e)
private void ProcessMessege(object sender, TCP_ServerSessionEventArgs<TCP_ServerSession> e)
{
//TODO:将获得的内容放入
OnMessegeProcessed(e.Session.ConnectTime + "Start Connect!");
OnMessegeProcessed(e.Session.ConnectTime + "Connect Established!");



new Thread(argSession =>
{
try
Expand All @@ -67,79 +74,32 @@ private void ProcessMessege(object sender, TCP_ServerSessionEventArgs<TCP_Server

var char16 = new StringBuilder();
char16.Append(" FROM: " + session.RemoteEndPoint.ToString() + " MSG:");

//var buffer = new byte[16384];
//msg.Read(buffer, 0, 16384);


//foreach (var t in buffer)
//{
// if (t > 0)
// {
// char16.Append(Convert.ToString(t, 16).ToUpper().PadLeft(2, '0') + " ");
// }
// else
// {
// break;
// }
//}


//while (true)
//{
// var temp = msg.ReadByte();
// if (temp >= 0)
// {
// char16.Append(Convert.ToString(temp, 16).ToUpper().PadLeft(2, '0') + " ");
// }
// else
// {
// break;
// }
//}
byte[] buffer;
int repPoint = 0;
DateTime lastPoint = DateTime.Now;
while (true)
{
buffer = new byte[16384];
msg.Read(buffer, 0, 16384);


if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0 && buffer[3] == 0 && buffer[4] == 0)
char16 = new StringBuilder();
char16.Append(" FROM: " + session.RemoteEndPoint.ToString() + " MSG:");
foreach (byte t in buffer)
{
if (repPoint > 5)
if (t > 0)
{
OnMessegeProcessed(session.ConnectTime + "Disconnect");
session.Disconnect();
break;
char16.Append(Convert.ToString(t, 16).ToUpper().PadLeft(2, '0') + " ");
}
if ((DateTime.Now - lastPoint).Ticks < 1000000)
else
{
repPoint++;
}
lastPoint = DateTime.Now;
}
else
{
char16 = new StringBuilder();
char16.Append(" FROM: " + session.RemoteEndPoint.ToString() + " MSG:");
foreach (byte t in buffer)
{
if (t > 0)
{
char16.Append(Convert.ToString(t, 16).ToUpper().PadLeft(2, '0') + " ");
}
else
{
break;
}
break;
}
session.TcpStream.Write(
new byte[] {0x78, 0x78, 0x05, 0x01, 0x00, 0x01, 0xd9, 0xdc, 0x0d, 0x0a}, 0, 10);
session.TcpStream.Flush();
OnMessegeProcessed(session.ConnectTime + char16.ToString());
}
var connsct = this._connects.AddConnect(session.RemoteEndPoint.ToString(), buffer);
var res = connsct.ProcessMessege(buffer);
session.TcpStream.Write(res, 0, res.Length);
session.TcpStream.Flush();
OnMessegeProcessed(session.ConnectTime + char16.ToString());

}

// e.Session.TcpStream
Expand Down Expand Up @@ -168,19 +128,19 @@ private void ProcessMessege(object sender, TCP_ServerSessionEventArgs<TCP_Server

public void StatrServer()
{
if (!Core.IsRunning)
if (!_core.IsRunning)
{
Core.Start();
_core.Start();
}
else
{
Core.Restart();
_core.Restart();
}
}

public void StopServer()
{
Core.Stop();
_core.Stop();
}

#endregion
Expand All @@ -202,8 +162,8 @@ protected virtual void OnServerStarted()
EventHandler handler = ServerStarted;
if (handler != null)
handler(this,
new ServerEventArgs(Core.Bindings[0].HostName + " " + Core.Bindings[0].Port + " " +
Core.Bindings[0].IP));
new ServerEventArgs(_core.Bindings[0].HostName + " " + _core.Bindings[0].Port + " " +
_core.Bindings[0].IP));
}

public event EventHandler ServerStoped = null;
Expand Down

0 comments on commit 67f3d86

Please sign in to comment.