0

I've got a php application that requires to make a connection to a server which authenticates with a token, this token stays valid until connection is lost.

When another connection is made while the first is still open my application crashes because the token is different from the currently connected one...

public function connect()
{
    $Socket = fsockopen("192.168.1.1", 1234);
    if ($Socket !== false) {
        stream_set_timeout($Socket, static::TIMEOUT_SEC, static::TIMEOUT_USEC);
        $this->socket = $Socket;
        $this->sendeverything;
    }
}

How am I able to run a function like:

 function gogogo() {
      connect();
 }

multiple times without having them running simultaneously

Sorry for my bad english

Marco
  • 368
  • 1
  • 4
  • 16
  • are you using objects/classes here ? – Maximus2012 Sep 06 '13 at 15:50
  • have you got any other use of `$this->socket` other than for `sendeverything`? – bansi Sep 06 '13 at 15:52
  • No, this is the only moment i use the socket, however the socket is being used simultaneously with the next action, it needs to wait until the previously connection has been closed – Marco Sep 06 '13 at 15:53

3 Answers3

2

Most easy solution would be to have a is_connected function:

function connect() {
    if(is_already_connected()) {
        return;
    }
    // ... your connect logic
}

In the is_already_connected() you'll have to write some intelligent code to determine if there is an open connection.

You can also create a kind of singleton connection (although this suggestion would probably instantiate a lot of debate about the use of singletons ;))

giorgio
  • 10,111
  • 2
  • 28
  • 41
  • I am unable to drop the connection, this will result in loss of action --- sorry read your solution wrong, let me try this, seems pretty good – Marco Sep 06 '13 at 15:52
  • you don't have to drop the connection, you'll only have to check if a connection is already open (because in that case you don't have to open up another one). Or I just don't understand your question ;) - Oh wait, you revised your comment while I was typing mine :p – giorgio Sep 06 '13 at 15:54
  • haha, well after testing it doesn't seem to be about the connection... its about processing the data :(... still need to find a way to queue the process of functions – Marco Sep 06 '13 at 16:04
  • another thing you can try is, instead of just returning if already connected you can use the same connection and execute `sendeverything`, if the server can keep the connection open. Also you can open multiple connections if the server allows it. – bansi Sep 06 '13 at 16:04
1

Try something like this...

<?php

class Connection {
    public $Socket = null;

    public function connect(){
        // Checking if Socket already has a pointer :P
        if((bool)$this->Socket){
            return true;
        }

        $this->Socket = fsockopen("192.168.1.1", 1234);
        if ($this->Socket !== false) {
            stream_set_timeout($this->Socket, static::TIMEOUT_SEC, static::TIMEOUT_USEC);
            $this->sendeverything();
        }
    }
}

$myconnect = new Connection();
$myconnect->connect();
$myconnect->connect();

?>
roosevelt
  • 1,874
  • 4
  • 20
  • 27
0

As mentioned in this question you can use sem_aquire for this. Something like:

function connect(){
    $key = "192.168.1.1:1234" ;
    try{
        $sem = sem_get( $SEMKey);
        sem_acquire($sem);
        //Do connecty stuff here
        sem_release($sem);
    }catch(Exception $ex){
        //Exception handling
    }finally{
        //Finally only available in PHP 5.5 place this in catch and try if < 5.5
        sem_release($sem);
    }
}

Note that this is entirely untested and wont work on windows. If you are on windows you can use flock - again as mentioned in the above question.

Community
  • 1
  • 1
Jim
  • 22,354
  • 6
  • 52
  • 80