Guest User

Untitled

a guest
Aug 20th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 KB | None | 0 0
  1. Program.cs:
  2.         static void Main(string[] args)
  3.         {
  4.             MainSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
  5.  
  6.             GameAddr = "192.168.1.74";
  7.             GamePort = 1626;
  8.  
  9.             MainSocket.Bind(new IPEndPoint(IPAddress.Parse(GameAddr), GamePort));
  10.             MainSocket.Listen(10);
  11.            
  12.             CaptainEnvironment.Initialize(MainSocket);
  13.  
  14.             Console.WriteLine("Game socket bound to " + GameAddr + ":" + GamePort);
  15.            
  16.             while (true)
  17.             {
  18.                 Console.ReadLine();
  19.                 Console.WriteLine(">> HELLO!!");
  20.             }
  21.         }
  22.  
  23. CaptainEnvironment.cs:
  24. namespace Captain
  25. {
  26.     static class CaptainEnvironment
  27.     {
  28.         static AsyncCallback AcceptGameConnection;
  29.         static Socket MainSocket;
  30.        
  31.         static int ConnectionCount = 0;
  32.  
  33.         public static void Initialize(Socket _MainSocket)
  34.         {
  35.             MainSocket = _MainSocket;
  36.  
  37.             AcceptGameConnection = new AsyncCallback(AddConnection);
  38.             MainSocket.BeginAccept(AcceptGameConnection, MainSocket);
  39.         }
  40.  
  41.         public static void AddConnection(IAsyncResult Result)
  42.         {
  43.             Socket MainSocket = (Socket)Result.AsyncState;
  44.             Socket UserSocket = MainSocket.EndAccept(Result);
  45.  
  46.             AcceptGameConnection = new AsyncCallback(AddConnection);
  47.             MainSocket.BeginAccept(0, AcceptGameConnection, MainSocket);
  48.  
  49.             IPEndPoint UserAddr = UserSocket.RemoteEndPoint as IPEndPoint;
  50.  
  51.             Console.WriteLine(">> Game connection request from " + UserAddr.Address);
  52.             Connection UserConnection = new Connection(UserSocket);
  53.             Console.WriteLine(">> Connection accepted from " + UserAddr.Address);
  54.  
  55.             ConnectionCount++;
  56.         }
  57.     }
  58. }
  59.  
  60. Connection.cs:
  61. namespace Captain.Communication
  62. {
  63.     class Connection
  64.     {
  65.         public User Player;
  66.         public Socket UserSocket;
  67.         public int Id;
  68.         public byte[] PacketBytes;
  69.         private bool Connected;
  70.        
  71.         public AsyncCallback ReceivedCallback;
  72.         public AsyncCallback SentCallback;
  73.        
  74.         public Connection(Socket _UserSocket)
  75.         {
  76.             // Login functions etc.
  77.             UserSocket = _UserSocket;
  78.  
  79.             ReceivedCallback = new AsyncCallback(DataReceived);
  80.             SentCallback = new AsyncCallback(DataSent);
  81.  
  82.             PacketBytes = new byte[4096];
  83.             UserSocket.BeginReceive(PacketBytes, 0, PacketBytes.Length, SocketFlags.None, ReceivedCallback, null);
  84.         }
  85.  
  86.         public void DataReceived(IAsyncResult Result)
  87.         {
  88.             try
  89.             {
  90.                 UserSocket.EndReceive(Result);
  91.             }
  92.  
  93.             catch (SocketException e)
  94.             {
  95.                 Console.ForegroundColor = ConsoleColor.Red;
  96.                 Console.WriteLine(">> " + e.SocketErrorCode + ": " + e.Message);
  97.                 Console.ForegroundColor = ConsoleColor.White;
  98.  
  99.                 CaptainEnvironment.RemoveConnection(this);
  100.                 return;
  101.             }
  102.                        
  103.             if (PacketBytes == null && !Player.Authenticated)
  104.             {
  105.                 UserSocket.BeginSend(new byte[0], 0, 0, SocketFlags.None, SentCallback, null);
  106.                 return;
  107.             }
  108.  
  109.             PacketBytes = Encoding.ASCII.GetBytes(Encoding.ASCII.GetString(PacketBytes).TrimEnd(new char[] { (char)(0) }));
  110.             byte[] LenArr = new byte[] { (byte)PacketBytes[4], (byte)PacketBytes[5] };
  111.            
  112.             if(BitConverter.IsLittleEndian)
  113.             {
  114.                 Array.Reverse(LenArr);
  115.             }
  116.  
  117.             int PacketLength = BitConverter.ToInt16(LenArr, 0);
  118.             Console.WriteLine(">> Length of incoming packet: " + PacketLength);
  119.  
  120.             HandlePacket(PacketBytes);
  121.  
  122.             if (Connected)
  123.             {
  124.                 PacketBytes = new byte[4096];
  125.                 UserSocket.BeginReceive(PacketBytes, 0, PacketBytes.Length, SocketFlags.None, ReceivedCallback, null);
  126.             }
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment