All pastes #749995 Raw Edit

Someone

public text v1 · immutable
#749995 ·published 2007-10-26 03:22 UTC
rendered paste body
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Client
{
    public class TcpServer
    {
        private readonly AsyncCallback onClientConnectCallback;
        private readonly AsyncCallback onServerReceiveData;

        private Socket mSock;
        private IPEndPoint ipInfo;
        private bool isListening;

        private ClientDetailsCollection clients;

        public Socket MainSocket { get { return this.mSock; } }
        public IPEndPoint IPInfo { get { return this.ipInfo; } }
        public bool IsListening { get { return this.isListening; } }

        public ClientDetailsCollection Clients { get { return this.clients; } }

        public delegate void DataReceivedEventHandler(ClientDetails details, ClientState state, int recvBytes);
        public event DataReceivedEventHandler OnDataReceived;

        public delegate void ClientConnectedEventHandler(ClientDetails details);
        public event ClientConnectedEventHandler OnClientConnect;

        public TcpServer(IPAddress ip, int port)
        {
            this.onClientConnectCallback = new AsyncCallback(this.onClientConnect);
            this.onServerReceiveData = new AsyncCallback(this.onServerReceive);

            this.mSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.ipInfo = new IPEndPoint(ip, port);
            this.isListening = false;

            this.clients = new ClientDetailsCollection();
        }

        public virtual void Start()
        {
            this.Start(-1);
        }

        public virtual void Start(int backlog)
        {
            this.mSock.Bind(this.ipInfo);
            this.mSock.Listen(backlog);
            this.isListening = true;
            this.mSock.BeginAccept(this.onClientConnectCallback, this.mSock);
        }

        public virtual void Stop()
        {
            this.mSock.Shutdown(SocketShutdown.Both);
            this.mSock.Close(5);
            this.isListening = false;
        }

        private void onClientConnect(IAsyncResult asyn)
        {
            Socket listener = (Socket)asyn.AsyncState;

            Socket client = listener.EndAccept(asyn);

            ClientState state = new ClientState(client);
            ClientDetails details = new ClientDetails((IPEndPoint)client.RemoteEndPoint);

            this.clients.Add(details);

            if (this.OnClientConnect != null)
            {
                this.OnClientConnect(details);
            }

            client.BeginReceive(state.Buffer, 0, client.Available, SocketFlags.None, this.onServerReceiveData, state);
            listener.BeginAccept(this.onClientConnectCallback, asyn);
        }

        private void onServerReceive(IAsyncResult asyn)
        {
            ClientState state = (ClientState)asyn.AsyncState;

            int dataReceived = state.Socket.EndReceive(asyn);

            if (this.OnDataReceived != null && ( dataReceived > 0 ))
            {
                this.OnDataReceived(this.clients[(IPEndPoint)state.Socket.RemoteEndPoint], state, dataReceived);
            }

            state.Socket.BeginReceive(state.Buffer, 0, state.Socket.Available, SocketFlags.None, this.onServerReceiveData, state);
        }
    }
}