abc
This commit is contained in:
6
BeWoChatServer/App.config
Normal file
6
BeWoChatServer/App.config
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
|
||||
</startup>
|
||||
</configuration>
|
||||
89
BeWoChatServer/BeWoChatServer.csproj
Normal file
89
BeWoChatServer/BeWoChatServer.csproj
Normal file
@@ -0,0 +1,89 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{4E069DC0-8153-41E0-A89A-FC1916DF4A5F}</ProjectGuid>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>BeWoChatServer</RootNamespace>
|
||||
<AssemblyName>BeWoChatServer</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<StartupObject />
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\Lib\log4net.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="NHibernate, Version=3.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\Lib\NHibernate.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.ServiceModel" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="WindowsBase" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Packet.cs" />
|
||||
<Compile Include="Server.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config">
|
||||
<SubType>Designer</SubType>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Data\Data.csproj">
|
||||
<Project>{b0d73e3d-4ae7-4024-93a6-db1f46d7ccee}</Project>
|
||||
<Name>Data</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Service\Service.csproj">
|
||||
<Project>{094331c3-ecee-4c89-bbfd-4c9ded89f0ef}</Project>
|
||||
<Name>Service</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Shared\Shared.csproj">
|
||||
<Project>{2f50b83d-a3f0-4ec4-979a-3f9b7e3d8ed4}</Project>
|
||||
<Name>Shared</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
82
BeWoChatServer/Packet.cs
Normal file
82
BeWoChatServer/Packet.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
using BS.Shared;
|
||||
|
||||
namespace BeWoChatServer
|
||||
{
|
||||
public class Packet
|
||||
{
|
||||
public Socket Socket { get; set; }
|
||||
public int ClientNumber { get; set; }
|
||||
public string ClientName { get; set; }
|
||||
public long SenderPersonOid { get; set; }
|
||||
public long RecipientPersonOid { get; set; }
|
||||
public DataIdentifier DataIdentifier { get; set; }
|
||||
public string ChatMessage { get; set; }
|
||||
|
||||
public byte[] DataBuffer = new byte[1024];
|
||||
|
||||
public Packet(Socket pSocket, int pClientNumber, string pClientName, long pSenderPersonOid, long pRecipientPersonOid)
|
||||
{
|
||||
Socket = pSocket;
|
||||
ClientNumber = pClientNumber;
|
||||
ClientName = pClientName;
|
||||
SenderPersonOid = pSenderPersonOid;
|
||||
RecipientPersonOid = pRecipientPersonOid;
|
||||
}
|
||||
|
||||
// 0 4 8 12 20 28
|
||||
// Description -> |dataIdentifier|name length|message length|sender person oid|recipient person oid| name | message |
|
||||
// Size in bytes -> | 4 | 4 | 4 | 8 | 8 |name length|message length|
|
||||
public Packet(byte[] dataStream)
|
||||
{
|
||||
DataIdentifier = (DataIdentifier)BitConverter.ToInt32(dataStream, 0);
|
||||
|
||||
var nameLength = BitConverter.ToInt32(dataStream, 4);
|
||||
|
||||
var messageLength = BitConverter.ToInt32(dataStream, 8);
|
||||
|
||||
SenderPersonOid = BitConverter.ToInt64(dataStream, 12);
|
||||
|
||||
RecipientPersonOid = BitConverter.ToInt64(dataStream, 20);
|
||||
|
||||
ClientName = nameLength > 0 ? Encoding.UTF8.GetString(dataStream, 28, nameLength) : null;
|
||||
|
||||
ChatMessage = messageLength > 0 ? Encoding.UTF8.GetString(dataStream, 28 + nameLength, messageLength) : null;
|
||||
}
|
||||
|
||||
public void GetData(string message)
|
||||
{
|
||||
//Aufbau Message:
|
||||
// die ersten fünf ";" ignorieren
|
||||
// SenderOid;RecipientOid;ClientNameLength;MessageLength;ChatDataIdentifier;ClientNameChatMessage
|
||||
|
||||
//23;281;20;389;1; 5 Semikola
|
||||
|
||||
var gesplittet = message.Split(';');
|
||||
|
||||
SenderPersonOid = Convert.ToInt64(gesplittet[0]);
|
||||
RecipientPersonOid = Convert.ToInt64(gesplittet[1]);
|
||||
|
||||
var chatNameLength = Convert.ToInt32(gesplittet[2]);
|
||||
var chatMessageLength = Convert.ToInt32(gesplittet[3]);
|
||||
|
||||
var headerLength = gesplittet[0].Length + gesplittet[1].Length + gesplittet[2].Length + gesplittet[3].Length + 5;
|
||||
var actualMessage = message.Substring(headerLength - 1);
|
||||
|
||||
ClientName = actualMessage.Substring(0, chatNameLength);
|
||||
ChatMessage = actualMessage.Substring(chatNameLength);
|
||||
}
|
||||
|
||||
// Java kennt nur unsigned bytes (-127 bis 128)
|
||||
public byte[] GetDataStream()
|
||||
{
|
||||
var result = "";
|
||||
|
||||
return Encoding.UTF8.GetBytes(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
36
BeWoChatServer/Properties/AssemblyInfo.cs
Normal file
36
BeWoChatServer/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// Allgemeine Informationen über eine Assembly werden über die folgenden
|
||||
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
|
||||
// die mit einer Assembly verknüpft sind.
|
||||
[assembly: AssemblyTitle("BeWoChatServer")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("BeWoChatServer")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2016")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
|
||||
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
|
||||
// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
|
||||
[assembly: Guid("ae6a4233-d1b0-4c13-bd70-5f1e3f60b4e7")]
|
||||
|
||||
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
|
||||
//
|
||||
// Hauptversion
|
||||
// Nebenversion
|
||||
// Buildnummer
|
||||
// Revision
|
||||
//
|
||||
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
|
||||
// übernehmen, indem Sie "*" eingeben:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
168
BeWoChatServer/Server.cs
Normal file
168
BeWoChatServer/Server.cs
Normal file
@@ -0,0 +1,168 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using BeWo.Data;
|
||||
using BeWo.Data.Access;
|
||||
using BeWo.Data.Entities;
|
||||
|
||||
using BS.Shared;
|
||||
|
||||
namespace BeWoChatServer
|
||||
{
|
||||
public class Server
|
||||
{
|
||||
private static object _Lock = string.Empty;
|
||||
|
||||
private static readonly Dictionary<Socket, long> clientList = new Dictionary<Socket, long>();
|
||||
protected static Socket serverSocket;
|
||||
|
||||
private static AsyncCallback asyncCallback;
|
||||
|
||||
private static int clientCount;
|
||||
|
||||
static Packet dataPacketsdf;
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Benutzt den DefaultHibernateSessionManager
|
||||
DefaultHibernateSessionManager.CurrentTenant = "demo";
|
||||
DefaultHibernateSessionManager.OpenSession();
|
||||
var test = DAOFactory.GenericDAO.LoadByID<ChatMessage>(1);
|
||||
|
||||
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
var server = new IPEndPoint(IPAddress.Any, 5000);
|
||||
|
||||
serverSocket.Bind(server);
|
||||
serverSocket.Listen(4);
|
||||
|
||||
serverSocket.BeginAccept(OnClientConnected, null);
|
||||
|
||||
while (true) { }
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.WriteLine("ChatServerException: " + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Datenbankverbindung herstellen
|
||||
|
||||
public static void OnClientConnected(IAsyncResult asyncResult)
|
||||
{
|
||||
try
|
||||
{
|
||||
var workerSocket = serverSocket.EndAccept(asyncResult);
|
||||
|
||||
Interlocked.Increment(ref clientCount);
|
||||
|
||||
clientList.Add(workerSocket, 0);
|
||||
|
||||
WaitForClientData(workerSocket, clientCount, "LEER", 0, 0);
|
||||
|
||||
serverSocket.BeginAccept(OnClientConnected, null);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
Debug.WriteLine("ChatServerException: " + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void WaitForClientData(Socket socket, int clientNumber, string clientName, long oid, long recipientPersonOid)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (asyncCallback == null)
|
||||
{
|
||||
asyncCallback = OnReceiveData;
|
||||
}
|
||||
|
||||
var packet = new Packet(socket, clientNumber, clientName, oid, recipientPersonOid);
|
||||
|
||||
socket.BeginReceive(packet.DataBuffer, 0, packet.DataBuffer.Length, SocketFlags.None, asyncCallback, packet);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.WriteLine("ChatServerException: " + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void OnReceiveData(IAsyncResult asyncResult)
|
||||
{
|
||||
var dataPacket = (Packet) asyncResult.AsyncState;
|
||||
|
||||
try
|
||||
{
|
||||
var responseByteCount = dataPacket.Socket.EndReceive(asyncResult);
|
||||
|
||||
var chars = new char[responseByteCount];
|
||||
|
||||
var decoder = Encoding.UTF8.GetDecoder();
|
||||
decoder.GetChars(dataPacket.DataBuffer, 0, responseByteCount, chars, 0);
|
||||
|
||||
if(chars.Count(f => f.Equals(';')) >= 4)
|
||||
{
|
||||
var message = new string(chars);
|
||||
|
||||
dataPacket.GetData(message);
|
||||
}
|
||||
|
||||
// Nur bei DataIdentifier.Login
|
||||
//TODO: SenderOid im Dictionary speichern
|
||||
switch(dataPacket.DataIdentifier) { case DataIdentifier.Login:
|
||||
var endPoint = dataPacket.Socket.RemoteEndPoint;
|
||||
|
||||
var endPointCollection = clientList.Select(s => s.Key.RemoteEndPoint).ToList();
|
||||
|
||||
var istEnthalten = endPointCollection.Contains(endPoint);
|
||||
if(istEnthalten)
|
||||
{
|
||||
clientList[dataPacket.Socket] = dataPacket.SenderPersonOid;
|
||||
}
|
||||
break;
|
||||
case DataIdentifier.Message:
|
||||
if(responseByteCount == 0)
|
||||
{
|
||||
clientList.Remove(dataPacket.Socket);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
Monitor.Enter(_Lock);
|
||||
|
||||
DefaultHibernateSessionManager.CurrentTenant = "demo";
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
Monitor.Exit(_Lock);
|
||||
}
|
||||
|
||||
|
||||
// in DB speichern und an evtl. an den Empfänger schicken, sofern dieser in der clientList enthalten ist
|
||||
}
|
||||
|
||||
break;
|
||||
case DataIdentifier.Logout:
|
||||
break;
|
||||
}
|
||||
|
||||
WaitForClientData(dataPacket.Socket, dataPacket.ClientNumber, dataPacket.ClientName, dataPacket.SenderPersonOid, dataPacket.RecipientPersonOid);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.WriteLine("ChatServerException: " + e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user