Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- C# compiler interprets static class as a namespace
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- using System.Collections;
- using ProtoBuf;
- using UnnamedGameServer;
- using System.Threading;
- namespace Unnamed_game
- {
- class Connection
- {
- public struct UserInformation
- {
- public string Username;
- public int UserID;
- }
- static private Connection instance;
- private Connection()
- {
- client = new TcpClient();
- header = new PacketHeader();
- _isLoggedCharacter = false;
- _isLoggedUser = false;
- magicByte = UnnamedGameServer.SharedConstants.ServerInfo.MagicByte;
- }
- // properties
- TcpClient client;
- PacketHeader header;
- Thread serverCommThread;
- byte[] magicByte;
- bool _isLoggedCharacter;
- CharacterInformation chInformation
- {
- get
- {
- return Player.Instance().Information;
- }
- }
- bool _isLoggedUser;
- public UserInformation Information;
- // methods
- static public Connection Instance()
- {
- if(instance == null)
- {
- instance = new Connection();
- return instance;
- }
- else
- {
- return instance;
- }
- }
- /// <summary>
- /// Should never be used. Use the parameterless one to get the server address and information. It doesn't use try-catch, the exception handling should be done on Game1.Initialize()
- /// </summary>
- public void ConnectServer(IPEndPoint endPoint)
- {
- if (client.Connected)
- {
- return;
- }
- else
- {
- client.Connect(endPoint);
- serverCommThread = new Thread(HandleServerCommunication);
- serverCommThread.Start();
- }
- }
- public void ConnectServer()
- {
- this.ConnectServer(new IPEndPoint(UnnamedGameServer.SharedConstants.ServerInfo.ServerAddress, UnnamedGameServer.SharedConstants.ServerInfo.Port));
- }
- private void HandleServerCommunication()
- {
- if (client == null)
- {
- throw new Exception("The TcpClient is null");
- }
- else
- {
- // this doesn't work
- byte[] magicByte = SharedConstants.ServerInfo.MagicByte;
- // this works
- magicByte = UnnamedGameServer.SharedConstants.ServerInfo.MagicByte;
- }
- }
- private void SendPacket(ActionType actionType, object packetStruct)
- {
- try
- {
- header.ActionTypeNumber = (short)actionType;
- using (NetworkStream stream = client.GetStream())
- {
- stream.Write(magicByte, 0, magicByte.Length);
- Serializer.SerializeWithLengthPrefix<PacketHeader>(stream, header, PrefixStyle.Base128);
- switch (actionType)
- {
- case ActionType.Connect:
- Serializer.SerializeWithLengthPrefix<PacketConnect>(stream, (PacketConnect)packetStruct, PrefixStyle.Base128);
- break;
- }
- stream.Write(magicByte, 0, magicByte.Length);
- }
- }
- catch (Exception)
- {
- // error
- return;
- }
- }
- public void CreateNewCharacter(string characterName, CharacterClass chClass, CharacterRace chRace)
- {
- var info = new NewCharacterInfo();
- info.Name = characterName;
- info.OwnerUsername = Information.Username;
- info.Class = chClass;
- info.Race = chRace;
- CreateNewCharacter(info);
- }
- public void CreateNewCharacter(NewCharacterInfo info)
- {
- var packet = new PacketCreateNewCharacter();
- packet.chInfo = info;
- SendPacket(ActionType.CreateNewCharacter, packet);
- }
- public void ConnectUser(string username, string unhashedPassword)
- {
- var packet = new PacketConnect();
- packet.Username = username;
- packet.UnhashedPassword = unhashedPassword;
- ConnectUser(packet);
- }
- public void ConnectUser(PacketConnect packet)
- {
- if (_isLoggedCharacter || _isLoggedUser)
- {
- return;
- }
- else
- {
- SendPacket(ActionType.Connect, packet);
- }
- }
- public void ConnectCharacter(string characterName, short serverID)
- {
- var packet = new PacketLoginCharacter();
- packet.CharacterName = characterName;
- packet.ServerID = serverID;
- ConnectCharacter(packet);
- }
- public void ConnectCharacter(PacketLoginCharacter packet)
- {
- if (_isLoggedCharacter || !_isLoggedUser)
- {
- return;
- }
- else
- {
- SendPacket(ActionType.LoginCharacter, packet);
- }
- }
- public void Disconnect(PacketDisconnect packet)
- {
- if (!_isLoggedUser)
- {
- return;
- }
- else
- {
- SendPacket(ActionType.Disconnect, packet);
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using System.Runtime.InteropServices;
- namespace UnnamedGameServer
- {
- /// <summary>
- /// ALL CONSTANTS THAT ARE SHARED BY THE CLIENT AND THE SERVER ARE STORED HERE. DONT ADD MORE CLASSES THAT STORE CONSTANTS. Use GameConstants for game-only constants.
- /// </summary>
- public static class SharedConstants
- {
- /// <summary>
- /// Server information with port, IP and MagicByte
- /// </summary>
- public static class ServerInfo
- {
- public const short Port = 6483;
- public static readonly IPAddress ServerAddress = IPAddress.Loopback;
- public static byte[] MagicByte
- {
- get
- {
- return new byte[4] { 0xF1, 0x64, 0x83, 0xC4 };
- }
- }
- }
- /// <summary>
- /// Character constants shared by client/server
- /// </summary>
- public static class CharacterConstants
- {
- public static class Movement
- {
- /// <summary>
- /// Coordinates per update
- /// </summary>
- public const float DefaultCoordinatePerUpdate = 8;
- public const float ModifierNormal = 1f;
- public const float ModifierFrozen = 0.8f;
- public const float ModifierSpeedBuff = 1.2f;
- public const float ModifierStop = 0f;
- }
- }
- /// <summary>
- /// Networking constants
- /// </summary>
- public static class Networking
- {
- public const int MilisecondsPerMovementUpdate = 100;
- public const ushort ActionTypeNonMetaActionStart = 0x0FFF + 1;
- /// <summary>
- /// What is the number of actions a non-logged user can perform
- /// </summary>
- public const ushort ActionTypeNonLoggedUser = 0x000F;
- }
- }
- enum CharacterDirection
- {
- NoMovement = 0x00,
- Top = 0x01,
- TopRight = 0x02,
- TopLeft = 0x03,
- Right = 0x04,
- BottomRight = 0x05,
- Bottom = 0x06,
- BottomLeft = 0x07,
- Left = 0x08
- }
- enum CharacterStatus
- {
- Alive = 0x01,
- Dead = 0x02
- }
- enum CharacterClass
- {
- Mage = 0x01,
- Knight = 0x02
- }
- enum CharacterRace
- {
- Human = 0x01
- }
- enum CharacterType
- {
- Player = 0x01,
- GameMaster = 0x02
- }
- enum CharacterFaction
- {
- Newbie = 0x01,
- Army = 0x02,
- Chaos = 0x03
- }
- enum CharacterMovementStatus
- {
- Normal = 0x01,
- Frozen = 0x02,
- SpeedBuff = 0x03,
- Stop = 0x04
- }
- struct CharacterExperience
- {
- public CharacterExperience(short level, int experience)
- {
- Level = level;
- Experience = experience;
- }
- public short Level;
- public int Experience;
- }
- }
- namespace Unnamed_game
- {
- using UnamedGameServer;
- class Connection
- using UnnamedGameServer;
- class MyClass {
- byte[] b = SharedConstants.ServerInfo.MagicByte;
- }
Add Comment
Please, Sign In to add comment