Example #1
0
bool
Msg2Msg::AddNewRcvSocket(const char* socketAddrStr)
{
    char charAddr[255];
    memset(charAddr,0,255);
    ProtoAddress rcv_addr;
    int rcv_port; 
    //parse socketAddrStr
    const char* index = strchr(socketAddrStr,'/');
    if(index!=NULL)
    {
        strncpy(charAddr,socketAddrStr,index-socketAddrStr);
        if(!(rcv_addr.ResolveFromString(charAddr)))
        {
            DMSG(0,"Msg2Msg::AddNewRcvSocket() Error setting address to %s\n",charAddr);
            return false;
        }
        rcv_port = atoi(index+1);
    }
    else
    {
        rcv_port = atoi(socketAddrStr);
    }
    ListableSocket *newSocketPtr = new ListableSocket(ProtoPipe::UDP);
    if(!newSocketPtr)
    {
        DMSG(0,"Msg2MsgAddNewRcvSocket error allocing new socket\n");
        return false;
    }
    newSocketPtr->SetNotifier(&GetSocketNotifier());
    newSocketPtr->SetListener(this,&Msg2Msg::OnSocketListenEvent);//bunny the this will need to be changed
    if(!(newSocketPtr->Open(rcv_port,ProtoAddress::IPv4)))
    {
        DMSG(0,"Msg2MsgAddNewRcvSocket Error Opening the socket rcv port %d\n",rcv_port);
        delete newSocketPtr;
        return false;
    }
    if(rcv_addr.IsMulticast()) //do multicast stuff
    {
        if(!newSocketPtr->JoinGroup(rcv_addr))
        {
            DMSG(0,"Msg2MsgAddNewRcvSocket Error joining the multicast group %s\n",rcv_addr.GetHostString());
            newSocketPtr->Close();
            delete newSocketPtr;
            return false;
        }
    }
    rcvSockets.Append(*newSocketPtr);
    return true;
}
Example #2
0
bool
Msg2Msg::AddNewSndSocket(const char* socketAddrStr)
{
    char charAddr[255];
    memset(charAddr,0,255);
    ProtoAddress dst_addr;
    int dst_port;
    int src_port;//bunny setting this to dst port for now this should be controled seperatly
    //parse the string
    const char* index = strchr(socketAddrStr,'/');
    if(index!=NULL)
    {
        strncpy(charAddr,socketAddrStr,index-socketAddrStr);
        if(!dst_addr.ResolveFromString(charAddr))
        {
            DMSG(0,"Msg2Msg::AddNewSndSocket: Error setting address to %s\n",socketAddrStr);
            return false;
        }
        dst_port = atoi(index+1);
        src_port = dst_port;
        dst_addr.SetPort(dst_port);
    }
    else
    {
        DMSG(0,"Msg2Msg::AddNewSndSocket: Error missing \"/\" for send socket command\n");
        return false;
    }
    ListableSocket *newSocketPtr = new ListableSocket(ProtoSocket::UDP);
    if(!newSocketPtr)
    {
        DMSG(0,"Msg2MsgAddNewSndSocket error allocing new socket\n");
        return false;
    }
    if(!(newSocketPtr->Bind(src_port)))
    {
        DMSG(0,"Msg2MsgAddNewSndSocket: Error binding to src_port %d\n",src_port);
        delete newSocketPtr;
        return false;
    }
    if(!(newSocketPtr->Open(src_port,ProtoAddress::IPv4,false)))
    {
        DMSG(0,"msg2MsgAddNewSndSocket: Error opening socket on port %d\n",src_port);
        delete newSocketPtr;
        return false;
    }
    if(dst_addr.IsMulticast())
    {
        if(!newSocketPtr->JoinGroup(dst_addr))
        {
            DMSG(0,"Msg2MsgAddNewSndSocket: Error joinging multicast Group %s\n",dst_addr.GetHostString());
            newSocketPtr->Close();
            delete newSocketPtr;
            return false;
        }
    }
    if(!(newSocketPtr->Connect(dst_addr)))
    {
        DMSG(0,"Msg2MsgAddNewSndSocket: Error connecting to %s\n",dst_addr.GetHostString());
        newSocketPtr->Close();
        delete newSocketPtr;
        return false;
    }
    sndSockets.Append(*newSocketPtr);
    return true;
}