Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program.cs:
- static void Main(string[] args)
- {
- MainSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
- GameAddr = "192.168.1.74";
- GamePort = 1626;
- MainSocket.Bind(new IPEndPoint(IPAddress.Parse(GameAddr), GamePort));
- MainSocket.Listen(10);
- CaptainEnvironment.Initialize(MainSocket);
- Console.WriteLine("Game socket bound to " + GameAddr + ":" + GamePort);
- while (true)
- {
- Console.ReadLine();
- Console.WriteLine(">> HELLO!!");
- }
- }
- CaptainEnvironment.cs:
- namespace Captain
- {
- static class CaptainEnvironment
- {
- static AsyncCallback AcceptGameConnection;
- static Socket MainSocket;
- static int ConnectionCount = 0;
- public static void Initialize(Socket _MainSocket)
- {
- MainSocket = _MainSocket;
- AcceptGameConnection = new AsyncCallback(AddConnection);
- MainSocket.BeginAccept(AcceptGameConnection, MainSocket);
- }
- public static void AddConnection(IAsyncResult Result)
- {
- Socket MainSocket = (Socket)Result.AsyncState;
- Socket UserSocket = MainSocket.EndAccept(Result);
- AcceptGameConnection = new AsyncCallback(AddConnection);
- MainSocket.BeginAccept(0, AcceptGameConnection, MainSocket);
- IPEndPoint UserAddr = UserSocket.RemoteEndPoint as IPEndPoint;
- Console.WriteLine(">> Game connection request from " + UserAddr.Address);
- Connection UserConnection = new Connection(UserSocket);
- Console.WriteLine(">> Connection accepted from " + UserAddr.Address);
- ConnectionCount++;
- }
- }
- }
- Connection.cs:
- namespace Captain.Communication
- {
- class Connection
- {
- public User Player;
- public Socket UserSocket;
- public int Id;
- public byte[] PacketBytes;
- private bool Connected;
- public AsyncCallback ReceivedCallback;
- public AsyncCallback SentCallback;
- public Connection(Socket _UserSocket)
- {
- // Login functions etc.
- UserSocket = _UserSocket;
- ReceivedCallback = new AsyncCallback(DataReceived);
- SentCallback = new AsyncCallback(DataSent);
- PacketBytes = new byte[4096];
- UserSocket.BeginReceive(PacketBytes, 0, PacketBytes.Length, SocketFlags.None, ReceivedCallback, null);
- }
- public void DataReceived(IAsyncResult Result)
- {
- try
- {
- UserSocket.EndReceive(Result);
- }
- catch (SocketException e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine(">> " + e.SocketErrorCode + ": " + e.Message);
- Console.ForegroundColor = ConsoleColor.White;
- CaptainEnvironment.RemoveConnection(this);
- return;
- }
- if (PacketBytes == null && !Player.Authenticated)
- {
- UserSocket.BeginSend(new byte[0], 0, 0, SocketFlags.None, SentCallback, null);
- return;
- }
- PacketBytes = Encoding.ASCII.GetBytes(Encoding.ASCII.GetString(PacketBytes).TrimEnd(new char[] { (char)(0) }));
- byte[] LenArr = new byte[] { (byte)PacketBytes[4], (byte)PacketBytes[5] };
- if(BitConverter.IsLittleEndian)
- {
- Array.Reverse(LenArr);
- }
- int PacketLength = BitConverter.ToInt16(LenArr, 0);
- Console.WriteLine(">> Length of incoming packet: " + PacketLength);
- HandlePacket(PacketBytes);
- if (Connected)
- {
- PacketBytes = new byte[4096];
- UserSocket.BeginReceive(PacketBytes, 0, PacketBytes.Length, SocketFlags.None, ReceivedCallback, null);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment