Exemplo n.º 1
0
// if connected, send out a message
// returns false if not connected or error.
// auto-splits large messages larger than buffer.
bool SockClient::sendc(CharString message){
    CharString emsg = (encryptor!=0x0) ? encryptor(message) : message; // Apply encryption
    if(emsg.getSize() <= 2) return false;
    //cout << "Send message(" << emsg.getSize() << ")" << emsg.get() << endl;
    cout << "Send message(" << emsg.getSize() << ")"<< endl;

    int flags=0;
    int looptimes=1;
    int i, msgret, msgsize;
    char* msg;
#ifdef LINUXXX
     if(!testAlive()){
         // uh...
         return false;
     }
    
    if(ctype == SC_TCP){
        //msgret = send(sockd, message.get(), message.getSize(), flags);
        //cout << "Pre-write SC_TCP " << emsg.getSize() << endl;
        if(testAlive())
            msgret = write(sockd, emsg.get(), emsg.getSize());
        //cout << "post-write" << endl;
    }else{
        socklen_t addrlen = sizeof(struct sockaddr_in);
        //msgret = sendto(sockd, emsg.get(), emsg.getSize(), &cli_addr, address);
    }
    // detect error
    if(msgret == -1) return false;
#endif
    return true;
}
Exemplo n.º 2
0
/* Returns the name of the end point.
*  Input: String location
*  Output: Very ending of string after last "/"
 */
CharString FileSystem::getNameFromString(CharString location) {
    // gets the name of the last "/dir/dir2/name", even if it ends with "/".
    if(location.get()[location.getSize()-1] == '/') {
        // remove trailing "/".
        location.get()[location.getSize()-1] = '\0';
        location.setSize(location.getSize()-1);
    }

    CharString result = CharString("");
    // split all by "/".
    if(location.contains("/")) {
        // get the very last result of the split.
        LinkedList<CharString>* sresult = location.split('/','`');
        result = *sresult->get(sresult->size()-1);
    } else {
        result = location;
    }

    return result;
}
Exemplo n.º 3
0
void Logger::processLog(CharString data){
    if(console){
        cout << data;// << endl;
        cout.flush();
    }
    
    // push data to file
    if(file.is_open()){
        file.write(data.get(), data.getSize());
        file.write((char*)"\n", 1);
    }
}
Exemplo n.º 4
0
void _msgClientHandler(CharString dataIn, CharString &dataOut, SockClient* client, void* instance){
    // handle packets. First 2 bytes on the packet are the channel to use.
    MSGServer *serv = (MSGServer*)instance;

    if(dataIn.getSize() > 2){
        
        // loop through and manage clients
        MSGClient* cli = serv->getMSGClient(client);
        
        if(cli != 0x0){
            _msgClientHandler_ANY_recv(serv, cli, dataIn, dataOut);
        
            //PacketChannel* channelx = cli->getChannel(channel);
            //channelx->addRecvPacketData(new CharString(dataIn.get(), dataIn.getSize()));
            //serv->handler (cli, channelx);
        }
    }
}
Exemplo n.º 5
0
// LinkedList<LinkedList<CharString>> lines = SimpleParseFile(file, '\n');
void APIMod::loadProperties(){ // , CharString name, CharString language, CharString version
    if(propertiesloaded) return;

    LinkedList<LinkedList<CharString>> lines = SimpleParseString(getModFileData("mod.properties"), '=');

    CharString depvers = "";
    LinkedListIterator<LinkedList<CharString>> lineit = lines.getIterator();
    while(lineit.hasNext()){
        LinkedList<CharString> p = lineit.next();
        CharString pname = p[0];
        if(pname.compare(CharString("name")))
            this->name = p[1];
        else if(pname.compare(CharString("unixname")))
            this->unixname = p[1];
        else if(pname.compare(CharString("language"))) // c++, java, etc.
            this->language = p[1];
        else if(pname.compare(CharString("version"))){
            this->versionstr = p[1];
            this->version = APIModVersion(p[1]);
        }else if(pname.compare(CharString("dependency_versions")))
            depvers = p[1];
    }

    this->modcwdloc = unixname; // relative folder location for mod
    
    // load dependency versioning information
    //      this->dependencyversions.add("mod1:v12938");
    if(depvers.getSize() > 3){
        if(depvers.contains(", ")){
            depvers.replace(" ", "");
            LinkedList<CharString> v = depvers.split(",");
            this->dependencyversions.addAll(v);
        }else{
            this->dependencyversions.add(depvers);
        }
    }


    propertiesloaded=true;
}
Exemplo n.º 6
0
void SockClient::readhandler(){
    int n;
    const int buflen = bufferSize;
    char buffer[buflen];
    CharString writeto;
    
    Logger::GLOBAL.log("[SockClient]  reading thread started");
    
    while(alive){
        
        n = read(sockd, buffer, buflen);
        if (n < 0){
            std::this_thread::sleep_for(std::chrono::milliseconds(5));
            continue;
        }else if(n==0){
            Logger::GLOBAL.log("[SockClient] disconnected from server");
            if(disconnected != 0x0) disconnected(server, this);
            alive = false;
            return;
        }
        
        // post-clear extra data in packet... (Prevents extra cpu usage, clearer data stream)
        for(int i=n-1;i<buflen;i++) buffer[i] = 0;

        if(_clientHandler != 0x0 && n > 1) {
            //CharString d = (server->decryptor!=0x0) ? server->decryptor(CharString(buffer,n)) : CharString(buffer,n); // decrypt
            CharString d = CharString(buffer,n); // decrypt
            _clientHandler(d, writeto, this, exVAL);

            if(writeto.getSize() > 0){
                // segment packet if it is too large.
                sendc(writeto); // encryption in-client
            }
        }
    }
    
    Logger::GLOBAL.log("[SockClient] reading thread ended");
    alive = false;
}
Exemplo n.º 7
0
// Handle basic client stuff
void ClientHandler_(SockClient* tclient) {
    // Talk with the client, pre-load into a buffer
    int n;
    char buffer[BUFFER_SIZEX];

    while(tclient->alive) {
#if __linux__ || __unix__


        n = read(tclient->sockd, buffer, BUFFER_SIZEX);
        if (n < 0) error("ERROR reading from socket");

        CharString* writeto = new CharString(); // writeto is a string that we write to, used by the

        if(tclient->_clientHandler != 0x0) {
            tclient->_clientHandler(new CharString(buffer,BUFFER_SIZEX), writeto);

            n = write(tclient->sockd, writeto->get(), writeto->getSize());
            if (n < 0) error("ERROR writing to socket");
        }
#endif // __linux__
    }
}
Exemplo n.º 8
0
/*
 * Desc: Takes in a single line and expresses the line in a math formula to solve it.
 * Input: CharString* Line.
 * Output: (Console) Output of all expressions.
 * */
CharString Eval(CharString Line) {
    //cout << line << endl;
    // get length of line
    int lsize = 0;
    for(int i=0; i<Line.getSize(); i++) {
        if(Line.get()[i] != 0x0) {
            lsize++;
        }
    }
    Line.setSize(lsize);


    // PARSE each section. Format SHOULD be like this:
    // 1 + 2 [number operator number]
    // 1 + 2 * 3 [n o n o n]
    // 1 + 5 * 10^2 [n o n o non]
    // 110/ 98 [no n] (Automatic parse finding)

    // list of operators:
    // () + - / * ^ % < > = <= >= [NOTE that as of this time, parentheses will not be used]

    // Thus, it can be infered that we can break up each system by placing the most important
    //  System (order of operations) on the top of the stack and placing the least important
    // +/- at the bottom of the stack...

    // Order of operations:
    //  ^  /  *  +  -  (>= = <= < > [END-CLASS operators])

    // Parse method:
    //  1.) loop-through n times to get full stack.

    NumStack = new Stack(); // NumStack is used to store the initial numbers
    OpStack = new Stack(); // OpStack is used to store the initial operators

    // note that both stacks store information through the same MathOperationNode Type.
    //  This will be solved later when we interlace the operators with the variables.
    char* LineD = Line.get();
    char* NumTemp = new char();
    int NumTempLen = 0;
    MOperator OpTemp = none;
    for(int i=0; i<Line.getSize(); i++) {
        char g = LineD[i];
        MOperator tOp = getOperatorFromChar(g);

        if(isCharNumber(g)) { // is this a number?
            // this is a single digit for NumTemp. Add to it.
            NumTemp[NumTempLen] = g;
            NumTempLen++;

            // dump OpTemp if it is available.
            dumpOp(OpTemp);
            OpTemp = none;
        } else if (tOp != none) {
            if(OpTemp != none) {
                MOperator gox = combineOperators(tOp,OpTemp);
                if(gox != none) OpTemp = gox;
            } else {
                OpTemp = tOp;
            }

            // dump NumTemp if it is available.
            dumpNum(NumTemp,NumTempLen);
            NumTempLen=0;
        } else {
            // dump NumTemp if it is available.
            // dump OpTemp if it is available.
            dumpNum(NumTemp,NumTempLen);
            NumTempLen=0;
            dumpOp(OpTemp);
            OpTemp = none;
        }
    }

    // dump NumTemp if it is available.
    // dump OpTemp if it is available.
    dumpNum(NumTemp,NumTempLen);
    NumTempLen=0;
    dumpOp(OpTemp);
    OpTemp = none;

    while(OpStack->size > 0) {
        int res = doOp();
        //cout << "=" << res << endl;
        NumStack->push(res);
    }
    int c = NumStack->pop();
    if(c == -98) cout << "F";
    else if(c == -99) cout << "T";
    else cout << c;

    return CharString::ConvertFromInt(c);
}