├── .travis.yml
├── FireLite.Samples.JsonPacketServer.Core
├── packages.config
├── Network
│ ├── Enums
│ │ └── OpCode.cs
│ ├── Interfaces
│ │ └── IPacket.cs
│ └── Packets
│ │ ├── Packet.cs
│ │ ├── HelloPacket.cs
│ │ └── PacketLookup.cs
├── Exceptions
│ └── IllegalPacketException.cs
├── Extensions
│ └── ByteArrayExtensions.cs
├── Properties
│ └── AssemblyInfo.cs
└── FireLite.Samples.JsonPacketServer.Core.csproj
├── FireLite.Samples.JsonPacketServer.Client
├── packages.config
├── App.config
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
├── Client.cs
└── FireLite.Samples.JsonPacketServer.Client.csproj
├── FireLite.Samples.JsonPacketServer.Server
├── packages.config
├── App.config
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
├── JsonPacketServer.cs
└── FireLite.Samples.JsonPacketServer.Server.csproj
├── FireLite.Core
├── Interfaces
│ ├── IServer.cs
│ ├── IClientConnection.cs
│ └── IClient.cs
├── Extensions
│ ├── StringExtensions.cs
│ ├── ByteArrayExtensions.cs
│ └── NetworkStreamExtensions.cs
├── Exceptions
│ ├── ConnectionException.cs
│ └── FireLiteException.cs
├── FireLite.Core.nuspec
├── Properties
│ └── AssemblyInfo.cs
├── Network
│ ├── ClientConnection.cs
│ ├── Client.cs
│ └── Server.cs
└── FireLite.Core.csproj
├── FireLite.Samples.SimpleServer.Client
├── App.config
├── SimpleClient.cs
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
└── FireLite.Samples.SimpleServer.Client.csproj
├── FireLite.Samples.SimpleServer.Server
├── App.config
├── Program.cs
├── SimpleServer.cs
├── Properties
│ └── AssemblyInfo.cs
└── FireLite.Samples.SimpleServer.Server.csproj
├── LICENSE
├── .gitignore
├── FireLite.sln
└── README.md
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: csharp
2 | solution: FireLite.sln
3 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Core/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Client/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Core/Network/Enums/OpCode.cs:
--------------------------------------------------------------------------------
1 | namespace FireLite.Samples.JsonPacketServer.Core.Network.Enums
2 | {
3 | public enum OpCode
4 | {
5 | Hello
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Server/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/FireLite.Core/Interfaces/IServer.cs:
--------------------------------------------------------------------------------
1 | namespace FireLite.Core.Interfaces
2 | {
3 | public interface IServer
4 | {
5 | int Port { get; set; }
6 |
7 | void Start();
8 | void Stop();
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Client/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Server/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/FireLite.Samples.SimpleServer.Client/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/FireLite.Samples.SimpleServer.Server/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/FireLite.Core/Interfaces/IClientConnection.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace FireLite.Core.Interfaces
4 | {
5 | public interface IClientConnection
6 | {
7 | Guid Id { get; }
8 |
9 | void Disconnect();
10 | void SendPacket(byte[] packetBytes);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Core/Network/Interfaces/IPacket.cs:
--------------------------------------------------------------------------------
1 | using FireLite.Samples.JsonPacketServer.Core.Network.Enums;
2 |
3 | namespace FireLite.Samples.JsonPacketServer.Core.Network.Interfaces
4 | {
5 | public interface IPacket
6 | {
7 | OpCode OpCode { get; }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/FireLite.Core/Interfaces/IClient.cs:
--------------------------------------------------------------------------------
1 | namespace FireLite.Core.Interfaces
2 | {
3 | public interface IClient
4 | {
5 | string Host { get; set; }
6 | int Port { get; set; }
7 |
8 | bool Connect();
9 | void Disconnect();
10 | void SendPacket(byte[] packetBytes);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/FireLite.Core/Extensions/StringExtensions.cs:
--------------------------------------------------------------------------------
1 | using System.Text;
2 |
3 | namespace FireLite.Core.Extensions
4 | {
5 | public static class StringExtensions
6 | {
7 | public static byte[] GetBytes(this string str)
8 | {
9 | var encoder = new UTF8Encoding();
10 | return encoder.GetBytes(str);
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Core/Network/Packets/Packet.cs:
--------------------------------------------------------------------------------
1 | using FireLite.Samples.JsonPacketServer.Core.Network.Enums;
2 | using FireLite.Samples.JsonPacketServer.Core.Network.Interfaces;
3 |
4 | namespace FireLite.Samples.JsonPacketServer.Core.Network.Packets
5 | {
6 | public class Packet : IPacket
7 | {
8 | public OpCode OpCode { get; set; }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/FireLite.Core/Extensions/ByteArrayExtensions.cs:
--------------------------------------------------------------------------------
1 | using System.Text;
2 |
3 | namespace FireLite.Core.Extensions
4 | {
5 | public static class ByteArrayExtensions
6 | {
7 | public static string GetString(this byte[] bytes)
8 | {
9 | var encoder = new UTF8Encoding();
10 | return encoder.GetString(bytes);
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Core/Exceptions/IllegalPacketException.cs:
--------------------------------------------------------------------------------
1 | using FireLite.Core.Exceptions;
2 |
3 | namespace FireLite.Samples.JsonPacketServer.Core.Exceptions
4 | {
5 | public class IllegalPacketException : FireLiteException
6 | {
7 | public IllegalPacketException() { }
8 | public IllegalPacketException(string message) : base(message) { }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Server/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace FireLite.Samples.JsonPacketServer.Server
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | var server = new JsonPacketServer(1337);
10 | server.Start();
11 | Console.ReadKey();
12 | server.Stop();
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/FireLite.Samples.SimpleServer.Server/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace FireLite.Samples.SimpleServer.Server
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | var server = new SimpleServer(1337);
10 | server.Start();
11 | Console.ReadKey();
12 | server.Stop();
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/FireLite.Core/Exceptions/ConnectionException.cs:
--------------------------------------------------------------------------------
1 | namespace FireLite.Core.Exceptions
2 | {
3 | public class ConnectionException : FireLiteException
4 | {
5 | public ConnectionException() : base("The client disconnected unexpectedly")
6 | {
7 |
8 | }
9 |
10 | public ConnectionException(string message) : base(message)
11 | {
12 |
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Core/Network/Packets/HelloPacket.cs:
--------------------------------------------------------------------------------
1 | using FireLite.Samples.JsonPacketServer.Core.Network.Enums;
2 |
3 | namespace FireLite.Samples.JsonPacketServer.Core.Network.Packets
4 | {
5 | public class HelloPacket : Packet
6 | {
7 | public string Message { get; set; }
8 |
9 | public HelloPacket()
10 | {
11 | OpCode = OpCode.Hello;
12 | }
13 |
14 | public HelloPacket(string message) : this()
15 | {
16 | Message = message;
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Core/Network/Packets/PacketLookup.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using FireLite.Samples.JsonPacketServer.Core.Network.Enums;
4 |
5 | namespace FireLite.Samples.JsonPacketServer.Core.Network.Packets
6 | {
7 | public static class PacketLookup
8 | {
9 | public static IDictionary OpCodeToPacketType = new Dictionary()
10 | {
11 | {
12 | OpCode.Hello, typeof (HelloPacket)
13 | }
14 | };
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/FireLite.Core/Exceptions/FireLiteException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace FireLite.Core.Exceptions
4 | {
5 | public class FireLiteException : Exception
6 | {
7 | public override string Message
8 | {
9 | get { return message; }
10 | }
11 |
12 | private readonly string message;
13 |
14 | public FireLiteException()
15 | {
16 | }
17 |
18 | public FireLiteException(string message)
19 | {
20 | this.message = message;
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/FireLite.Samples.SimpleServer.Client/SimpleClient.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using FireLite.Core.Extensions;
3 |
4 | namespace FireLite.Samples.SimpleServer.Client
5 | {
6 | public class SimpleClient : Core.Network.Client
7 | {
8 | public SimpleClient(string host, int port) : base(host, port) { }
9 |
10 | protected override void OnPacketReceived(byte[] packetBytes)
11 | {
12 | var packetString = packetBytes.GetString();
13 |
14 | Console.WriteLine("Got packet: {0}", packetString);
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/FireLite.Core/FireLite.Core.nuspec:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $id$
5 | $version$
6 | $title$
7 | $author$
8 | $author$
9 | https://github.com/tomeglenn/FireLite/
10 | false
11 | $description$
12 |
13 | Copyright 2015
14 | TCP Socket Server Client Multiplayer Network
15 |
16 |
--------------------------------------------------------------------------------
/FireLite.Samples.SimpleServer.Server/SimpleServer.cs:
--------------------------------------------------------------------------------
1 | using System.Threading;
2 | using FireLite.Core.Extensions;
3 |
4 | namespace FireLite.Samples.SimpleServer.Server
5 | {
6 | public class SimpleServer : Core.Network.Server
7 | {
8 | public SimpleServer(int port) : base(port)
9 | {
10 | var timer = new Timer(SendMessage, null, 5000, 5000);
11 | }
12 |
13 | private void SendMessage(object state)
14 | {
15 | foreach (var connectedClient in ConnectedClients)
16 | {
17 | connectedClient.SendPacket("Hey man".GetBytes());
18 | }
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/FireLite.Samples.SimpleServer.Client/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace FireLite.Samples.SimpleServer.Client
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | var client = new SimpleClient("127.0.0.1", 1337);
10 | var connected = false;
11 |
12 | while (!connected)
13 | {
14 | Console.WriteLine("Press any key to connect");
15 | Console.ReadKey();
16 |
17 | connected = client.Connect();
18 | }
19 |
20 | Console.WriteLine("Press any key to disconnect");
21 | Console.ReadKey();
22 | client.Disconnect();
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Core/Extensions/ByteArrayExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using FireLite.Core.Extensions;
3 | using FireLite.Samples.JsonPacketServer.Core.Exceptions;
4 | using FireLite.Samples.JsonPacketServer.Core.Network.Packets;
5 | using Newtonsoft.Json;
6 |
7 | namespace FireLite.Samples.JsonPacketServer.Core.Extensions
8 | {
9 | public static class ByteArrayExtensions
10 | {
11 | public static Packet ToPacket(this byte[] bytes)
12 | {
13 | try
14 | {
15 | var packetString = bytes.GetString();
16 | var packet = JsonConvert.DeserializeObject(packetString);
17 | var packetType = PacketLookup.OpCodeToPacketType[packet.OpCode];
18 |
19 | return (Packet) JsonConvert.DeserializeObject(packetString, packetType);
20 | }
21 | catch (Exception)
22 | {
23 | throw new IllegalPacketException();
24 | }
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Tom Glenn
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Client/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using FireLite.Samples.JsonPacketServer.Core.Network.Enums;
3 | using FireLite.Samples.JsonPacketServer.Core.Network.Packets;
4 |
5 | namespace FireLite.Samples.JsonPacketServer.Client
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | var client = new Client("127.0.0.1", 1337);
12 | client.RegisterPacketHandler(OpCode.Hello, HandleHelloPacket);
13 |
14 | var connected = false;
15 | while (!connected)
16 | {
17 | Console.WriteLine("Press any key to connect");
18 | Console.ReadKey();
19 |
20 | connected = client.Connect();
21 | }
22 |
23 | Console.WriteLine("Press any key to disconnect");
24 | Console.ReadKey();
25 | client.Disconnect();
26 | }
27 |
28 | private static void HandleHelloPacket(Packet packet)
29 | {
30 | Console.WriteLine("Received 'Hello' packet from server with message:\n{0}", ((HelloPacket)packet).Message);
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Core/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("FireLite.Samples.JsonPacketServer.Core")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("FireLite.Samples.JsonPacketServer.Core")]
13 | [assembly: AssemblyCopyright("Copyright © 2015")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("98bd96f0-1895-44a5-8b79-10b58f5acc32")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Client/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("FireLite.Samples.JsonPacketServer.Client")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("FireLite.Samples.JsonPacketServer.Client")]
13 | [assembly: AssemblyCopyright("Copyright © 2015")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("ebd082c2-a091-4dd6-b92d-c8dba90e7a29")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Server/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("FireLite.Samples.JsonPacketServer.Server")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("FireLite.Samples.JsonPacketServer.Server")]
13 | [assembly: AssemblyCopyright("Copyright © 2015")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("febc7814-a743-4709-95c3-c2dacc5c26a9")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/FireLite.Core/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("FireLite.Core")]
9 | [assembly: AssemblyDescription("A lightweight TCP Socket Server/Client Library")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("Tom Glenn")]
12 | [assembly: AssemblyProduct("FireLite.Core")]
13 | [assembly: AssemblyCopyright("Copyright © 2015")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("408004f9-b155-40ba-8ac9-9d9217e38b38")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.1.1.0")]
36 | [assembly: AssemblyFileVersion("1.1.1.0")]
37 |
--------------------------------------------------------------------------------
/FireLite.Samples.SimpleServer.Client/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("FireLite.Samples.SimpleServer.Client")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("FireLite.Samples.SimpleServer.Client")]
13 | [assembly: AssemblyCopyright("Copyright © 2015")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("8ca1f4ef-0767-47d5-8772-52954167b3dc")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/FireLite.Samples.SimpleServer.Server/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("FireLite.Samples.SimpleServer.Server")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("FireLite.Samples.SimpleServer.Server")]
13 | [assembly: AssemblyCopyright("Copyright © 2015")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("f626f791-7c61-47a0-93e1-64ac3258982f")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/FireLite.Core/Extensions/NetworkStreamExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Net.Sockets;
4 | using FireLite.Core.Exceptions;
5 |
6 | namespace FireLite.Core.Extensions
7 | {
8 | public static class NetworkStreamExtensions
9 | {
10 | public static byte[] ReadNBytes(this NetworkStream networkStream, int n)
11 | {
12 | var buffer = new byte[n];
13 | var bytesRead = 0;
14 |
15 | while (bytesRead < n)
16 | {
17 | try
18 | {
19 | var chunk = networkStream.Read(buffer, bytesRead, buffer.Length - bytesRead);
20 | if (chunk == 0)
21 | {
22 | throw new ConnectionException();
23 | }
24 |
25 | bytesRead += chunk;
26 | }
27 | catch (IOException)
28 | {
29 | throw new ConnectionException();
30 | }
31 | }
32 |
33 | return buffer;
34 | }
35 |
36 | public static byte[] ReadPacket(this NetworkStream networkStream)
37 | {
38 | var lengthBuffer = networkStream.ReadNBytes(4);
39 | var packetLength = BitConverter.ToInt32(lengthBuffer, 0);
40 |
41 | var buffer = networkStream.ReadNBytes(packetLength);
42 | return buffer;
43 | }
44 |
45 | public static void SendPacket(this NetworkStream networkStream, byte[] packetBytes)
46 | {
47 | var lengthBuffer = BitConverter.GetBytes(packetBytes.Length);
48 | networkStream.Write(lengthBuffer, 0, lengthBuffer.Length);
49 | networkStream.Write(packetBytes, 0, packetBytes.Length);
50 | networkStream.Flush();
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Client/Client.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using FireLite.Core.Extensions;
4 | using FireLite.Samples.JsonPacketServer.Core.Extensions;
5 | using FireLite.Samples.JsonPacketServer.Core.Network.Packets;
6 | using Newtonsoft.Json;
7 | using FireLite.Samples.JsonPacketServer.Core.Network.Enums;
8 |
9 | namespace FireLite.Samples.JsonPacketServer.Client
10 | {
11 | public class Client : FireLite.Core.Network.Client
12 | {
13 | private readonly IDictionary>> packetHandlers = new Dictionary>>();
14 |
15 | public Client(string host, int port) : base(host, port)
16 | {
17 | }
18 |
19 | public void SendPacket(Packet packet)
20 | {
21 | var jsonString = JsonConvert.SerializeObject(packet);
22 | SendPacket(jsonString.GetBytes());
23 | }
24 |
25 | protected override void OnPacketReceived(byte[] packetBytes)
26 | {
27 | try
28 | {
29 | var packet = packetBytes.ToPacket();
30 | OnPacketReceived(packet);
31 | }
32 | catch (Exception)
33 | {
34 | Console.WriteLine("Received illegal packet from server: {0}");
35 | }
36 | }
37 |
38 | protected void OnPacketReceived(Packet packet)
39 | {
40 | if (!packetHandlers.ContainsKey(packet.OpCode))
41 | {
42 | return;
43 | }
44 |
45 | foreach (var packetHandler in packetHandlers[packet.OpCode])
46 | {
47 | packetHandler(packet);
48 | }
49 | }
50 |
51 | public void RegisterPacketHandler(OpCode opCode, Action packetHandler)
52 | {
53 | if (!packetHandlers.ContainsKey(opCode))
54 | {
55 | packetHandlers.Add(opCode, new List>());
56 | }
57 |
58 | packetHandlers[opCode].Add(packetHandler);
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/FireLite.Core/Network/ClientConnection.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Net.Sockets;
3 | using System.Threading;
4 | using FireLite.Core.Exceptions;
5 | using FireLite.Core.Extensions;
6 | using FireLite.Core.Interfaces;
7 |
8 | namespace FireLite.Core.Network
9 | {
10 | public delegate void ClientConnectionEventHandler(ClientConnection sender);
11 | public delegate void ClientPacketReceivedEventHandler(ClientConnection sender, byte[] bytes);
12 |
13 | public class ClientConnection : IClientConnection
14 | {
15 | public event ClientConnectionEventHandler Disconnected;
16 | public event ClientPacketReceivedEventHandler PacketReceived;
17 |
18 | public Guid Id { get; private set; }
19 |
20 | private readonly TcpClient tcpClient;
21 | private NetworkStream networkStream;
22 | private readonly Thread listenThread;
23 |
24 | public ClientConnection(TcpClient tcpClient)
25 | {
26 | Id = Guid.NewGuid();
27 |
28 | this.tcpClient = tcpClient;
29 |
30 | listenThread = new Thread(ListenToClient);
31 | listenThread.Start();
32 | }
33 |
34 | public void Disconnect()
35 | {
36 | if (Disconnected != null)
37 | {
38 | Disconnected(this);
39 | }
40 |
41 | tcpClient.Close();
42 | listenThread.Abort();
43 | }
44 |
45 | public void SendPacket(byte[] packetBytes)
46 | {
47 | networkStream.SendPacket(packetBytes);
48 | }
49 |
50 | private void ListenToClient()
51 | {
52 | networkStream = tcpClient.GetStream();
53 |
54 | while (true)
55 | {
56 | try
57 | {
58 | var packetBytes = networkStream.ReadPacket();
59 |
60 | if (PacketReceived != null)
61 | {
62 | PacketReceived(this, packetBytes);
63 | }
64 | }
65 | catch (ConnectionException)
66 | {
67 | Disconnect();
68 | break;
69 | }
70 | }
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Server/JsonPacketServer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using FireLite.Core.Extensions;
4 | using FireLite.Core.Network;
5 | using FireLite.Samples.JsonPacketServer.Core.Extensions;
6 | using FireLite.Samples.JsonPacketServer.Core.Network.Packets;
7 | using Newtonsoft.Json;
8 |
9 | namespace FireLite.Samples.JsonPacketServer.Server
10 | {
11 | public class JsonPacketServer : FireLite.Core.Network.Server
12 | {
13 | public JsonPacketServer(int port) : base(port)
14 | {
15 | var timer = new Timer(SendHelloPacketToAllClients, null, 1000, 1000);
16 | }
17 |
18 | private void SendHelloPacketToAllClients(object state)
19 | {
20 | foreach (var connectedClient in ConnectedClients)
21 | {
22 | var packet = new HelloPacket("Hello world!");
23 | SendPacketToClient(connectedClient, packet);
24 | Console.WriteLine("Sent Packet OpCode.{0} to client {1}", packet.OpCode, connectedClient.Id);
25 | }
26 | }
27 |
28 | public void SendPacketToClient(ClientConnection clientConnection, Packet packet)
29 | {
30 | var jsonString = JsonConvert.SerializeObject(packet);
31 | clientConnection.SendPacket(jsonString.GetBytes());
32 | }
33 |
34 | protected override void OnClientPacketReceived(ClientConnection clientConnection, byte[] packetBytes)
35 | {
36 | try
37 | {
38 | var packet = packetBytes.ToPacket();
39 | OnClientPacketReceived(clientConnection, packet);
40 | }
41 | catch (Exception)
42 | {
43 | Console.WriteLine("Received illegal packet from client: {0}\nKilling client connection.", clientConnection.Id);
44 | clientConnection.Disconnect();
45 | }
46 | }
47 |
48 | protected void OnClientPacketReceived(ClientConnection clientConnection, Packet packet)
49 | {
50 | var helloPacket = packet as HelloPacket;
51 | if (helloPacket != null)
52 | {
53 | Console.WriteLine("Got a HelloPacket with values - Message: {0}", helloPacket.Message);
54 | }
55 | }
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/FireLite.Core/Network/Client.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Net.Sockets;
3 | using System.Threading;
4 | using FireLite.Core.Exceptions;
5 | using FireLite.Core.Extensions;
6 | using FireLite.Core.Interfaces;
7 |
8 | namespace FireLite.Core.Network
9 | {
10 | public abstract class Client : IClient
11 | {
12 | public string Host { get; set; }
13 | public int Port { get; set; }
14 |
15 | private readonly TcpClient tcpClient;
16 | private NetworkStream networkStream;
17 | private Thread listenThread;
18 |
19 | protected Client(string host, int port)
20 | {
21 | Host = host;
22 | Port = port;
23 |
24 | tcpClient = new TcpClient();
25 | }
26 |
27 | public bool Connect()
28 | {
29 | try
30 | {
31 | tcpClient.Connect(Host, Port);
32 | }
33 | catch (SocketException)
34 | {
35 | OnConnectionFailed();
36 | return false;
37 | }
38 |
39 | listenThread = new Thread(ListenToServer);
40 | listenThread.Start();
41 |
42 | OnConnected();
43 |
44 | return true;
45 | }
46 |
47 | public void Disconnect()
48 | {
49 | OnDisconnected();
50 |
51 | tcpClient.Close();
52 |
53 | if (listenThread != null)
54 | {
55 | listenThread.Abort();
56 | }
57 | }
58 |
59 | public void SendPacket(byte[] packetBytes)
60 | {
61 | networkStream.SendPacket(packetBytes);
62 | }
63 |
64 | protected virtual void OnConnected()
65 | {
66 | Console.WriteLine("Connected to server {0}:{1}", Host, Port);
67 | }
68 |
69 | protected virtual void OnConnectionFailed()
70 | {
71 | Console.WriteLine("Connection failed to server {0}:{1}, please try again", Host, Port);
72 | }
73 |
74 | protected virtual void OnDisconnected()
75 | {
76 | Console.WriteLine("Disconnected from server");
77 | }
78 |
79 | protected virtual void OnPacketReceived(byte[] packetBytes)
80 | {
81 | var str = packetBytes.GetString();
82 | Console.WriteLine("Got Message: {0}", str);
83 | }
84 |
85 | private void ListenToServer()
86 | {
87 | networkStream = tcpClient.GetStream();
88 |
89 | while (true)
90 | {
91 | try
92 | {
93 | var packetBytes = networkStream.ReadPacket();
94 | OnPacketReceived(packetBytes);
95 | }
96 | catch (ConnectionException)
97 | {
98 | Disconnect();
99 | break;
100 | }
101 | }
102 | }
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/FireLite.Samples.SimpleServer.Server/FireLite.Samples.SimpleServer.Server.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {E2DA786A-303F-4CEF-9983-93518C7DAD65}
8 | Exe
9 | Properties
10 | FireLite.Samples.SimpleServer.Server
11 | FireLite.Samples.SimpleServer.Server
12 | v4.5
13 | 512
14 |
15 |
16 | AnyCPU
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | AnyCPU
27 | pdbonly
28 | true
29 | bin\Release\
30 | TRACE
31 | prompt
32 | 4
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | {5DB73B3E-3466-48B1-8BB5-51CF9060905D}
54 | FireLite.Core
55 |
56 |
57 |
58 |
65 |
--------------------------------------------------------------------------------
/FireLite.Core/FireLite.Core.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {5DB73B3E-3466-48B1-8BB5-51CF9060905D}
8 | Library
9 | Properties
10 | FireLite.Core
11 | FireLite.Core
12 | v3.5
13 | 512
14 |
15 |
16 |
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | pdbonly
27 | true
28 | bin\Release\
29 | TRACE
30 | prompt
31 | 4
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 |
64 |
--------------------------------------------------------------------------------
/FireLite.Samples.SimpleServer.Client/FireLite.Samples.SimpleServer.Client.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {B71C09F1-D86D-4D08-BB76-E1F38C70B43A}
8 | Exe
9 | Properties
10 | FireLite.Samples.SimpleServer.Client
11 | FireLite.Samples.SimpleServer.Client
12 | v4.5
13 | 512
14 |
15 |
16 | AnyCPU
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | AnyCPU
27 | pdbonly
28 | true
29 | bin\Release\
30 | TRACE
31 | prompt
32 | 4
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | ..\packages\Unity.Newtonsoft.Json.7.0.0.0\lib\net35-Client\Unity.Newtonsoft.Json.dll
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 | {5DB73B3E-3466-48B1-8BB5-51CF9060905D}
57 | FireLite.Core
58 |
59 |
60 |
61 |
68 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Core/FireLite.Samples.JsonPacketServer.Core.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {74E9CCC0-C605-4959-9B86-95B173BF5FB3}
8 | Library
9 | Properties
10 | FireLite.Samples.JsonPacketServer.Core
11 | FireLite.Samples.JsonPacketServer.Core
12 | v4.5
13 | 512
14 |
15 |
16 | true
17 | full
18 | false
19 | bin\Debug\
20 | DEBUG;TRACE
21 | prompt
22 | 4
23 |
24 |
25 | pdbonly
26 | true
27 | bin\Release\
28 | TRACE
29 | prompt
30 | 4
31 |
32 |
33 |
34 | ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 | {5db73b3e-3466-48b1-8bb5-51cf9060905d}
57 | FireLite.Core
58 |
59 |
60 |
61 |
62 |
63 |
64 |
71 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Client/FireLite.Samples.JsonPacketServer.Client.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {5D818C08-E442-46CC-8D08-34DB1D4D1282}
8 | Exe
9 | Properties
10 | FireLite.Samples.JsonPacketServer.Client
11 | FireLite.Samples.JsonPacketServer.Client
12 | v4.5
13 | 512
14 |
15 |
16 | AnyCPU
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | AnyCPU
27 | pdbonly
28 | true
29 | bin\Release\
30 | TRACE
31 | prompt
32 | 4
33 |
34 |
35 |
36 | ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 | {5db73b3e-3466-48b1-8bb5-51cf9060905d}
58 | FireLite.Core
59 |
60 |
61 | {74e9ccc0-c605-4959-9b86-95b173bf5fb3}
62 | FireLite.Samples.JsonPacketServer.Core
63 |
64 |
65 |
66 |
73 |
--------------------------------------------------------------------------------
/FireLite.Samples.JsonPacketServer.Server/FireLite.Samples.JsonPacketServer.Server.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {3CBBDE85-EB4B-49E6-9340-71893161FECE}
8 | Exe
9 | Properties
10 | FireLite.Samples.JsonPacketServer.Server
11 | FireLite.Samples.JsonPacketServer.Server
12 | v4.5
13 | 512
14 |
15 |
16 | AnyCPU
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | AnyCPU
27 | pdbonly
28 | true
29 | bin\Release\
30 | TRACE
31 | prompt
32 | 4
33 |
34 |
35 |
36 | ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 | {5db73b3e-3466-48b1-8bb5-51cf9060905d}
58 | FireLite.Core
59 |
60 |
61 | {74e9ccc0-c605-4959-9b86-95b173bf5fb3}
62 | FireLite.Samples.JsonPacketServer.Core
63 |
64 |
65 |
66 |
73 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | build/
21 | bld/
22 | [Bb]in/
23 | [Oo]bj/
24 |
25 | # Visual Studo 2015 cache/options directory
26 | .vs/
27 |
28 | # MSTest test Results
29 | [Tt]est[Rr]esult*/
30 | [Bb]uild[Ll]og.*
31 |
32 | # NUNIT
33 | *.VisualState.xml
34 | TestResult.xml
35 |
36 | # Build Results of an ATL Project
37 | [Dd]ebugPS/
38 | [Rr]eleasePS/
39 | dlldata.c
40 |
41 | *_i.c
42 | *_p.c
43 | *_i.h
44 | *.ilk
45 | *.meta
46 | *.obj
47 | *.pch
48 | *.pdb
49 | *.pgc
50 | *.pgd
51 | *.rsp
52 | *.sbr
53 | *.tlb
54 | *.tli
55 | *.tlh
56 | *.tmp
57 | *.tmp_proj
58 | *.log
59 | *.vspscc
60 | *.vssscc
61 | .builds
62 | *.pidb
63 | *.svclog
64 | *.scc
65 |
66 | # Chutzpah Test files
67 | _Chutzpah*
68 |
69 | # Visual C++ cache files
70 | ipch/
71 | *.aps
72 | *.ncb
73 | *.opensdf
74 | *.sdf
75 | *.cachefile
76 |
77 | # Visual Studio profiler
78 | *.psess
79 | *.vsp
80 | *.vspx
81 |
82 | # TFS 2012 Local Workspace
83 | $tf/
84 |
85 | # Guidance Automation Toolkit
86 | *.gpState
87 |
88 | # ReSharper is a .NET coding add-in
89 | _ReSharper*/
90 | *.[Rr]e[Ss]harper
91 | *.DotSettings.user
92 |
93 | # JustCode is a .NET coding addin-in
94 | .JustCode
95 |
96 | # TeamCity is a build add-in
97 | _TeamCity*
98 |
99 | # DotCover is a Code Coverage Tool
100 | *.dotCover
101 |
102 | # NCrunch
103 | _NCrunch_*
104 | .*crunch*.local.xml
105 |
106 | # MightyMoose
107 | *.mm.*
108 | AutoTest.Net/
109 |
110 | # Web workbench (sass)
111 | .sass-cache/
112 |
113 | # Installshield output folder
114 | [Ee]xpress/
115 |
116 | # DocProject is a documentation generator add-in
117 | DocProject/buildhelp/
118 | DocProject/Help/*.HxT
119 | DocProject/Help/*.HxC
120 | DocProject/Help/*.hhc
121 | DocProject/Help/*.hhk
122 | DocProject/Help/*.hhp
123 | DocProject/Help/Html2
124 | DocProject/Help/html
125 |
126 | # Click-Once directory
127 | publish/
128 |
129 | # Publish Web Output
130 | *.[Pp]ublish.xml
131 | *.azurePubxml
132 | # TODO: Comment the next line if you want to checkin your web deploy settings
133 | # but database connection strings (with potential passwords) will be unencrypted
134 | *.pubxml
135 | *.publishproj
136 |
137 | # NuGet Packages
138 | *.nupkg
139 | # The packages folder can be ignored because of Package Restore
140 | **/packages/*
141 | # except build/, which is used as an MSBuild target.
142 | !**/packages/build/
143 | # Uncomment if necessary however generally it will be regenerated when needed
144 | #!**/packages/repositories.config
145 |
146 | # Windows Azure Build Output
147 | csx/
148 | *.build.csdef
149 |
150 | # Windows Store app package directory
151 | AppPackages/
152 |
153 | # Others
154 | *.[Cc]ache
155 | ClientBin/
156 | [Ss]tyle[Cc]op.*
157 | ~$*
158 | *~
159 | *.dbmdl
160 | *.dbproj.schemaview
161 | *.pfx
162 | *.publishsettings
163 | node_modules/
164 | bower_components/
165 |
166 | # RIA/Silverlight projects
167 | Generated_Code/
168 |
169 | # Backup & report files from converting an old project file
170 | # to a newer Visual Studio version. Backup files are not needed,
171 | # because we have git ;-)
172 | _UpgradeReport_Files/
173 | Backup*/
174 | UpgradeLog*.XML
175 | UpgradeLog*.htm
176 |
177 | # SQL Server files
178 | *.mdf
179 | *.ldf
180 |
181 | # Business Intelligence projects
182 | *.rdl.data
183 | *.bim.layout
184 | *.bim_*.settings
185 |
186 | # Microsoft Fakes
187 | FakesAssemblies/
188 |
189 | # Node.js Tools for Visual Studio
190 | .ntvs_analysis.dat
191 |
192 | # Visual Studio 6 build log
193 | *.plg
194 |
195 | # Visual Studio 6 workspace options file
196 | *.opt
197 |
--------------------------------------------------------------------------------
/FireLite.Core/Network/Server.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Net;
6 | using System.Net.Sockets;
7 | using System.Threading;
8 | using FireLite.Core.Extensions;
9 | using FireLite.Core.Interfaces;
10 |
11 | namespace FireLite.Core.Network
12 | {
13 | public abstract class Server : IServer
14 | {
15 | public int Port { get; set; }
16 |
17 | protected IList ConnectedClients { get; private set; }
18 |
19 | private readonly TcpListener tcpListener;
20 | private Thread listenThread;
21 |
22 | protected Server(int port)
23 | {
24 | Port = port;
25 | tcpListener = new TcpListener(IPAddress.Any, Port);
26 | ConnectedClients = new List();
27 | }
28 |
29 | public void Start()
30 | {
31 | tcpListener.Start();
32 |
33 | listenThread = new Thread(ListenForClients);
34 | listenThread.Start();
35 |
36 | OnStarted();
37 | }
38 |
39 | public void Stop()
40 | {
41 | tcpListener.Stop();
42 | listenThread.Abort();
43 |
44 | OnStopped();
45 | }
46 |
47 | protected virtual void OnStarted()
48 | {
49 | Console.WriteLine("Server started on port {0}", Port);
50 | }
51 |
52 | protected virtual void OnStopped()
53 | {
54 | Console.WriteLine("Server stopped");
55 | }
56 |
57 | protected virtual void OnClientConnected(ClientConnection clientConnection)
58 | {
59 | Console.WriteLine("Client connected: {0}", clientConnection.Id);
60 | }
61 |
62 | protected virtual void OnClientDisconnected(ClientConnection clientConnection)
63 | {
64 | Console.WriteLine("Client disconnected: {0}", clientConnection.Id);
65 | }
66 |
67 | protected virtual void OnClientPacketReceived(ClientConnection clientConnection, byte[] packetBytes)
68 | {
69 | Console.WriteLine("Packet received from client {0}:\n{1}", clientConnection.Id, packetBytes.GetString());
70 | }
71 |
72 | private void ListenForClients()
73 | {
74 | while (true)
75 | {
76 | try
77 | {
78 | var client = tcpListener.AcceptTcpClient();
79 | var clientThread = new Thread(HandleClientConnected);
80 | clientThread.Start(client);
81 | }
82 | catch (ObjectDisposedException)
83 | {
84 | listenThread.Abort();
85 | }
86 | }
87 | }
88 |
89 | private void HandleClientConnected(object client)
90 | {
91 | var clientConnection = new ClientConnection((TcpClient) client);
92 | ConnectedClients.Add(clientConnection);
93 |
94 | clientConnection.Disconnected += HandleClientDisconnected;
95 | clientConnection.PacketReceived += HandleClientPacketReceived;
96 |
97 | OnClientConnected(clientConnection);
98 | }
99 |
100 | private void HandleClientDisconnected(ClientConnection clientConnection)
101 | {
102 | var connectedClient = ConnectedClients.FirstOrDefault(x => x.Id == clientConnection.Id);
103 | if (connectedClient != null)
104 | {
105 | ConnectedClients.Remove(connectedClient);
106 | }
107 |
108 | OnClientDisconnected(clientConnection);
109 | }
110 |
111 | private void HandleClientPacketReceived(ClientConnection clientConnection, byte[] packetBytes)
112 | {
113 | OnClientPacketReceived(clientConnection, packetBytes);
114 | }
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/FireLite.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2013
4 | VisualStudioVersion = 12.0.31101.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FireLite.Core", "FireLite.Core\FireLite.Core.csproj", "{5DB73B3E-3466-48B1-8BB5-51CF9060905D}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{F9E2383A-D7B3-44CA-99ED-9483AE819D91}"
9 | EndProject
10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SimpleServer", "SimpleServer", "{E2077E9D-7AB7-4849-9B51-944008A36173}"
11 | EndProject
12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FireLite.Samples.SimpleServer.Server", "FireLite.Samples.SimpleServer.Server\FireLite.Samples.SimpleServer.Server.csproj", "{E2DA786A-303F-4CEF-9983-93518C7DAD65}"
13 | EndProject
14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FireLite.Samples.SimpleServer.Client", "FireLite.Samples.SimpleServer.Client\FireLite.Samples.SimpleServer.Client.csproj", "{B71C09F1-D86D-4D08-BB76-E1F38C70B43A}"
15 | EndProject
16 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "JsonPacketServer", "JsonPacketServer", "{FC2C5015-F6A7-4C72-90EA-3A5BE3CBB7B9}"
17 | EndProject
18 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FireLite.Samples.JsonPacketServer.Server", "FireLite.Samples.JsonPacketServer.Server\FireLite.Samples.JsonPacketServer.Server.csproj", "{3CBBDE85-EB4B-49E6-9340-71893161FECE}"
19 | EndProject
20 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FireLite.Samples.JsonPacketServer.Client", "FireLite.Samples.JsonPacketServer.Client\FireLite.Samples.JsonPacketServer.Client.csproj", "{5D818C08-E442-46CC-8D08-34DB1D4D1282}"
21 | EndProject
22 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FireLite.Samples.JsonPacketServer.Core", "FireLite.Samples.JsonPacketServer.Core\FireLite.Samples.JsonPacketServer.Core.csproj", "{74E9CCC0-C605-4959-9B86-95B173BF5FB3}"
23 | EndProject
24 | Global
25 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
26 | Debug|Any CPU = Debug|Any CPU
27 | Release|Any CPU = Release|Any CPU
28 | EndGlobalSection
29 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
30 | {5DB73B3E-3466-48B1-8BB5-51CF9060905D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31 | {5DB73B3E-3466-48B1-8BB5-51CF9060905D}.Debug|Any CPU.Build.0 = Debug|Any CPU
32 | {5DB73B3E-3466-48B1-8BB5-51CF9060905D}.Release|Any CPU.ActiveCfg = Release|Any CPU
33 | {5DB73B3E-3466-48B1-8BB5-51CF9060905D}.Release|Any CPU.Build.0 = Release|Any CPU
34 | {E2DA786A-303F-4CEF-9983-93518C7DAD65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35 | {E2DA786A-303F-4CEF-9983-93518C7DAD65}.Debug|Any CPU.Build.0 = Debug|Any CPU
36 | {E2DA786A-303F-4CEF-9983-93518C7DAD65}.Release|Any CPU.ActiveCfg = Release|Any CPU
37 | {E2DA786A-303F-4CEF-9983-93518C7DAD65}.Release|Any CPU.Build.0 = Release|Any CPU
38 | {B71C09F1-D86D-4D08-BB76-E1F38C70B43A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39 | {B71C09F1-D86D-4D08-BB76-E1F38C70B43A}.Debug|Any CPU.Build.0 = Debug|Any CPU
40 | {B71C09F1-D86D-4D08-BB76-E1F38C70B43A}.Release|Any CPU.ActiveCfg = Release|Any CPU
41 | {B71C09F1-D86D-4D08-BB76-E1F38C70B43A}.Release|Any CPU.Build.0 = Release|Any CPU
42 | {3CBBDE85-EB4B-49E6-9340-71893161FECE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43 | {3CBBDE85-EB4B-49E6-9340-71893161FECE}.Debug|Any CPU.Build.0 = Debug|Any CPU
44 | {3CBBDE85-EB4B-49E6-9340-71893161FECE}.Release|Any CPU.ActiveCfg = Release|Any CPU
45 | {3CBBDE85-EB4B-49E6-9340-71893161FECE}.Release|Any CPU.Build.0 = Release|Any CPU
46 | {5D818C08-E442-46CC-8D08-34DB1D4D1282}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47 | {5D818C08-E442-46CC-8D08-34DB1D4D1282}.Debug|Any CPU.Build.0 = Debug|Any CPU
48 | {5D818C08-E442-46CC-8D08-34DB1D4D1282}.Release|Any CPU.ActiveCfg = Release|Any CPU
49 | {5D818C08-E442-46CC-8D08-34DB1D4D1282}.Release|Any CPU.Build.0 = Release|Any CPU
50 | {74E9CCC0-C605-4959-9B86-95B173BF5FB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51 | {74E9CCC0-C605-4959-9B86-95B173BF5FB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
52 | {74E9CCC0-C605-4959-9B86-95B173BF5FB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
53 | {74E9CCC0-C605-4959-9B86-95B173BF5FB3}.Release|Any CPU.Build.0 = Release|Any CPU
54 | EndGlobalSection
55 | GlobalSection(SolutionProperties) = preSolution
56 | HideSolutionNode = FALSE
57 | EndGlobalSection
58 | GlobalSection(NestedProjects) = preSolution
59 | {E2077E9D-7AB7-4849-9B51-944008A36173} = {F9E2383A-D7B3-44CA-99ED-9483AE819D91}
60 | {E2DA786A-303F-4CEF-9983-93518C7DAD65} = {E2077E9D-7AB7-4849-9B51-944008A36173}
61 | {B71C09F1-D86D-4D08-BB76-E1F38C70B43A} = {E2077E9D-7AB7-4849-9B51-944008A36173}
62 | {FC2C5015-F6A7-4C72-90EA-3A5BE3CBB7B9} = {F9E2383A-D7B3-44CA-99ED-9483AE819D91}
63 | {3CBBDE85-EB4B-49E6-9340-71893161FECE} = {FC2C5015-F6A7-4C72-90EA-3A5BE3CBB7B9}
64 | {5D818C08-E442-46CC-8D08-34DB1D4D1282} = {FC2C5015-F6A7-4C72-90EA-3A5BE3CBB7B9}
65 | {74E9CCC0-C605-4959-9B86-95B173BF5FB3} = {FC2C5015-F6A7-4C72-90EA-3A5BE3CBB7B9}
66 | EndGlobalSection
67 | EndGlobal
68 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://travis-ci.org/tomeglenn/FireLite)
2 |
3 | # FireLite
4 | ### A Lightweight TCP Socket Server/Client Library
5 | FireLite is an extremely lightweight TCP Socket Server/Client Library that enables you to quickly and easily setup client/server communication in your .NET applications.
6 |
7 | FireLite was built primarily to facilitate the creation of a lightweight online multiplayer game and so it's key functionality is to provide a simple and elegant solution to transferring raw bytes over a TCP connection.
8 |
9 | ## Usage
10 | ### Installation
11 | Open NuGet Packet Manager Console and type:
12 | ```
13 | Install-Package FireLite.Core
14 | ```
15 |
16 | ### Creating a Basic Server
17 | To create a basic server, you simply inherit from `FireLite.Core.Network.Server`.
18 |
19 | ```
20 | public class BasicServer : FireLite.Core.Network.Server
21 | {
22 | public BasicServer(int port) : base(port) { }
23 | }
24 | ```
25 |
26 | You can then start your server in a Console Application as follows:
27 |
28 | ```
29 | static void Main(string[] args)
30 | {
31 | var server = new BasicServer(1337);
32 | server.Start();
33 |
34 | Console.WriteLine("Press any key to stop the server.")
35 | Console.ReadKey();
36 | server.Stop();
37 | }
38 | ```
39 |
40 | #### The Server Class
41 | The `Server` class is an abstract base implementation that must be inherited from.
42 |
43 | ##### Public/Protected Properties
44 |
45 | * `public int Port`
46 | * `protected IList ConnectedClients`
47 |
48 | ##### Public/Protected Methods
49 |
50 | * `public void Start()`
51 | * `public void Stop()`
52 | * `protected virtual void OnStarted()`
53 | * `protected virtual void OnStopped()`
54 | * `protected virtual void OnClientConnected(ClientConnection)`
55 | * `protected virtual void OnClientDisconnected(ClientConnection)`
56 | * `protected virtual void OnClientPacketReceived(ClientConnection, byte[])`
57 |
58 | #### The ClientConnection Class
59 | The `ClientConnection` class represents a single connected client.
60 |
61 | ##### Public/Protected Properties
62 | * `public Guid Id`
63 |
64 | ##### Public/Protected Methods
65 |
66 | * `public void SendPacket(byte[])`
67 | * `public void Disconnect()`
68 |
69 | #### Overriding the Server's Functionality
70 | There are several protected methods available for you to override which allow you to customize how your server interacts with clients. These are:
71 |
72 | * `protected virtual void OnStarted()`
73 | * `protected virtual void OnStopped()`
74 | * `protected virtual void OnClientConnected(ClientConnection)`
75 | * `protected virtual void OnClientDisconnected(ClientConnection)`
76 | * `protected virtual void OnClientPacketReceived(ClientConnection, byte[])`
77 |
78 | An example use case of overriding one of these methods would be to send a welcome message to a client when they connect to the server.
79 |
80 | ```
81 | protected override void OnClientConnected(ClientConnection clientConnection)
82 | {
83 | var message = "Hello, client. Welcome to the server".GetBytes();
84 | clientConnection.SendPacket(message);
85 | }
86 | ```
87 |
88 | ## Creating a Basic Client
89 | To create a basic client, you simply inherit from `FireLite.Core.Network.Client`.
90 |
91 | ```
92 | public class BasicClient : FireLite.Core.Network.Client
93 | {
94 | public BasicClient(string host, int port) : base(host, port) { }
95 | }
96 | ```
97 |
98 | You can then connect your client to a server in a Console Application as follows:
99 |
100 | ```
101 | static void Main(string[] args)
102 | {
103 | var client = new BasicClient("127.0.0.1", 1337);
104 | client.Connect();
105 |
106 | Console.WriteLine("Press any key to disconnect");
107 | Console.ReadKey();
108 | client.Disconnect();
109 | }
110 | ```
111 |
112 | #### The Client Class
113 | The `Client` class is an abstract base implementation that must be inherited from.
114 |
115 | ##### Public/Protected Properties
116 | * `public string Host`
117 | * `public int Port`
118 |
119 | ##### Public/Protected Methods
120 | * `public bool Connect()`
121 | * `public void Disconnect()`
122 | * `public void SendPacket(byte[])`
123 | * `protected virtual void OnConnected()`
124 | * `protected virtual void OnConnectionFailed()`
125 | * `protected virtual void OnDisconnected()`
126 | * `protected virtual void OnPacketReceived(byte[])`
127 |
128 | #### Overriding the Client's Functionality
129 | There are several protected methods available for you to override which allow you to customize how your client interacts with the server. These are:
130 |
131 | * `protected virtual void OnConnected()`
132 | * `protected virtual void OnConnectionFailed()`
133 | * `protected virtual void OnDisconnected()`
134 | * `protected virtual void OnPacketReceived(byte[])`
135 |
136 | An example use case of overriding one of these methods would be to send a response back to the server when you receive a specific message.
137 |
138 | ```
139 | protected override void OnPacketReceived(byte[] packetBytes)
140 | {
141 | var message = packetBytes.GetString();
142 | if (message == "Hello, world!")
143 | {
144 | var response = "Oh hi, server!".GetBytes();
145 | SendPacket(response);
146 | }
147 | }
148 | ```
149 |
150 | ## Utility Extension Methods
151 | FireLite comes with a couple of extension methods that facilitate sending UTF8 encoded strings over the TCP connection. These include:
152 |
153 | * `string GetString(this byte[])`
154 | * `byte[] GetString(this string)`
155 |
156 | It also comes with a few `NetworkStream` extension methods that facilitate sending and receiving TCP packets. These are really only meant to be used internally by `FireLite.Core` classes, but they have been made public should you wish to use them for any reason. These include:
157 |
158 | * `byte[] ReadNBytes(this NetworkStream)`
159 | * `byte[] ReadPacket(this NetworkStream)`
160 | * `void SendPacket(this NetworkStream, byte[])`
161 |
--------------------------------------------------------------------------------