Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.Net.Sockets;
  11.  
  12.  
  13.  
  14. namespace Game
  15. {
  16.  
  17.     public partial class Form1 : Form
  18.     {
  19.         delegate LoginSocket LogInDelegate(Spine.Objects.loginpackage l);
  20.  
  21.         Socket client;
  22.         IPAddress[] aryLocalAddr = null;
  23.         int serverport = 1000;
  24.         int backlog = 20;
  25.         Spine.Objects O = new Spine.Objects();
  26.         BackgroundWorker Login = new BackgroundWorker();
  27.         [Serializable]
  28.         public class LoginSocket{
  29.            public Spine.Objects.Player P = new Spine.Objects.Player();
  30.            public Spine.Objects.logininfo L;
  31.            
  32.            public string username;
  33.            public string password;
  34.          }
  35.  
  36.  
  37.         public Form1()
  38.         {
  39.             InitializeComponent();
  40.          
  41.  
  42.         }
  43.  
  44.      
  45.  
  46.  
  47.         private void Form1_Load(object sender, EventArgs e)
  48.         {
  49.             Socket listener = new Socket(AddressFamily.InterNetwork,
  50.                   SocketType.Stream, ProtocolType.Tcp);
  51.             IPAddress hostIP = (Dns.Resolve(IPAddress.Any.ToString())).AddressList[0];
  52.             listener.Bind(new IPEndPoint(hostIP, serverport));
  53.             //listener.Bind( new IPEndPoint( IPAddress.Loopback, 399 ) );
  54.  
  55.             // For use with localhost 127.0.0.1
  56.  
  57.             listener.Listen(10);
  58.  
  59.             // Setup a callback to be notified of connection requests
  60.  
  61.             listener.BeginAccept(new AsyncCallback(OnConnectRequest), listener);
  62.         }
  63.         public void OnConnectRequest(IAsyncResult ar)
  64.         {
  65.            
  66.             string response = "";
  67.             Socket listener = (Socket)ar.AsyncState;
  68.             client = listener.EndAccept(ar);
  69.             byte [] b = new byte[1024];
  70.  
  71.            
  72.                 int bytes = 0;
  73.  
  74.  
  75.                 do
  76.                 {
  77.                     bytes = client.Receive(b);
  78.                     response += Encoding.ASCII.GetString(b, 0, bytes);
  79.                 } while (bytes > 0 && !response.Contains("\r\n"));
  80.  
  81.  
  82.                 string requesttype = response.Substring(0, 1);
  83.                 string objectrep = response.Substring(1).Replace("\r\n", "");
  84.                 object O = Spine.Objects.ByteArrayToObject(System.Text.Encoding.ASCII.GetBytes(objectrep));
  85.                 Spine.Objects.loginpackage LP;
  86.                 if (requesttype == "L")
  87.                 {
  88.                     LP = (Spine.Objects.loginpackage)O;
  89.                     LogInDelegate d = new LogInDelegate(logInDelegate);
  90.                     object o = this.Invoke (d, new object[] { LP });
  91.                     LoginSocket LS = (LoginSocket)o;
  92.                     client.Send(Spine.Objects.BuildRequest(LS,"L"));
  93.  
  94.                    
  95.  
  96.  
  97.                 }
  98.  
  99.  
  100.        
  101.             listener.BeginAccept(new AsyncCallback(OnConnectRequest), listener);
  102.  
  103.  
  104.                 // It's on a different thread, so use Invoke.
  105.  
  106.          
  107.         }
  108.         private LoginSocket logInDelegate(Spine.Objects.loginpackage l)
  109.         {
  110.             LoginSocket L = new LoginSocket();
  111.             Spine.Objects.Player P = O.login(l);
  112.             L.P = P;
  113.             L.password = l.password;
  114.             L.username = l.username;
  115.             L.L = new Spine.Objects.logininfo(P.Session, P.Info.ID);
  116.             O.PlayerBuffer.Add(P);
  117.             return L;
  118.            
  119.         }
  120.  
  121.         private void Click_Tick(object sender, EventArgs e)
  122.         {
  123.             if (O.PlayerBuffer.Count > 0)
  124.             {
  125.                 string s = "s";
  126.             }
  127.         }
  128.  
  129.  
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement