Exemple #1
0
        /// <summary>
        /// Add the given connection to our list of clients.
        /// </summary>
        /// <param name="pSocket">Connection socket</param>
        public void NewConnection(Socket pSocket)
        {
            // This will block accepting sockets until a client is shuffled to
            // a new socket in the buffer.  This should stop any issues if
            // the server is DOS'd because of it's limit on connection
            // speed, however, load would be somewhat limited (i.e. you could
            // not have more than 1000 connections in a second).
            //
            // For my current uses of this library these limitations should be fine.
            SocketClientBuffer tmpClient = new SocketClientBuffer(pSocket);

            _ClientsConnected.Add(tmpClient);
            tmpClient._RemoteInformation = pSocket.RemoteEndPoint.ToString();
            this.WriteToLog(string.Concat("Client Connected from [", tmpClient._RemoteInformation, "]"), 1);
            tmpClient.Die += new SocketBufferEventHandler(ClientDied);

            //Raise the connection event
            this.ClientHasConnected(tmpClient.Sock.RemoteEndPoint.ToString(), tmpClient);
            tmpClient.SetupRecieveCallback(this);
        }
Exemple #2
0
        /// <summary>
        /// Recieve the data, and handle it in the class.
        /// </summary>
        /// <param name="pAsyncResult"></param>
        public void OnRecievedData(IAsyncResult pAsyncResult)
        {
            SocketClientBuffer tmpClientBuffer = (SocketClientBuffer)pAsyncResult.AsyncState;

            byte [] tmpRecievedBuffer = tmpClientBuffer.GetRecievedData(pAsyncResult);

            // If no data was recieved then the connection is probably dead
            if (tmpRecievedBuffer.Length < 1)
            {
                //Raise the disconnected event
                if (tmpClientBuffer._CleanUp == true)
                {
                    this.ClientHasDisconnected(tmpClientBuffer._RemoteInformation, tmpClientBuffer);
                }
                else
                {
                    this.ClientHasDisconnected(tmpClientBuffer.Sock.RemoteEndPoint.ToString(), tmpClientBuffer);
                    //Kill the socket
                    tmpClientBuffer.Sock.Close();
                    this._ClientsConnected.Remove(tmpClientBuffer);
                }

                this.WriteToLog(string.Concat("Client Disconnected from [", tmpClientBuffer._RemoteInformation, "]"), 2);

                return;
            }
            else
            {
                //Raise the recieved data event
                this.DataWasRecieved(tmpRecievedBuffer, tmpRecievedBuffer.Length, tmpClientBuffer);

                //Walk through the bytes that just came through and such.
                //This could be optional?  We may not need this granularity on incoming data.
                foreach (byte tmpByte in tmpRecievedBuffer)
                {
                    switch (tmpByte)
                    {
                    case 10:
                        //A CR was recieved
                        if (tmpClientBuffer._LineBuffer.Length > 0)
                        {
                            string tmpBuffer = tmpClientBuffer._LineBuffer.ToString();

                            //Clear the linebuffer
                            tmpClientBuffer._LineBuffer.Remove(0, tmpClientBuffer._LineBuffer.Length);

                            //Now fire the event
                            this.LineWasRecieved(tmpBuffer, tmpClientBuffer);
                        }
                        this.NewLineWasRecieved(tmpClientBuffer);
                        tmpClientBuffer._InputTouched = true;
                        break;

                    case 127:
                        //A Backspace was recieved, remove the end character
                        if (tmpClientBuffer._LineBuffer.Length > 0)
                        {
                            tmpClientBuffer._LineBuffer.Remove(tmpClientBuffer._LineBuffer.Length - 1, 1);
                            this.BackspaceWasRecieved(tmpClientBuffer);
                        }
                        break;

                    default:
                        //See if it's an "ok" ascii character
                        if ((tmpByte > 13) && (tmpByte < 147) && (tmpClientBuffer._LineBuffer.Length < 300))
                        {
                            //Append it to our line buffer
                            tmpClientBuffer._LineBuffer.Append(Convert.ToChar(tmpByte));
                        }
                        break;
                    }
                }
            }

            tmpClientBuffer.SetupRecieveCallback(this);
        }