dbgagents/trkagent/engine/TrkSocketCommPort.cpp
changeset 0 c6b0df440bee
equal deleted inserted replaced
-1:000000000000 0:c6b0df440bee
       
     1 /*
       
     2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "TrkSocketCommPort.h"
       
    21 #include "TrkFramingLayer.h"
       
    22 #include "TrkEngine.h"
       
    23 #include <e32cons.h>
       
    24 
       
    25 
       
    26 CTrkSocketCommPort::CTrkSocketCommPort()
       
    27 	: CTrkCommPort(CActive::EPriorityStandard),
       
    28 	  iConnected(EFalse)
       
    29 {
       
    30 }
       
    31 
       
    32 void CTrkSocketCommPort::ConstructL(const TDesC &/*aInitFilePath*/, TDes& /*aErrorMessage*/)
       
    33 {
       
    34 	CActiveScheduler::Add(this); 
       
    35 }
       
    36 
       
    37 void CTrkSocketCommPort::IssueReadRequest()
       
    38 {
       
    39 	iNextReadChar = 0;
       
    40 	TSockXfrLength len;
       
    41 	iSocket.RecvOneOrMore(iReceivedChars, 0, iStatus, len);
       
    42 
       
    43 	SetActive();
       
    44 }
       
    45 
       
    46 void CTrkSocketCommPort::DoCancel()
       
    47 {
       
    48 	iSocket.CancelRead();
       
    49 }
       
    50 
       
    51 void CTrkSocketCommPort::RunL()
       
    52 {
       
    53 	if (iStatus.Int() == KErrNone)
       
    54 		while (iNextReadChar < iReceivedChars.Length())
       
    55 			iFramingLayer->HandleByte(iReceivedChars[iNextReadChar++]);
       
    56 
       
    57 	IssueReadRequest();
       
    58 }
       
    59 
       
    60 
       
    61 CTrkSocketCommPort::~CTrkSocketCommPort()
       
    62 {
       
    63 	Cancel();
       
    64 //	iSocket.Close();
       
    65 	iConnected = EFalse;
       
    66 	iSocketServ.Close();
       
    67 }
       
    68 
       
    69 
       
    70 CTrkSocketCommPort* CTrkSocketCommPort::NewL(const TDesC& aInitFilePath, TDes& aErrorMessage)
       
    71 {
       
    72 	CTrkSocketCommPort* self = new(ELeave) CTrkSocketCommPort;
       
    73 	CleanupStack::PushL(self);
       
    74 	self->ConstructL(aInitFilePath, aErrorMessage);
       
    75 	CleanupStack::Pop(self);
       
    76 	return self;
       
    77 }
       
    78 
       
    79 
       
    80 void CTrkSocketCommPort::OpenPortL()
       
    81 {
       
    82 	TInt error = KErrNone;
       
    83 	
       
    84 	error = iSocketServ.Connect();
       
    85 	if (error != KErrNone)
       
    86 		User::Leave(100);
       
    87 
       
    88 	error = iSocket.Open(iSocketServ, KAfInet, KSockStream, KProtocolInetTcp);
       
    89 	if (error != KErrNone)
       
    90 		User::Leave(101);
       
    91 
       
    92 	TUint protocols = 0;
       
    93 	
       
    94 	error = iSocketServ.NumProtocols(protocols);
       
    95 	
       
    96 	TProtocolDesc desc;
       
    97 	
       
    98 	for (TUint i=0; i<protocols; i++)
       
    99 	{
       
   100 		error = iSocketServ.GetProtocolInfo(i, desc);
       
   101 	}
       
   102 	
       
   103 }
       
   104 
       
   105 void CTrkSocketCommPort::ConnectL()
       
   106 {
       
   107 	TInetAddr address;
       
   108 	TInt err = KErrNone;
       
   109 	
       
   110 //	address.SetPort(10000);
       
   111 //	err = address.Input(_L("10.86.2.93"));
       
   112 //
       
   113 //	TRequestStatus stat;
       
   114 //	iSocket.Connect(address, stat);
       
   115 //	User::WaitForRequest(stat);
       
   116 
       
   117 //	iConnected = ETrue;
       
   118 
       
   119 //	TSockAddr test;
       
   120 //	iSocket.LocalName(test);
       
   121 //	TUint lp = iSocket.LocalPort();
       
   122 
       
   123 	
       
   124 	address.SetPort(6110);
       
   125 	err = address.Input(_L("127.0.0.1"));
       
   126 //	err = address.Input(_L("0.0.0.0"));
       
   127 
       
   128 	err = iSocket.Bind(address);
       
   129 	if (err != KErrNone)
       
   130 	{
       
   131 		User::Leave(err);
       
   132 	}
       
   133 
       
   134 	err = iSocket.Listen(1);
       
   135 	if (err != KErrNone)
       
   136 	{
       
   137 		User::Leave(err);
       
   138 	}
       
   139 
       
   140 	RSocket blankSocket;
       
   141 	err = blankSocket.Open(iSocketServ);
       
   142 
       
   143 	TRequestStatus stat;
       
   144 	iSocket.Accept(blankSocket, stat);
       
   145 //	SetActive();
       
   146 	User::WaitForRequest(stat);
       
   147 	User::Leave(5000);
       
   148 
       
   149 //	iSocket.Connect(address, iStatus);
       
   150 
       
   151 	iConnected = ETrue;
       
   152 }
       
   153 
       
   154 void CTrkSocketCommPort::ClosePort()
       
   155 {
       
   156 	Cancel();
       
   157 	
       
   158 	if (iConnected)
       
   159 	{
       
   160 		iSocket.Close();
       
   161 		iConnected = EFalse;
       
   162 	}
       
   163 
       
   164 	iSocketServ.Close();
       
   165 }
       
   166 
       
   167 void CTrkSocketCommPort::SendDataL(const TDesC8& aBuffer)
       
   168 {
       
   169 	TRequestStatus status;
       
   170 	iSocket.Write(aBuffer, status);
       
   171 	User::WaitForRequest(status);
       
   172 
       
   173 	User::LeaveIfError(status.Int());
       
   174 }
       
   175 
       
   176 void CTrkSocketCommPort::Listen(CTrkFramingLayer */*aFramingLayer*/)
       
   177 {
       
   178 //#warning - can leave
       
   179 //	if (!iConnected)
       
   180 //		ConnectL();
       
   181 //	iFramingLayer = aFramingLayer;
       
   182 //	CActiveScheduler::Add(this);
       
   183 //	IssueReadRequest();
       
   184 }
       
   185 
       
   186 void CTrkSocketCommPort::StopListening()
       
   187 {
       
   188 	Cancel();
       
   189 	Deque();
       
   190 }